因为版本号不同,可能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<Document> 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<Document> resultIterable=coll.find();
//运行条件过滤
resultIterable=resultIterable.sort(sort).filter(filter).skip(p.getBeginIndex()).batchSize(p.getEveryPage());
MongoCursor<Document> cousor=resultIterable.iterator();
List<Document> dataList=new ArrayList<Document>();
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<Document> coll=db.getCollection(table);
FindIterable<Document> 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<Document> coll=getCollection(table);
coll.insertOne(doc);
}
/**
* 批量加入文档
* @param table 集合名称
* @prama docList 文档集合
*/
public void addDocumentList(String table,List<Document> docList){
MongoCollection<Document> coll=getCollection(table);
coll.insertMany(docList);
}
/**
* 更新文档
* @param table 集合名称
* @param query 查询条件
* @param up 更新数据
*/
public UpdateResult updateDocument(String table,Bson query,Bson up){
MongoCollection<Document> coll=getCollection(table);
return coll.updateOne(query,up);
}
/**
* 替换文档
* @param table 集合名称
* @param query 查询条件
* @param up 替换的文件对象
*/
public UpdateResult replaceDocument(String table,Bson query,Document up){
MongoCollection<Document> coll=getCollection(table);
return coll.replaceOne(query, up);
}
/**
* 删除文档
* @param table 集合名称
* @param delete 删除条件
*/
public DeleteResult deleteDocument(String table,Bson delete){
MongoCollection<Document> coll=getCollection(table);
return coll.deleteOne(delete);
}
/**
* 获取集合对象
* @param table 集合名称
*/
private MongoCollection<Document> 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与Java共舞的更多相关文章

  1. MongoDB初探系列之二:认识MongoDB提供的一些经常使用工具

    在初探一中,我们已经能够顺利的将MongoDB在我们自己的机器上跑起来了. 可是在其bin文件夹以下另一些我们不熟知的工具.接下来,将介绍一下各个小工具的用途以及初探一中MongoDB在data文件夹 ...

  2. MongoDb进阶实践之四 MongoDB查询命令详述

    一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...

  3. MongoDb进阶实践之六 MongoDB查询命令详述(补充)

    一.引言         上一篇文章我们已经介绍了MongoDB数据库的查询操作,但是并没有介绍全,随着自己的学习的深入,对查询又有了新的东西,决定补充进来.如果大家想看上一篇有关MongoDB查询的 ...

  4. java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作

    官方api教程:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started ...

  5. mongodb基础系列——数据库查询数据返回前台JSP(一)

    经过一段时间停顿,终于提笔来重新整理mongodb基础系列博客了. 同时也很抱歉,由于各种原因,没有及时整理出,今天做了一个demo,来演示,mongodb数据库查询的数据在JSP显示问题. 做了一个 ...

  6. MongoDB学习笔记-2(使用java连接Mongo)

    本人使用maven创建的java工程,mongo-java-driver使用的是3.2.2,这个驱动2.x和3.x是有差异的 pom.xml配置加入: <dependency> <g ...

  7. MongoDb进阶实践之八 MongoDB的聚合初探

    一.引言 好久没有写东西了,MongoDB系列的文章也丢下好长时间了.今天终于有时间了,就写了一篇有关聚合的文章.一说到“聚合”,用过关系型数据库的人都应该知道它是一个什么东西.关系型数据库有“聚合” ...

  8. MongoDB学习之(二)java连接

    上一章完了下mongodb的安装和IDE工具,现在开始使用java进行连接. 第一步:使用jar包, 这里需要三个包,具体为啥我也不清楚,反正因为报错,我就按照官方文档一个个的都下载了. 链接:htt ...

  9. MongoDB干货系列2-MongoDB执行计划分析详解(2)(转载)

    写在之前的话 作为近年最为火热的文档型数据库,MongoDB受到了越来越多人的关注,但是由于国内的MongoDB相关技术分享屈指可数,不少朋友向我抱怨无从下手. <MongoDB干货系列> ...

随机推荐

  1. HBase编程 API入门系列之modify(管理端而言)(10)

    这里,我带领大家,学习更高级的,因为,在开发中,尽量不能去服务器上修改表. 所以,在管理端来修改HBase表.采用线程池的方式(也是生产开发里首推的) package zhouls.bigdata.H ...

  2. [] == ![]为什么是true

    我们先来考虑这个问题,console.log([] == false)会打印什么呢? 答案是true.为什么呢? 首先,因为当"=="号两边其中一个是布尔值的话,先把它转换为数字( ...

  3. 移动端web开发初探之Vuejs的简单实战

    这段时间在做的东西,是北邮人论坛APP的注册页.这个注册页是内嵌的网页,因为打算安卓和IOS平台同时使用.因此实际上就是在做移动端的web开发了. 在这过程中遇到了不少有意思的东西. DEMO的git ...

  4. json属性(Jackson)

    Jackson相关:使用Jackson相关的注解时一定要注意自己定义的属性命名是否规范. 命名不规范时会失去效果.(例如Ename ,Eage 为不规范命名.“nameE”,“ageE”为规范命名). ...

  5. 常用的CSS命名

    头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体佈局宽度:wrapper 左右中:left rig ...

  6. CV/PR:模式识别与图像处理笔试题

    汉王高级研究人员(模式识别.图像处理类)招聘试题  原文链接:http://www.cnblogs.com/dongsheng/articles/2793142.html 说明:  可能您的专业并不完 ...

  7. MD5加盐,实现一人一密

    理论上md5是不可逆的,而且MD5本来也不是作加密使用,而是用来校验数据的完整性,只是因为其不可逆且稳定.快速的特点,被广泛用于对明文密码的加密. 至今仍然后很多开发人员相信MD5的保密性,也许因为他 ...

  8. Here comes Treble: A modular base for Android

    On the Android team, we view each dessert release as an opportunity to make Android better for our u ...

  9. Linux 命令查询系统负载信息

    linux uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令过去只显示系统运行多久.现在,可以显示系统已经运行了多长 时间,信息显示依次为:现在时间.系统已经运 ...

  10. css 里面怎么改链接颜色

    a.color1:link{color: #FFFFFF ; text-decoration:none;} /*常规时候的样式*/a.color1:visited{color: #FFFFFF; te ...