当前位置:首页 > 开发 > 数据库 > 正文

mongodb工具类

发表于: 2015-03-04   作者:cmysunny   来源:转载   浏览:
摘要: package org.sunstar.util; import java.net.UnknownHostException; import java.util.List; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.P
package org.sunstar.util;

import java.net.UnknownHostException;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;

/**
* MongodbUtil
*
* @author chenlongquan
*
*/
public class MongodbUtil {

private final static ThreadLocal<MongoClient> mongoClientes = new ThreadLocal<MongoClient>();

private static final Integer SOCKET_TIMEOUT = 300000;
private static final Integer CONNECTIONS_PER_HOST = 500;
private static final Integer threadsAllowedToBlockForConnectionMultiplier = 500;

public static DB getdb() {
try {
return getMongos().getDB(new PropertiesConfiguration("mongo").getString("db"));
} catch (ConfigurationException e) {
e.printStackTrace();
return null;
}
}

public static MongoClient getMongos() {
MongoClient mongoClient = mongoClientes.get();
if (mongoClient == null) {
try {
mongoClient = new MongoClient(new ServerAddress(new PropertiesConfiguration("mongo").getString("host"),
new PropertiesConfiguration("mongo").getInt("port")), new MongoClientOptions.Builder()
.socketTimeout(SOCKET_TIMEOUT).connectionsPerHost(CONNECTIONS_PER_HOST)
.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier)
.socketKeepAlive(true).build());
} catch (UnknownHostException | ConfigurationException e) {
e.printStackTrace();
}
mongoClientes.set(mongoClient);

}
return mongoClient;
}

public static void close() {
MongoClient mongoClient = mongoClientes.get();
if (mongoClient != null) {
mongoClient.close();
mongoClientes.remove();
}
}

/**
* 获取集合(表)
*
* @param collection
*/
public static DBCollection getCollection(String collection) {

return getdb().getCollection(collection);
}

/**
* 插入
*
* @param collection
* @param o 插入
*
*/
public static void insert(String collection, DBObject o) {

getCollection(collection).insert(o);
}

/**
* 批量插入
*
* @param collection
* @param list
*            插入的列表
*/
public static void insertBatch(String collection, List<DBObject> list) {

if ((list == null) || list.isEmpty()) {
return;
}

getCollection(collection).insert(list);

}

/**
* 删除
*
* @param collection
* @param q
*            查询条件
*/
public static void delete(String collection, DBObject q) {

getCollection(collection).remove(q);
}

/**
* 批量删除
*
* @param collection
* @param list
*            删除条件列表
*/
public static void deleteBatch(String collection, List<DBObject> list) {

if ((list == null) || list.isEmpty()) {
return;
}

for (int i = 0; i < list.size(); i++) {
getCollection(collection).remove(list.get(i));
}
}

/**
* 更新
*
* @param collection
* @param q
*            查询条件
* @param setFields
*            更新对象
*/
public static void update(String collection, DBObject q, DBObject setFields) {

getCollection(collection).updateMulti(q, new BasicDBObject("$set", setFields));
}

/**
* 查找集合所有对象
*
* @param collection
*/
public static List<DBObject> findAll(String collection) {

return getCollection(collection).find().toArray();
}

/**
* 按顺序查找集合所有对象
*
* @param collection
*            数据集
* @param orderBy
*            排序
*/
public static List<DBObject> findAll(String collection, DBObject orderBy) {

return getCollection(collection).find().sort(orderBy).toArray();
}

/**
* 查找(返回一个对象)
*
* @param collection
* @param q
*            查询条件
*/
public static DBObject findOne(String collection, DBObject q) {

return getCollection(collection).findOne(q);
}

/**
* 查找(返回一个对象)
*
* @param collection
* @param q
*            查询条件
* @param fileds
*            返回字段
*/
public static DBObject findOne(String collection, DBObject q, DBObject fileds) {

return getCollection(collection).findOne(q, fileds);
}

/**
* 分页查找集合对象,返回特定字段
*
* @param collection
* @param q
*            查询条件
* @param fileds
*            返回字段
* @pageNo 第n页
* @perPageCount 每页记录数
*/
public static List<DBObject> findLess(String collection, DBObject q, DBObject fileds, int pageNo, int perPageCount) {

return getCollection(collection).find(q, fileds).skip((pageNo - 1) * perPageCount).limit(perPageCount)
.toArray();
}

}

mongodb工具类

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号