generated from root/miduo_server
108 lines
4.4 KiB
Java
108 lines
4.4 KiB
Java
|
|
package com.ljsd.util;
|
||
|
|
|
||
|
|
import com.mongodb.*;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
/*
|
||
|
|
* mongodb数据库链接池
|
||
|
|
*/
|
||
|
|
public class MyMongoDBPool {
|
||
|
|
private MongoClient mongoClient = null;
|
||
|
|
private String dbName;
|
||
|
|
private DB db;
|
||
|
|
|
||
|
|
public MyMongoDBPool() {
|
||
|
|
MongoClientOptions.Builder buide = new MongoClientOptions.Builder();
|
||
|
|
buide.connectionsPerHost(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maximumNumberOfConnections")));// 与目标数据库可以建立的最大链接数
|
||
|
|
buide.connectTimeout(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_connectTimeout")));// 与数据库建立链接的超时时间
|
||
|
|
buide.maxWaitTime(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maxWaitTime")));// 一个线程成功获取到一个可用数据库之前的最大等待时间
|
||
|
|
buide.threadsAllowedToBlockForConnectionMultiplier(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_threadsAllowedToBlockForConnectionMultiplier")));
|
||
|
|
buide.maxConnectionIdleTime(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maxConnectionIdleTime")));
|
||
|
|
buide.maxConnectionLifeTime(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_maxConnectionLifeTime")));
|
||
|
|
buide.socketTimeout(Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_socketTimeout")));
|
||
|
|
buide.socketKeepAlive(Boolean.parseBoolean(BaseGlobal.properties.getProperty("mongodb_socketKeepAlive")));
|
||
|
|
MongoClientOptions myOptions = buide.build();
|
||
|
|
mongoClient = new MongoClient(new ServerAddress(BaseGlobal.properties.getProperty("mongodb_host"), Integer.parseInt(BaseGlobal.properties.getProperty("mongodb_port"))),
|
||
|
|
myOptions);
|
||
|
|
dbName = BaseGlobal.properties.getProperty("mongodb_name");
|
||
|
|
db = mongoClient.getDB(dbName);
|
||
|
|
}
|
||
|
|
|
||
|
|
public DBCollection getCollection(String collectionName) {
|
||
|
|
return db.getCollection(collectionName);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public DBObject findOne(String collectionName, String id) throws Exception {
|
||
|
|
return findOne2(collectionName, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<DBObject> find(String collectionName, DBObject object) throws Exception {
|
||
|
|
DBCollection coll = db.getCollection(collectionName);
|
||
|
|
DBCursor cursor = coll.find(object);
|
||
|
|
List<DBObject> docs = new ArrayList<>();
|
||
|
|
while (cursor.hasNext()) {
|
||
|
|
DBObject doc = cursor.next();
|
||
|
|
docs.add(doc);
|
||
|
|
}
|
||
|
|
return docs;
|
||
|
|
}
|
||
|
|
|
||
|
|
public String findList(String collectionName) throws Exception {
|
||
|
|
DBCollection coll = db.getCollection(collectionName);
|
||
|
|
DBCursor cursor = coll.find();
|
||
|
|
List<DBObject> docs = new ArrayList<>();
|
||
|
|
|
||
|
|
while (cursor.hasNext()) {
|
||
|
|
DBObject doc = cursor.next();
|
||
|
|
docs.add(doc);
|
||
|
|
}
|
||
|
|
return docs.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public DBObject findOne(String collectionName, Integer id) throws Exception {
|
||
|
|
return findOne2(collectionName, id);
|
||
|
|
}
|
||
|
|
|
||
|
|
private DBObject findOne2(String collectionName, Object id) throws Exception {
|
||
|
|
DBCollection coll = db.getCollection(collectionName);
|
||
|
|
return coll.findOne(id.toString());
|
||
|
|
}
|
||
|
|
|
||
|
|
public void updateValue(String collectionName, BasicDBObject searchQuery, BasicDBObject dbObject) {
|
||
|
|
DBCollection coll = db.getCollection(collectionName);
|
||
|
|
coll.update(searchQuery, dbObject);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void save(String collectionName, DBObject doc) {
|
||
|
|
DBCollection coll = db.getCollection(collectionName);
|
||
|
|
coll.save(doc);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public int inc(String collectionName, String id, String key) {
|
||
|
|
DBCollection collection = db.getCollection(collectionName);
|
||
|
|
BasicDBObject searchQuery = new BasicDBObject();
|
||
|
|
searchQuery.put("_id", id);
|
||
|
|
// 使用collection的find方法查找document
|
||
|
|
DBCursor cursor = collection.find(searchQuery);
|
||
|
|
// 循环输出结果
|
||
|
|
if (cursor.hasNext()) {
|
||
|
|
BasicDBObject update = new BasicDBObject().append("$inc", new BasicDBObject().append(key, 1));
|
||
|
|
DBObject query = cursor.next();
|
||
|
|
DBObject result = collection.findAndModify(query, update);
|
||
|
|
return Integer.valueOf(result.get(key).toString()) + 1;
|
||
|
|
} else {
|
||
|
|
searchQuery.put(key, 1);
|
||
|
|
collection.insert(searchQuery);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public int inc(String key) {
|
||
|
|
return inc("inc_c", "inc_k", key);
|
||
|
|
}
|
||
|
|
}
|