博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BaseDB MongoDb dao数据库操作类
阅读量:6909 次
发布时间:2019-06-27

本文共 6509 字,大约阅读时间需要 21 分钟。

  hot3.png

package cn.ohalo.db.mongodb;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import com.alibaba.fastjson.util.TypeUtils;import com.mongodb.BasicDBObject;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.WriteConcern;import com.mongodb.WriteResult;/** *  * @author halo *  */public class BaseDb
 { public static Log logger = LogFactory.getLog(BaseDb.class); private DBCollection collection; private String collectionName; private Class
 entityClass; public String getCollectionName() { return collectionName; } public void initDBCollection() { MongoConnection.initMongodb(); MongoConnection.setWriteConcern(WriteConcern.SAFE); } public void setCollectionName(String collectionName) { this.collectionName = collectionName; collection = MongoConnection.initCollection(collectionName); } public DBCollection getCollection() { return collection; } @SuppressWarnings("unchecked") public BaseDb() { initDBCollection(); Type genericType = getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType) genericType) .getActualTypeArguments(); entityClass = (Class
) params[0]; } public T findOne() { return TypeUtils.castToJavaBean(collection.findOne().toMap(), entityClass); } /**  * 根据参数查询所有的文档信息  *   * @param params  *            查询参数  * @return 返回文档集合  */ public List
 findAll(DBObject params) { return findAllAndSortAndLimit(params, null, null, null); } /**  * 根据参数查询所有的文档信息  *   * @param params  *            查询参数  * @return 返回文档集合  */ public List
 findAll(T t) { return findAll(t == null ? null : t.toDBObject()); } /**  * 查询所有的文档信息,并进行排序  *   * @param params  *            查询参数  * @param sortparams  *            排序参数 1为正序排列 , -1为倒序排列  * @return 返回文档集合  */ public List
 findAllAndSort(DBObject params, DBObject sortparams) { return findAllAndSortAndLimit(params, sortparams, null, null); } /**  * 查询所有的文档信息,并进行排序  *   * @param params  *            查询参数  * @param sortparams  *            排序参数 1为正序排列 , -1为倒序排列  * @return 返回文档集合  */ public List
 findAllAndSort(T t, DBObject sortparams) { return findAllAndSort(t == null ? null : t.toDBObject(), sortparams); } /**  * 查询符合条件的文档数量  *   * @param params  *            查询参数  * @return  */ public Long count(DBObject params) { return collection.count(params); } /**  * 查询符合条件的文档数量  *   * @param params  *            查询参数  * @return  */ public Long count(T t) { return count(t == null ? null : t.toDBObject()); } /**  * 查询所有的文档记录信息,并且对这些信息进行排序和分页  *   * @param params  *            查询参数  * @param sortparams  *            排序参数  * @param skip  *            从第几个位置开始  * @param limit  *            查询几条记录  * @return  */ public List
 findAllAndSortAndLimit(DBObject params, DBObject sortparams, Integer skip, Integer limit) { DBCursor cursor = null; if (params == null) { cursor = collection.find(); } else { cursor = collection.find(params); } if (sortparams != null) { cursor = cursor.sort(sortparams); } if (skip != null) { cursor = cursor.skip(skip); } if (limit != null && limit > 0) { cursor = cursor.limit(limit); } List
 list = new ArrayList
(); while (cursor.hasNext()) { T t = TypeUtils.castToJavaBean(cursor.next().toMap(), entityClass); list.add(t); } return list; } /**  * 查询所有的文档记录信息,并且对这些信息进行排序和分页  *   * @param params  *            查询参数  * @param sortparams  *            排序参数  * @param skip  *            从第几个位置开始  * @param limit  *            查询几条记录  * @return  */ public List
 findAllAndSortAndLimit(T t, DBObject sortparams, Integer skip, Integer limit) { return findAllAndSortAndLimit(t == null ? null : t.toDBObject(), sortparams, skip, limit); } /**  * 查询多条文档  *   * @param objs  *            文档集合  * @return 返回插入的文档成功或者是失败 ,true 为成功, false 为失败  */ public boolean insert(List
 objs) { boolean flag = false; try { WriteResult result = collection.insert(objs); if (result.getN() > 0) { flag = true; } } catch (Exception e) { logger.error("插入数据库出现异常,数据库名称:" + this.getCollectionName() + ",插入参数:" + objs == null ? "null" : objs.toString(), e); } return flag; } /**  * 插入单条文档  *   * @param obj  *            单个文档  * @return 返回插入的文档成功或者是失败 ,true 为成功, false 为失败  */ public boolean insert(DBObject obj) { boolean flag = false; try { WriteResult result = collection.insert(obj); if (result.getN() > 0) { flag = true; } } catch (Exception e) { logger.error("插入数据库出现异常,数据库名称:" + this.getCollectionName() + ",插入参数:" + obj == null ? "null" : obj.toString(), e); } return flag; } /**  * 插入更新数据库  *   * @param obj  * @return  */ public boolean saveOrUpdate(DBObject obj) { boolean flag = false; try { WriteResult result = collection.save(obj); if (result.getN() > 0) { flag = true; } } catch (Exception e) { logger.error("插入数据库或者是更新数据库出现异常,数据库名称:" + this.getCollectionName() + ",插入参数:" + obj == null ? "null" : obj.toString(), e); } return flag; } /**  * 插入更新数据库  *   * @param obj  * @return  */ public boolean saveOrUpdate(T t) { return saveOrUpdate(t == null ? null : t.toDBObject()); } /**  * 插入单条文档  *   * @param obj  *            单个文档  * @return 返回插入的文档成功或者是失败 ,true 为成功, false 为失败  */ public boolean insert(T t) { return insert(t == null ? null : t.toDBObject()); } /**  * 更新文档信息  *   * @param queryParams  *            查询参数  * @param obj  *            更新文档  * @return 返回插入的文档成功或者是失败 ,true 为成功, false 为失败  */ public boolean update(DBObject queryParams, DBObject obj) { boolean flag = false; try { WriteResult result = collection.update(queryParams, new BasicDBObject().append("$set", obj), true, true); if (result.getN() > 0) { flag = true; } } catch (Exception e) { logger.error( "更新数据库出现异常,数据库名称:" + this.getCollectionName() + ",更新参数:" + obj == null ? "null" : obj.toString() + ",查询参数:" + queryParams == null ? "null" : queryParams .toString(), e); } return flag; } /**  * 更新文档信息  *   * @param queryParams  *            查询参数  * @param obj  *            更新文档  * @return 返回插入的文档成功或者是失败 ,true 为成功, false 为失败  */ public boolean update(DBObject queryParams, T t) { return update(queryParams, t == null ? null : t.toDBObject()); } /**  * 删除数据库的文档信息  *   * @param obj  * @return  */ public boolean remove(DBObject obj) { boolean flag = false; try { WriteResult result = collection.remove(obj); if (result.getN() > 0) { flag = true; } } catch (Exception e) { logger.error("删除数据出现异常,数据库名称:" + this.getCollectionName() + ",删除参数:" + obj == null ? "null" : obj.toString(), e); } return flag; } /**  * 删除数据库的文档信息  *   * @param obj  * @return  */ public boolean remove(T t) { return remove(t == null ? null : t.toDBObject()); } /**  * 删除这个collection所有文档信息  */ public void removeAll() { collection.drop(); }}

转载于:https://my.oschina.net/ohalo/blog/308319

你可能感兴趣的文章
SolrCloud 6.6.2 之 Collection API
查看>>
运用Oltu框架搭建OAuth的Demo工程
查看>>
优化Android应用内存的若干方法
查看>>
ajax get和post跨域问题解决方法
查看>>
Android库和项目收集
查看>>
组织机构构建说明
查看>>
gitlab安装包下载的两种方法
查看>>
深入理解Loadrunner中的Browser Emulation
查看>>
mysql 中文按照拼音排序
查看>>
Python的函数参数
查看>>
android开发之安全防护
查看>>
使用AnimationListener设置应用开启时的欢迎界面
查看>>
shell脚本编程学习之路-字符串测试表达式
查看>>
memcache,php装载memcache模块
查看>>
H3C的简单使用方法
查看>>
C++除零异常
查看>>
css的兼容问题汇总
查看>>
android apk 防止反编译技术第五篇-完整性校验(转)
查看>>
ios优秀开发者笔记汇总
查看>>
CSS 异步加载技术 不影响页面渲染
查看>>