Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1015630
  • 博文数量: 146
  • 博客积分: 3444
  • 博客等级: 中校
  • 技术积分: 1602
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-21 15:18
文章分类

全部博文(146)

文章存档

2014年(9)

2013年(3)

2012年(6)

2011年(44)

2010年(38)

2009年(46)

分类: Java

2014-03-03 21:04:10

类java下一个简单的mongodb的helper类,参考了下面地址的写法,做了一些修改,调整了全局变量定义,修改了init方法,增加了save方法。
http://blog.csdn.net/phinecos/article/details/8444515

点击(此处)折叠或打开

  1. package cls;

  2. import java.net.UnknownHostException;
  3. import java.util.Map;
  4. import java.util.concurrent.ConcurrentHashMap;

  5. import org.slf4j.LoggerFactory;
  6. import org.slf4j.Logger;

  7. import com.mongodb.DB;
  8. import com.mongodb.DBCollection;
  9. import com.mongodb.DBCursor;
  10. import com.mongodb.DBObject;
  11. import com.mongodb.Mongo;
  12. import com.mongodb.WriteResult;

  13. public class MongoDbHelper {
  14.     protected static Logger logger = LoggerFactory.getLogger("MongoDbHelper");
  15.     private Mongo myMongo = null;
  16.     private DB myDb = null;
  17.     private static final String serverAddr="127.0.0.1";
  18.     private static final int port=27017;
  19.     private static final String myDbName="test";
  20.     private static final String userName="admin";
  21.     private static final String passWord="123456";
  22.     private static Map<String, DBCollection> dbCollectionMap = new ConcurrentHashMap<String, DBCollection>();
  23.     
  24.     public MongoDbHelper(){
  25.         this.init();     
  26.     }

  27.      private void init() {
  28.      if (this.myMongo == null) {
  29.      try {
  30.      this.myMongo = new Mongo( MongoDbHelper.serverAddr , MongoDbHelper.port);
  31.      if (null != this.myMongo) {
  32.      this.myDb = this.myMongo.getDB(MongoDbHelper.myDbName);
  33.      //如果需要用户认证则先切换到admin数据库进行用户认证后,再切回用户数据库;
  34.      if(userName!=""){
  35.          this.myDb=this.myMongo.getDB("admin");
  36.          this.myDb.authenticate(userName, passWord.toCharArray());
  37.          this.myDb=this.myMongo.getDB(MongoDbHelper.myDbName);
  38.      }
  39.      }
  40.      } catch (UnknownHostException e) {
  41.      logger.error("连接mongoDb失败, 服务器地址: " + MongoDbHelper.serverAddr + ", 端口: " + MongoDbHelper.port);
  42.      throw new RuntimeException(e);
  43.      }
  44.      }
  45.      }
  46.      private DBCollection getDBCollection(String collectionName) {
  47.      DBCollection collection = null;
  48.      if (dbCollectionMap.containsKey(collectionName)) {
  49.      collection = dbCollectionMap.get(collectionName);
  50.      } else {
  51.      collection = this.myDb.getCollection(collectionName);
  52.      if (null != collection) {
  53.      dbCollectionMap.put(collectionName, collection);
  54.      }
  55.      }
  56.      return collection;
  57.      }
  58.     
  59.      public boolean isDocumentExsit(String collectionName, DBObject query) {
  60.      boolean result = false;
  61.      DBCursor dbCursor = null;
  62.      DBCollection collection = this.getDBCollection(collectionName);
  63.      if (null != collection) {
  64.      dbCursor = collection.find(query);
  65.      if (null != dbCursor && dbCursor.hasNext()) {
  66.      result = true;
  67.      }
  68.      }
  69.      return result;
  70.      }
  71.     
  72.      public DBCursor selectDocument(String collectionName, DBObject query) {
  73.          DBCursor result = null;
  74.      DBCursor dbCursor = null;
  75.      DBCollection collection = this.getDBCollection(collectionName);
  76.      if (null != collection) {
  77.      dbCursor = collection.find(query);
  78.      if (null != dbCursor && dbCursor.hasNext()) {
  79.      result = dbCursor;
  80.      }
  81.      }
  82.      return result;
  83.      }
  84.     
  85.      public void insertDocument(String collectionName, DBObject newDocument) {
  86.      DBCollection collection = this.getDBCollection(collectionName);
  87.      if (null != collection) {
  88.      if (!this.isDocumentExsit(collectionName, newDocument)) {//insert only doc not exist
  89.      collection.insert(newDocument);
  90.      }
  91.      }
  92.      }
  93.     
  94.      public boolean updateDocument(String collectionName, DBObject query, DBObject updatedDocument) {
  95.      boolean result = false;
  96.      WriteResult writeResult = null;
  97.      DBCollection collection = this.getDBCollection(collectionName);
  98.      if (null != collection) {
  99.      writeResult = collection.update(query, updatedDocument);
  100.      if (null != writeResult) {
  101.      if (writeResult.getN() > 0) {
  102.      result = true;
  103.      }
  104.      }
  105.      }
  106.      return result;
  107.      }
  108.         //以下代码为测试mongodb的save功能
  109.         /*
  110.         DBObject upObject =new BasicDBObject("_id", new ObjectId("52fdbe36e97fee8601000000"));
  111.         upObject.put("enterpriseName","cccc");
  112.         myMongoDb.saveDoc("testColl", upObject);*/
  113.         //============================================
  114.      public boolean saveDoc(String collectionName,DBObject saveObject){
  115.          boolean result=false;
  116.          WriteResult writeResult = null;
  117.      DBCollection collection = this.getDBCollection(collectionName);
  118.      if (null != collection) {
  119.      writeResult = collection.save(saveObject);
  120.      if (null != writeResult) {
  121.      if (writeResult.getN() > 0) {
  122.      result = true;
  123.      }
  124.      }
  125.      }
  126.      return result;
  127.      }
  128.     
  129.      public boolean deleteDocument(String collectionName, DBObject query) {
  130.      boolean result = false;
  131.      WriteResult writeResult = null;
  132.      DBCollection collection = this.getDBCollection(collectionName);
  133.      if (null != collection) {
  134.      writeResult = collection.remove(query);
  135.      if (null != writeResult) {
  136.      if (writeResult.getN() > 0) {
  137.      result = true;
  138.      }
  139.      }
  140.      }
  141.      return result;
  142.      }
  143.     
  144.     
  145. }

阅读(3668) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~