转自:http://blog.csdn.net/gaopeng0071/article/details/51281103
主要的调整在于2.10.0版本中引入了MongoClient类,同时在其API中也说明了Mongo类会在将来的版本中被MongoClient替换(Note: This class has been superseded by MongoClient, and may be deprecated in a future release.)。故在这次调整中,也对原先的MongoDB部分做了相应的修改。
MongoClient被设计成线程安全、可以被多线程共享的。通常访问数据库集群的应用只需要一个实例,所以这次调整我们设计成单例形式。如果出于某些原因,你决定使用多个实例,请注意:所有资源使用限制(最大连接数等等)对每个MongoClient都适用;销毁一个实例时,请确认调用MongoClient.close()方法来清理资源。
下面给出相关的代码:
/** * 项目名:SpiderCrawler * 文件名:MongoDBDao.java * 作者:zhouyh * 时间:2014-8-30 下午03:46:55 * 描述:TODO(用一句话描述该文件做什么) */ package com.newsTest.dao; import java.util.ArrayList; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; /** * 类名: MongoDBDao * 包名: com.newsTest.dao * 作者: zhouyh * 时间: 2014-8-30 下午03:46:55 * 描述: TODO(这里用一句话描述这个类的作用) */ public interface MongoDBDao { /** * * 方法名:getDb * 作者:zhouyh * 创建时间:2014-8-30 下午03:53:40 * 描述:获取指定的mongodb数据库 * @param dbName * @return */ public DB getDb(String dbName); /** * * 方法名:getCollection * 作者:zhouyh * 创建时间:2014-8-30 下午03:54:43 * 描述:获取指定mongodb数据库的collection集合 * @param dbName 数据库名 * @param collectionName 数据库集合 * @return */ public DBCollection getCollection(String dbName, String collectionName); /** * * 方法名:inSert * 作者:zhouyh * 创建时间:2014-8-30 下午04:07:35 * 描述:向指定的数据库中添加给定的keys和相应的values * @param dbName * @param collectionName * @param keys * @param values * @return */ public boolean inSert(String dbName, String collectionName, String[] keys, Object[] values); /** * * 方法名:delete * 作者:zhouyh * 创建时间:2014-8-30 下午04:09:00 * 描述:删除数据库dbName中,指定keys和相应values的值 * @param dbName * @param collectionName * @param keys * @param values * @return */ public boolean delete(String dbName, String collectionName, String[] keys, Object[] values); /** * * 方法名:find * 作者:zhouyh * 创建时间:2014-8-30 下午04:11:11 * 描述:从数据库dbName中查找指定keys和相应values的值 * @param dbName * @param collectionName * @param keys * @param values * @param num * @return */ public ArrayList<DBObject> find(String dbName, String collectionName, String[] keys, Object[] values, int num); /** * * 方法名:update * 作者:zhouyh * 创建时间:2014-8-30 下午04:17:54 * 描述:更新数据库dbName,用指定的newValue更新oldValue * @param dbName * @param collectionName * @param oldValue * @param newValue * @return */ public boolean update(String dbName, String collectionName, DBObject oldValue, DBObject newValue); /** * * 方法名:isExit * 作者:zhouyh * 创建时间:2014-8-30 下午04:19:21 * 描述:判断给定的keys和相应的values在指定的dbName的collectionName集合中是否存在 * @param dbName * @param collectionName * @param keys * @param values * @return */ public boolean isExit(String dbName, String collectionName, String key, Object value); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
/** * 项目名:SpiderCrawler * 文件名:MongoDBDaoImpl.java * 作者:zhouyh * 时间:2014-8-30 下午04:21:11 * 描述:TODO(用一句话描述该文件做什么) */package com.newsTest.dao.impl;import java.net.UnknownHostException;import java.util.ArrayList;import com.newsTest.dao.MongoDBDao;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.MongoClient;import com.mongodb.MongoClientOptions;import com.mongodb.MongoException;import com.mongodb.WriteResult;/** * 类名: MongoDBDaoImpl * 包名: com.newsTest.dao.impl * 作者: zhouyh * 时间: 2014-8-30 下午04:21:11 * 描述: TODO(这里用一句话描述这个类的作用) */public class MongoDBDaoImpl implements MongoDBDao{ /** * MongoClient的实例代表数据库连接池,是线程安全的,可以被多线程共享,客户端在多线程条件下仅维持一个实例即可 * Mongo是非线程安全的,目前mongodb API中已经建议用MongoClient替代Mongo */ private MongoClient mongoClient = null; /** * * 私有的构造函数 * 作者:zhouyh */ private MongoDBDaoImpl(){ if(mongoClient == null){ MongoClientOptions.Builder build = new MongoClientOptions.Builder(); build.connectionsPerHost(50); build.autoConnectRetry(true); build.threadsAllowedToBlockForConnectionMultiplier(50); build.maxWaitTime(1000*60*2); build.connectTimeout(1000*60*1); MongoClientOptions myOptions = build.build(); try { mongoClient = new MongoClient("127.0.0.1", myOptions); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e){ e.printStackTrace(); } } } /********单例模式声明开始,采用饿汉式方式生成,保证线程安全********************/ private static final MongoDBDaoImpl mongoDBDaoImpl = new MongoDBDaoImpl(); /** * * 方法名:getMongoDBDaoImplInstance * 作者:zhouyh * 创建时间:2014-8-30 下午04:29:26 * 描述:单例的静态工厂方法 * @return */ public static MongoDBDaoImpl getMongoDBDaoImplInstance(){ return mongoDBDaoImpl; } /************************单例模式声明结束*************************************/ @Override public boolean delete(String dbName, String collectionName, String[] keys, Object[] values) { DB db = null; DBCollection dbCollection = null; if(keys!=null && values!=null){ if(keys.length != values.length){ return false; }else{ try { db = mongoClient.getDB(dbName); dbCollection = db.getCollection(collectionName); BasicDBObject doc = new BasicDBObject(); WriteResult result = null; String resultString = null; for(int i=0; i<keys.length; i++){ doc.put(keys[i], values[i]); } result = dbCollection.remove(doc); resultString = result.getError(); if(null != db){ try { db.requestDone(); db = null; } catch (Exception e) { e.printStackTrace(); } } return (resultString!=null) ? false : true; } catch (Exception e) { e.printStackTrace(); } finally{ if(null != db){ db.requestDone(); db = null; } } } } return false; } @Override public ArrayList<DBObject> find(String dbName, String collectionName, String[] keys, Object[] values, int num) { ArrayList<DBObject> resultList = new ArrayList<DBObject>(); DB db = null; DBCollection dbCollection = null; DBCursor cursor = null; if(keys!=null && values!=null){ if(keys.length != values.length){ return resultList; }else{ try { db = mongoClient.getDB(dbName); dbCollection = db.getCollection(collectionName); BasicDBObject queryObj = new BasicDBObject(); for(int i=0; i<keys.length; i++){ queryObj.put(keys[i], values[i]); } cursor = dbCollection.find(queryObj); int count = 0; if(num != -1){ while(count<num && cursor.hasNext()){ resultList.add(cursor.next()); count++; } return resultList; }else{ while(cursor.hasNext()){ resultList.add(cursor.next()); } return resultList; } } catch (Exception e) { } finally{ if(null != cursor){ cursor.close(); } if(null != db){ db.requestDone(); } } } } return resultList; } @Override public DBCollection getCollection(String dbName, String collectionName) { return mongoClient.getDB(dbName).getCollection(collectionName); } @Override public DB getDb(String dbName) { return mongoClient.getDB(dbName); } @Override public boolean inSert(String dbName, String collectionName, String[] keys, Object[] values) { DB db = null; DBCollection dbCollection = null; WriteResult result = null; String resultString = null; if(keys!=null && values!=null){ if(keys.length != values.length){ return false; }else{ db = mongoClient.getDB(dbName); dbCollection = db.getCollection(collectionName); BasicDBObject insertObj = new BasicDBObject(); for(int i=0; i<keys.length; i++){ insertObj.put(keys[i], values[i]); } try { result = dbCollection.insert(insertObj); resultString = result.getError(); } catch (Exception e) { e.printStackTrace(); }finally{ if(null != db){ db.requestDone(); } } return (resultString != null) ? false : true; } } return false; } @Override public boolean isExit(String dbName, String collectionName, String key, Object value) { DB db = null; DBCollection dbCollection = null; if(key!=null && value!=null){ try { db = mongoClient.getDB(dbName); dbCollection = db.getCollection(collectionName); BasicDBObject obj = new BasicDBObject(); obj.put(key, value); if(dbCollection.count(obj) > 0) { return true; }else{ return false; } } catch (Exception e) { e.printStackTrace(); } finally{ if(null != db){ db.requestDone(); db = null; } } } return false; } @Override public boolean update(String dbName, String collectionName, DBObject oldValue, DBObject newValue) { DB db = null; DBCollection dbCollection = null; WriteResult result = null; String resultString = null; if(oldValue.equals(newValue)){ return true; }else{ try { db = mongoClient.getDB(dbName); dbCollection = db.getCollection(collectionName); result = dbCollection.update(oldValue, newValue); resultString = result.getError(); return (resultString!=null) ? false : true; } catch (Exception e) { e.printStackTrace(); } finally{ if(null != db){ db.requestDone(); db = null; } } } return false; } /** * 方法名:main * 作者:zhouyh * 创建时间:2014-8-30 下午04:21:11 * 描述:TODO(这里用一句话描述这个方法的作用) * @param args */ public static void main(String[] args) { }}
0 0