mongodb工具类

这里是笔者自己写的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;
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值