用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

MongoDB Java 通用方法 MongoDB Manager

2015-02-07 作者: java源代码大全举报

[java]代码库

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
* MongoDB Manager
* @author Ken Chau
*
*/
public class MongoDBManager {
     private static Logger log = Logger.getLogger(MongoDBManager.class);

     private static Mongo mg = null;
     private static DB db = null;

     private final static MongoDBManager instance = new MongoDBManager();

     /**
     * 实例化
     * @return
     * @throws Exception
     */
     public static MongoDBManager getInstance() throws Exception {
            return instance;
     }

     static {
          try {
               mg = new Mongo(HOST, PORT);
              db=mg.getDB(DB);
          } catch (Exception e) {
               log.error("Can't connect MongoDB!");
              e.printStackTrace();
          }
     }

     /**
     * 获取集合(表)
     * @param collection
     */
     public static DBCollection getCollection(String collection) {
          return db.getCollection(collection);
     }

     /**
     * ----------------------------------分割线--------------------------------------
     */

     /**
     * 插入
     * @param collection
     * @param map
     */
     public void insert(String collection , Map<String, Object> map) {
          try {
               DBObject dbObject = map2Obj(map);
               getCollection(collection).insert(dbObject);
          } catch (MongoException e) {
               log.error("MongoException:" + e.getMessage());
          }
     }

     /**
     * 批量插入
     * @param collection
     * @param list
     */
     public void insertBatch(String collection ,List<Map<String, Object>> list) {
          if (list == null || list.isEmpty()) {
               return;
          }
          try {
               List<DBObject> listDB = new ArrayList<DBObject>();
               for (int i = 0; i < list.size(); i++) {
                    DBObject dbObject = map2Obj(list.get(i));
                    listDB.add(dbObject);
               }
               getCollection(collection).insert(listDB);
          } catch (MongoException e) {
               log.error("MongoException:" + e.getMessage());
          }
     }

     /**
     * 删除
     * @param collection
     * @param map
     */
     public void delete(String collection ,Map<String, Object> map) {
          DBObject obj = map2Obj(map);
          getCollection(collection).remove(obj);
     }

     /**
       * 删除全部
       * @param collection
       * @param map
       */
     public void deleteAll(String collection) {
          List<DBObject> rs = findAll(collection);
          if (rs != null &amp;&amp; !rs.isEmpty()) {
               for (int i = 0; i < rs.size(); i++) {
                    getCollection(collection).remove(rs.get(i));
               }
          }
     }

     /**
     * 批量删除
     * @param collection
     * @param list
     */
     public void deleteBatch(String collection,List<Map<String, Object>> list) {
          if (list == null || list.isEmpty()) {
               return;
          }
          for (int i = 0; i < list.size(); i++) {
               getCollection(collection).remove(map2Obj(list.get(i)));
          }
     }

     /**
     * 计算满足条件条数
     * @param collection
     * @param map
     */
     public long getCount(String collection,Map<String, Object> map) {
          return getCollection(collection).getCount(map2Obj(map));
     }

     /**
     * 计算集合总条数
     * @param collection
     * @param map
     */
     public long getCount(String collection) {
          return getCollection(collection).find().count();
     }

     /**
     * 更新
     * @param collection
     * @param setFields
     * @param whereFields
     */
     public void update(String collection,Map<String, Object> setFields,
               Map<String, Object> whereFields) {
          DBObject obj1 = map2Obj(setFields);
          DBObject obj2 = map2Obj(whereFields);
          getCollection(collection).updateMulti(obj1, obj2);
     }

     /**
     * 查找对象(根据主键_id)
     * @param collection
     * @param _id
     */
     public DBObject findById(String collection,String _id) {
          DBObject obj = new BasicDBObject();
          obj.put("_id", ObjectId.massageToObjectId(_id));
          return getCollection(collection).findOne(obj);
     }

     /**
     * 查找集合所有对象
     * @param collection
     */
     public List<DBObject> findAll(String collection) {
          return getCollection(collection).find().toArray();
     }

     /**
     * 查找(返回一个对象)
     * @param map
     * @param collection
     */
     public DBObject findOne(String collection,Map<String, Object> map) {
          DBCollection coll = getCollection(collection);
          return coll.findOne(map2Obj(map));
     }

     /**
     * 查找(返回一个List<DBObject>)
     * @param <DBObject>
     * @param map
     * @param collection
     * @throws Exception
     */
     public List<DBObject> find(String collection,Map<String, Object> map) throws Exception {
          DBCollection coll = getCollection(collection);
          DBCursor c = coll.find(map2Obj(map));
          if (c != null)
               return c.toArray();
          else
               return null;
     }
//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...