モンゴルドbは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);
}
}