mongodb使用 java

[size=large]
mongodb使用
1. 下载 mongo-2.6.3.jar
2. 新建 java项目
3. 按Spring方式封装查询 直接上代码
[/size]


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);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值