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);
}
}
分享到:
相关推荐
MongoDB实现增删改查,java程序驱动,复杂查询,源代码示例
mongodb for java api 英文版
mongodb-java-driver-4.4.0.jar
MongoDB之Java使用例子,详细的增删改查例子,以及图片保存删除查询等~
MongoDB for Java Developers Design, build, and deliver efficient Java applications using the most advanced NoSQL database
MongoDB Java操作大全 源代码 实例
mongodb_java_driver 已经验证可用!
MongoDB应用 Java应用 纯java mongodb操作 mongodb 通用dao
这是一个简单的例子,说明如何为 mongoDB 使用 java 驱动程序。 从http://www.mongodb.org/ : MongoDB 是一个开源文档数据库,也是领先的 NoSQL 数据库。 用 C++ 编写。 如果您没有任何工具箱来操作数据库,您有...
MongoDB的Java驱动包 。3.8版本,MongoDB官网https://www.mongodb.com/
MongoDB、Java与对象关系映射 MongoDB、Java与对象关系映射
亲测可用,解压包含三个jar包,引用时sources和doc包根据需要添加。 mongo-java-driver-3.5.0.jar; mongo-java-driver-3.5.0-javadoc.jar; mongo-java-driver-3.5.0-sources.jar;
java中mongodb使用环境详细配置,主要是在Windows上(WinXP)安装、运行、安装Windows服务的笔记,以作备忘。
MongoDBjava各版本驱动下载
使用java 操作mongoDB必须的jar包,
MongoDB-Java 博客演示###Prerequisties 要构建 MongoDB-Java 博客应用程序,您需要具备以下条件: JDK 1.7(不推荐使用 1.8) MavenSpark-java 自由标记###下载项目 git clone ...建造要构建 MongoDB-Java 演示应用...
mongodb 3.4.2 java包
这篇文章就是展示如何使用Java基于MongoDB和Spring Data创建一个CRUD应用。 Spring Data for MongoDB提供了一个类似于基于Sping编程模型的NoSQL数据存储。Spring Data for MongoDB提供了很多特性,它使很多MongoDB的...
此文档专门针对于使用Java语言操作MongoDB数据库的开发人员。
Mongodb 数据库 JAVA 增删改查操作例子