mongodb使用 java
mongodb使用1. 下载 mongo-2.6.3.jar
2. 新建 java项目
3. 按Spring方式封装查询 直接上代码
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 newObj) {return (Integer) dbTemp.execute(new MsgCallback() {public Object doExecute(DBCollection dbCollection) {return dbCollection.update(oldObj, newObj).getN();}}, collection);}public int delete(String collection, final BasicDBObject dbObj) {return (Integer) dbTemp.execute(new MsgCallback() {public Object doExecute(DBCollection dbCollection) {return dbCollection.remove(dbObj).getN();}}, collection);}public Object selectOne(String collection, final BasicDBObject dbObj) {return dbTemp.execute(new MsgCallback() {public Object doExecute(DBCollection dbCollection) {return dbCollection.findOne(dbObj);}}, collection);}@SuppressWarnings("unchecked")public Map<String, DBObject> selectMap(String collection,final BasicDBObject dbObj) {return ((DBObject) selectOne(collection, dbObj)).toMap();}@SuppressWarnings("unchecked")public List<DBObject> selectList(String collection,final BasicDBObject dbObj) {return (List<DBObject>) dbTemp.execute(new MsgCallback() {public Object doExecute(DBCollection dbCollection) {return dbCollection.find(dbObj).toArray();}}, collection);}}
package com.mytest;import java.util.ArrayList;import java.util.List;import com.mongodb.BasicDBObject;import com.mongodb.DBObject;import com.mongodb.util.JSON;/** ** 功能描述: ** @author lw * @created 2011-6-28 下午03:31:51 * @version 1.0.0 * @date 2011-6-28 下午03:31:51 */public class TestSQLDao extends SQLDao {/** * 功能描述: ** @param args */public static void main(String[] args) {TestSQLDao test = new TestSQLDao();BasicDBObject obj = new BasicDBObject();// obj.put("id", 6);obj.put("name", "pluto");// BasicDBObject newObj = new BasicDBObject("$set", new// BasicDBObject("name", "gogo"));List<DBObject> list = new ArrayList<DBObject>();list = test.selectList("students", obj);for (DBObject db : list) {System.out.println("-----------------------------------------------------");System.out.println(JSON.serialize(db));System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");System.out.println(db.toMap());System.out.println("-----------------------------------------------------");}}public int insert(String collection, BasicDBObject dbObj) {return super.insert(collection, dbObj);}public List<DBObject> selectList(String collection, BasicDBObject dbObj) {return super.selectList(collection, dbObj);}}
页:
[1]