因为版本号不同,可能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. 10.2 Hibernate持久层

    点击项目右键->MyEclipse->Add Hibernate Capabilities 打开MyEclipse Hibernate Perspective(MyEclipse Hibe ...

  2. 认识React框架

    在大厂面试的时候被问会不会React框架几乎是必须的,可见React框架在现在前端市场的份额.所以说学习React框架的必要性. react框架起源于Facebook的内部项目,因为对市场上的Java ...

  3. 错误信息:getOutputStream() has already been called for this response

    原因(转): getOutputStream()和getWriter()这两个方法不能在一个请求内同时使用, 如果使用forward,这时将要跳转到的页面是要用getWriter()方法获得输出流把页 ...

  4. Android Google 地图 API for Android

    从健康类 app Runkeeper 到游戏 app 精灵宝可梦,位置服务对现代 app 来说越来越重要. 在本文中,我们将创建一个 app,名字就叫做 City Guide.这个 app 允许用户搜 ...

  5. hdu2112 HDU Today 基础最短路

    这题的关键是把车站的名字转化为点的编号.我用的是map.声明一个map<string,int> st,然后按照字符串出现的次序给st赋值.例如:st[s1]=2;代表这字符串s1出现的次序 ...

  6. 模拟试题C

    模拟试题C 一.单项选择题(2′*14 =28′) 1.双线性法向插值法(Phong Shading)的优点是( ) A)法向计算精确 B)高光域准确 C)对光源和视点没有限制 D)速度较快 2.用编 ...

  7. 杭电2061WA 01

    #include<stdio.h> struct mem { char s[50]; double c; double f; }; int main() { struct mem x[60 ...

  8. ES6的let命令

    1.let命令所在的代码块内有效: 2.循环的计数器,就很合适使用let命令:(for循环还有一个特别之处,就是循环语句部分是一个父作用域,而循环体内部是一个单独的子作用域.) 例如:如下会输出十次s ...

  9. 团体程序设计天梯赛-练习集-L1-028. 判断素数

    L1-028. 判断素数 本题的目标很简单,就是判断一个给定的正整数是否素数. 输入格式: 输入在第一行给出一个正整数N(<=10),随后N行,每行给出一个小于231的需要判断的正整数. 输出格 ...

  10. HH的项链 树状数组动态维护前缀

    #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const ...