MongoDB Helper的简单封装(二)

2015-11-21 01:51:09 · 作者: · 浏览: 16
, null); }else{ collection=this.getDBCollection(collectionName, null, null, null); } result=collection.save( new BasicDBObject(map) ); } catch (Exception e) { LogUtil.log.error(e); e.printStackTrace(); } finally{ this.closeAll(mongo, db); } return result.getN(); } /** * 添加对象 * @param map * @param collection * @return */ public int addObjects(Map map,String collectionName){ WriteResult result=null; try { if(collectionName==null){ collection=this.getDBCollection(null, null, null, null); }else{ collection=this.getDBCollection(collectionName, null, null, null); } result=collection.save( new BasicDBObject(map) ); } catch (Exception e) { LogUtil.log.error(e); e.printStackTrace(); } finally{ this.closeAll(mongo, db); } return result.getN(); } /** * 查询单个结果 * @param params * @param collectionName * @return */ public Map findOne(Map params,String collectionName){ Map map=new HashMap(); try { if(collectionName==null){ collection=this.getDBCollection(null, null, null, null); }else{ collection=this.getDBCollection(collectionName, null, null, null); } DBObject object; if(params!=null){ object=collection.findOne( new BasicDBObject(params) ); }else{ object=collection.findOne(); } Set keys=object.keySet(); for(String key:keys){ map.put(key, object.get(key)); } }catch (MongoException e) { LogUtil.log.error(e); e.printStackTrace(); }finally{ this.closeAll(mongo, db); } return map; } public Object findOneToObject(Map params,String collectionName){ DBObject object = null; try { if(collectionName==null){ collection=this.getDBCollection(null, null, null, null); }else{ collection=this.getDBCollection(collectionName, null, null, null); } if(params!=null){ object=collection.findOne( new BasicDBObject(params) ); }else{ object=collection.findOne(); } }catch (MongoException e) { LogUtil.log.error(e); e.printStackTrace(); }finally{ this.closeAll(mongo, db); } return object; } public List
find(Map params,String collectionName){ List list=new ArrayList(); try { if(collectionName==null){ collection=this.getDBCollection(null, null, null, null); }else{ collection=this.getDBCollection(collectionName, null, null, null); } DBCursor cursor; if(params!=null){ cursor=collection.find(new BasicDBObject(params) ); }else{ cursor=collection.find(); } DBObject object; while(cursor.hasNext()){ object=cursor.next(); list.add(object); } }catch (MongoException e) { LogUtil.log.error(e); e.printStackTrace(); }finally{ this.closeAll(mongo, db); } return list; } /** * 获取总记录条数 * @param params:参数列表 * @return:总记录数 */ public int getTotal(Map params,String collectionName){ int total=0; try { if(collectionName==null){ collection=this.getDBCollection(null, null, null, null); }else{ collection=this.getDBCollection(collectionName, null, null, null); } if(params!=null){ total=(int) collection.count( new BasicDBObject(params) ); }else{ total=(int) collection.count(); } }catch (MongoException e) { LogUtil.log.error(e); e.printStackTrace(); }finally{ this.closeAll(mongo, db); } return total; } /