|
|
@@ -5,11 +5,13 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.mongodb.MongoClient;
|
|
|
import com.mongodb.MongoCredential;
|
|
|
import com.mongodb.ServerAddress;
|
|
|
+import com.mongodb.client.MongoDatabase;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
/**
|
|
|
@@ -22,33 +24,47 @@ import java.util.Properties;
|
|
|
public class MongoUtils {
|
|
|
|
|
|
private static MongoClient mongoClient;
|
|
|
+ private static MongoDatabase mongoDatabase;
|
|
|
|
|
|
- static {
|
|
|
- ClassPathResource resource = new ClassPathResource("mongo.properties");
|
|
|
- Properties properties = new Properties();
|
|
|
- try {
|
|
|
- properties.load(resource.getStream());
|
|
|
- //通过连接认证获取MongoDB连接
|
|
|
- ServerAddress serverAddress = new ServerAddress("192.168.1.218",27017);
|
|
|
- List<ServerAddress> addrs = new ArrayList<ServerAddress>();
|
|
|
- addrs.add(serverAddress);
|
|
|
-
|
|
|
- //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
|
|
|
- MongoCredential credential = MongoCredential.createScramSha1Credential("root", "ldt", "123456".toCharArray());
|
|
|
- List<MongoCredential> credentials = new ArrayList<MongoCredential>();
|
|
|
- credentials.add(credential);
|
|
|
-
|
|
|
- //通过连接认证获取MongoDB连接
|
|
|
- mongoClient = new com.mongodb.MongoClient(addrs,credentials);
|
|
|
- System.out.println(JSON.toJSONString(properties));
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("============================== error start ( Mongo connect error ) =====================");
|
|
|
- e.printStackTrace();
|
|
|
- log.error("============================== error end ( Mongo connect error ) =====================");
|
|
|
+
|
|
|
+ public static MongoDatabase getMongoDatabase(String databaseName){
|
|
|
+ if(Objects.isNull(mongoClient)){
|
|
|
+ ClassPathResource resource = new ClassPathResource("mongo.properties");
|
|
|
+ Properties properties = new Properties();
|
|
|
+ try {
|
|
|
+ properties.load(resource.getStream());
|
|
|
+
|
|
|
+ String host = properties.getProperty("host");
|
|
|
+ int port = Integer.parseInt(properties.getProperty("port","27017"));
|
|
|
+ String userName = properties.getProperty("userName");
|
|
|
+ String password = properties.getProperty("password");
|
|
|
+ String database = properties.getProperty("database");
|
|
|
+
|
|
|
+ //通过连接认证获取MongoDB连接
|
|
|
+ ServerAddress serverAddress = new ServerAddress(host,port);
|
|
|
+ List<ServerAddress> addrs = new ArrayList<ServerAddress>();
|
|
|
+ addrs.add(serverAddress);
|
|
|
+
|
|
|
+ //MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
|
|
|
+ MongoCredential credential = MongoCredential.createScramSha1Credential(userName, database, password.toCharArray());
|
|
|
+ List<MongoCredential> credentials = new ArrayList<MongoCredential>();
|
|
|
+ credentials.add(credential);
|
|
|
+
|
|
|
+ //通过连接认证获取MongoDB连接
|
|
|
+ mongoClient = new com.mongodb.MongoClient(addrs,credentials);
|
|
|
+ System.out.println(JSON.toJSONString(properties));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("============================== error start ( Mongo connect error ) =====================");
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("============================== error end ( Mongo connect error ) =====================");
|
|
|
+ }
|
|
|
}
|
|
|
+ return mongoClient.getDatabase(databaseName);
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- new MongoUtils();
|
|
|
+
|
|
|
+ public static void main(String[] args){
|
|
|
+ MongoDatabase ldt = MongoUtils.getMongoDatabase("ldt");
|
|
|
+ System.out.println(ldt);
|
|
|
}
|
|
|
}
|