这里是笔者自己写的mongodb单例工具类,仅供参考,已在开发中正常使用
可留言提出建议,一起探讨
package com.fusionskye.ezsonar.rpo.util;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.fusionskye.ezsonar.tools.utils.LoadProperties;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
public class MongoUtils {
private static DB db = null;
private static String host;
private static Integer port;
private static String name;
private static String username;
private static String password;
private MongoUtils() {
}
static {
InputStream is = null;
try {
String path = LoadProperties.getPath();
PropertiesUtil props = new PropertiesUtil(path, "mongo");
host = props.getProperty("mongodb.auth.host");
port = Integer.parseInt(props.getProperty("mongodb.auth.port"));
name = props.getProperty("mongodb.auth.name");
username = props.getProperty("mongodb.auth.username");
password = props.getProperty("mongodb.auth.password");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("加载db.properties文件失败");
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
is = null;
}
}
}
}
/**
* 连接数据库
*
* @return
*/
public static synchronized DB getConnection() {
if (db == null) {
MongoClient mongoClient = null;
try {
//IP地址 和 端口
ServerAddress serverAddress = new ServerAddress(host, port);
List<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createCredential(username, name, password.toCharArray());
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
// 通过连接认证获取MongoDB连接
mongoClient = new MongoClient(addrs, credentials);
// 连接到数据库
db = mongoClient.getDB(name);
System.out.println(host + "--" + port + "--" + name + "--" + username + "--" + password);
System.out.println("Connect to database successfully");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
return db;
}
// 获取集合连接
public static DBCollection getCollection(String collectionName) {
return getConnection().getCollection(collectionName);
}
/**
* 将实体类所有属性值添加到BasicDBObject中
*
* @param model
* @param bdo
* @return
*/
public static BasicDBObject putIntoDBObject(SuperPersistentObject model) {
BasicDBObject bdo = new BasicDBObject();
try {
if (model != null) {
Field[] fields = model.getClass().getDeclaredFields();
for (Field field : fields) {
// id交由mongodb自动生成
if (!"objId".equals(field.getName())) {
field.setAccessible(true);
String classType = field.getType().toString();
int lastIndex = classType.lastIndexOf(".");
classType = classType.substring(lastIndex + 1);
if (field.get(model) != null) {
bdo.put(field.getName(), field.get(model));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bdo;
}
/**
* 将实体类所有属性值添加到BasicDBObject中,使用原来的BasicDBObject
*
* @param model
* @param bdo
* @return
*/
public static BasicDBObject putIntoDBObjectEdit(SuperPersistentObject model, BasicDBObject bdo) {
try {
if (model != null) {
Field[] fields = model.getClass().getDeclaredFields();
for (Field field : fields) {
// id交由mongodb自动生成
if (!"objId".equals(field.getName())) {
field.setAccessible(true);
String classType = field.getType().toString();
int lastIndex = classType.lastIndexOf(".");
classType = classType.substring(lastIndex + 1);
if (field.get(model) != null) {
bdo.put(field.getName(), field.get(model));
} else {
bdo.put(field.getName(), "");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bdo;
}
}
mongodb工具类
最新推荐文章于 2023-07-14 10:26:28 发布