日期:2014-05-16 浏览次数:20626 次
package com.mytest; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * * * @author lw * @created 2011-6-27 下午04:26:40 * @version 1.0.0 * @date 2011-6-27 下午04:26:40 */ public class DBTemplate { private static String MONGODB_SERVER = "192.168.42.212"; private static int SERVER_PORT = 27017; private static String MONGODB_DBNAME = "test"; public final Object execute(MsgCallback action, String collection) { DB db = getConn(); DBCollection dbColl = db.getCollection(collection); Object result = action.doExecute(dbColl); closeDb(db); closeCollection(dbColl); return result; } private DB getConn() { return getConn(MONGODB_SERVER, SERVER_PORT, MONGODB_DBNAME); } private DB getConn(String server, int port, String dbName) { Mongo m = null; try { m = new Mongo(server, port); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } return m.getDB(dbName); } private void closeDb(DB db) { if (db != null) { db = null; } } private void closeCollection(DBCollection col) { if (col != null) { col = null; } } }
package com.mytest; import com.mongodb.DBCollection; /** * * 功能描述: * * @author lw * @created 2011-6-28 下午01:57:44 * @version 1.0.0 * @date 2011-6-28 下午01:57:44 */ public interface MsgCallback { Object doExecute(DBCollection dbCollection); }
package com.mytest; import java.util.List; import java.util.Map; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; /** * * 功能描述: * * @author lw * @created 2011-6-28 下午02:13:33 * @version 1.0.0 * @date 2011-6-28 下午02:13:33 */ public interface SQLTemplate { int insert(String collection, BasicDBObject dbObj); int update(String collection, BasicDBObject oldObj, BasicDBObject newObj); int delete(String collection, BasicDBObject dbObj); Object selectOne(String collection, BasicDBObject dbObj); Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj); List<DBObject> selectList(String collection, final BasicDBObject dbObj); }
package com.mytest; import java.util.List; import java.util.Map; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; /** * * 功能描述: * * @author lw * @created 2011-6-28 下午02:13:33 * @version 1.0.0 * @date 2011-6-28 下午02:13:33 */ public interface SQLTemplate { int insert(String collection, BasicDBObject dbObj); int update(String collection, BasicDBObject oldObj, BasicDBObject newObj); int delete(String collection, BasicDBObject dbObj); Object selectOne(String collection, BasicDBObject dbObj); Map<String, DBObject> selectMap(String collection, BasicDBObject dbObj); List<DBObject> selectList(String collection, final BasicDBObject dbObj); }
package com.mytest; import java.util.List; import java.util.Map; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBObject; /** * * 功能描述: * * @author lw * @created 2011-6-28 下午02:21:43 * @version 1.0.0 * @date 2011-6-28 下午02:21:43 */ public class SQLDao implements SQLTemplate { //可改成 Spring 注入 DBTemplate dbTemp = new DBTemplate(); public int insert(String collection, final BasicDBObject dbObj) { return (Integer) dbTemp.execute(new MsgCallback() { public Object doExecute(DBCollection dbCollection) { return dbCollection.insert(dbObj).getN(); } }, collection); } public int update(String collection, final BasicDBObject oldObj, final BasicDBObject