因为版本号不同,可能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. [HTML] 如何使用robots.txt防止搜索引擎抓取页面

    Robots.txt 文件对抓取网络的搜索引擎漫游器(称为漫游器)进行限制.这些漫游器是自动的,在它们访问网页前会查看是否存在限制其访问特定网页的 robots.txt 文件.如果你想保护网站上的某些 ...

  2. 实体类中方法名尽量避免set,get,报错com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException)

    自己建了一个实体类 public class MissPoint implements Serializable{ private static final long serialVersionUID ...

  3. SwiftUI 官方教程(五)

    SwiftUI官方教程(五) 5. 同时使用 UIKit 和 SwiftUI 至此,我们已准备好创建 map view 了,接下来使用 MapKit 中的 MKMapView 类来渲染地图. 在 Sw ...

  4. 开发vue插件并发布到npm包管理工具的流程

    1-10是开发流程,后面的是发布流程 1. 在Git里面…新建项目   2. 克隆项目到本地用来开发 git clone https://github.com/***/vue-prevent-brow ...

  5. Entity Framework 的懒加载、预先加载、显示加载

    1.新建两个实体,一个班级有多个学生 public class Student { public int StudentId { get; set; } public string StudentNa ...

  6. TensorFlow-LSTM序列预测

    问题情境:已知某一天内到目前为止股票各个时刻的价格,预测接下来短时间内的价格变化. import tushare as ts import time from collections import n ...

  7. C++文本操作.Vs.Python

    C++利用文件流: (1):读取一个字符 std::string TestTxt(argv[3]); // freopen(TestTxt.c_str(),"r",stdin);/ ...

  8. mvvm模式和mvc模式 概述总结对比

    1.mvc模式简介: MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范.例如: angular ...

  9. MySQL安装部署

    MySQL安装部署 使用自动化脚本

  10. Shoot the Bullet ZOJ - 3229有上下界网络流

    Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...