mongodb与java结合_MongoDB初探系列之四:MongoDB与Java共舞

MongoDB初探系列之四:MongoDB与Java共舞

来源:互联网

作者:佚名

时间:2015-08-05 08:20

对各位注意到这个帖子的朋友说一声对不起,我不是故意的测试服务器一直没关,一忙就给忘了删了.....如果有管理员看到,可以把这个阅读数什么的清零,谢谢

MongoDB初探系列之四:MongoDB与Java共舞

分类:

MongoDB初探

由于版本不同,可能API也有所不同,,本次学习用的是3.0版本。

1、使用的mongodb的jdbc驱动版本为:mongo-java-driver-3.0.0.jar

2、本节只是简单介绍JDBC操作,暂时不考虑效率问题。

3、封装的工具类代码如下:

public class MongoDBProxy {

private static MongoDBProxy proxy=null;//单实例

private static MongoDatabase db=null;//数据库连接对象

private static String [] paramArray=new String[5];//数据库连接参数

private MongoDBProxy(){

}

static{

paramArray[0]="username";

paramArray[1]="password";

paramArray[2]="host";

paramArray[3]="port";

paramArray[4]="databaseName";

}

/**

* 得到MongoDBProxy

* 采用系统默认配置

*/

public static MongoDBProxy getMongoDBProxy(){

if(proxy==null){

proxy=new MongoDBProxy();

String sURI = String.format("mongodb://%s:%s@%s:%d/%s",paramArray[0],paramArray[1],paramArray[2],Integer.parseInt(paramArray[3]),paramArray[4]);

MongoClientURI uri = new MongoClientURI(sURI);

MongoClient mongoClient = new MongoClient(uri);

db= mongoClient.getDatabase(paramArray[4]);

}

return proxy;

}

/**

* 批量查询数据

* @param table 集合名称

* @param page 分页参数

* @param filter 过滤条件

* @param sort 排序条件

*/

public Page findDocList(String table,Page page,Bson filter,Bson sort){

MongoCollection coll=db.getCollection(table);

long count=coll.count(filter);//根据过滤条件获取数据总量

Page p=PageUtil.createPage(page,Integer.parseInt(String.valueOf(count)));

p.setFromUrl((page.getFromUrl()==null)?"":page.getFromUrl());

p.setParamString((page.getParamString()==null)?"":page.getParamString());

FindIterable resultIterable=coll.find();

//执行条件过滤

resultIterable=resultIterable.sort(sort).filter(filter).skip(p.getBeginIndex()).batchSize(p.getEveryPage());

MongoCursor cousor=resultIterable.iterator();

List dataList=new ArrayList();

while(cousor.hasNext()){

dataList.add(cousor.next());

}

p.setDataList(dataList);

return PageUtil.buildPageString(p);

}

/**

* 获取单个文档

* @param table 集合名称

* @param filter 过滤条件

* @param sort 排序条件

*/

public Document findOneDoc(String table,Bson filter,Bson sort){

MongoCollection coll=db.getCollection(table);

FindIterable resultIterable=coll.find();

if(sort!=null){

resultIterable.sort(sort);

}

if(filter!=null){

resultIterable.filter(filter);

}

return resultIterable.first();

}

/**

* 添加文档

* @param table 集合名称

* @prama doc 文档内容

*/

public void addDocument(String table,Document doc){

MongoCollection coll=getCollection(table);

coll.insertOne(doc);

}

/**

* 批量添加文档

* @param table

集合名称

* @prama docList 文档集合

*/

public void addDocumentList(String table,List docList){

MongoCollection coll=getCollection(table);

coll.insertMany(docList);

}

/**

* 更新文档

* @param table 集合名称

* @param query 查询条件

* @param up

更新数据

*/

public UpdateResult updateDocument(String table,Bson query,Bson up){

MongoCollection coll=getCollection(table);

return coll.updateOne(query,up);

}

/**

* 替换文档

* @param table 集合名称

* @param query 查询条件

* @param up

替换的文件对象

*/

public UpdateResult replaceDocument(String table,Bson query,Document up){

MongoCollection coll=getCollection(table);

return coll.replaceOne(query, up);

}

/**

* 删除文档

* @param table 集合名称

* @param delete 删除条件

*/

public DeleteResult deleteDocument(String table,Bson delete){

MongoCollection coll=getCollection(table);

return coll.deleteOne(delete);

}

/**

* 获取集合对象

* @param table 集合名称

*/

private MongoCollection getCollection(String table){

return db.getCollection(table);

}

}

4、调用demo

MongoDBProxy proxy=MongoDBProxy.getMongoDBProxy();

System.out.println(proxy.findOneDoc("users",null,null).get("_id"));

Document doc=new Document();

doc.put("user","李四");

proxy.addDocument("users", doc);

Bson bson=new BasicDBObject("user","张三");

proxy.deleteDocument("users", bson);

后续再深入学习,先用demo上上手哇,哈哈。

版权声明:转载请注明博文地址,尊重作者劳动成果。欢迎关注,一起成长。

上一篇MongoDB初探系列之三:MongoDB用户权限操作

0

0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值