在springboot2中使用MongoDB

1、引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>

2、写入yml文件

spring:
data:
mongodb:
uri: mongodb://user:pwd@localhost:27017/database

注意,下划线部分,根据本机设置,自己调整,如果没有设置权限,去掉黄色部分即可。

还有需要注意的是,涉及到权限时,要不就是使用uri的方式指定路径,要不就使用port、host、username等指定,不可混用。

3、上代码(我觉着定义成静态方法更合适,先不调整了,这个可以用)

@Service
public class MongoUtils { @Autowired
private MongoTemplate mongoTemplate; public void insertOne(String collection, Document document) {
mongoTemplate.getCollection(collection).insertOne(document);
} public void insertMany(String collection, List<Document> documentList) {
mongoTemplate.getCollection(collection).insertMany(documentList);
} public Document findById(String collection, String id) {
Document query = new Document();
query.put("_id", id);
return findOne(collection, query);
} public Document findOne(String collection, Bson filter) {
return findMany(collection, filter).get(0);
} public List<Document> findAll(String collection) {
return findMany(collection, null);
} public List<Document> findMany(String collection, Bson filter) {
filter = handelId(filter);
MongoCollection<Document> mongoCollection = mongoTemplate.getCollection(collection);
FindIterable<Document> findIterable = mongoCollection.find();
MongoCursor<Document> mongoCursor = findIterable.filter(filter).iterator();
List<Document> documentList = new ArrayList<Document>();
while (mongoCursor.hasNext()) {
Document document = mongoCursor.next();
documentList.add(document);
}
return documentList;
} public void updateOne(String collection, Document document) {
Document query = new Document();
query.put("_id", new ObjectId(document.get("_id").toString()));
document.remove("_id");
Document updateDoc = new Document();
updateDoc.put("$set", document);
mongoTemplate.getCollection(collection).updateOne(query, updateDoc);
} private Document handelId(Bson bson) {
if (bson != null) {
Document document = (Document) bson;
Object id = null;
if (document.containsKey("id")) {
id = document.get("id");
document.remove("id");
} else if (document.containsKey("_id")) {
id = document.get("_id");
}
if (id != null) {
ObjectId objId = new ObjectId(id.toString());
document.put("_id", objId);
}
return document;
}
return new Document();
}
}

4、测试类(用法参考)

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional    // 注释后回滚
class MongoDBTest {
@Autowired
private MongoUtils mongoUtils; @BeforeEach
void setUp() {
} @AfterEach
void tearDown() {
} @Test
void insertOne() {
Document doc = new Document();
doc.put("name", "world");
doc.put("age", 16);
mongoUtils.insertOne("collection", doc);
} @Test
void insertMany() {
List<Document> documentList = new ArrayList<Document>();
Document doc = new Document();
doc.put("name", "sam");
doc.put("age", 30);
documentList.add(doc);
doc = new Document();
doc.put("name", "nicole");
doc.put("age", 30);
documentList.add(doc);
mongoUtils.insertMany("collection", documentList);
} @Test
void findById() {
System.out.println(mongoUtils.findById("collection", "5db7a5e66957f95f2a7459a6"));
} @Test
void updateOne() {
Document doc = mongoUtils.findById("collection", "5db7a5e66957f95f2a7459a6");
doc.replace("name", "Jully");
mongoUtils.updateOne("collection", doc);
} @Test
void findOne() {
Document doc = new Document();
doc.put("name", "hello");
System.out.println(mongoUtils.findOne("collection", doc));
} @Test
void findMany() {
Document doc = new Document();
doc.put("id", "5db7a5e66957f95f2a7459a6");
System.out.println(mongoUtils.findMany("collection", doc));
} @Test
void findAll() {
System.out.println(mongoUtils.findAll("collection"));
}
}

如果有问题,欢迎反馈。

springboot2 整合mongodb的更多相关文章

  1. springboot 学习之路 14(整合mongodb的Api操作)

    springboot整合mongodb: mongodb的安装和权限配置  请点击连接参考 mongodb集成 : 第一步:引如pom文件 第二步:配置文件配置mongodb路径: 第三步:关于mon ...

  2. spring MVC 整合mongodb

    Spring Mongodb 目录 1 SPRING整合MONGODB 1 1.1 环境准备 1 1.2 包依赖 1 1.3 配置 2 2 案列 5 2.1 SPRING MVC整合MONGODB代码 ...

  3. MongoDB系列:四、spring整合mongodb,带用户验证

    在前面的两篇博客 MongoDB常用操作练习.springboot整合mongoDB的简单demo中,我们基本上熟悉了mongodb,也把它与spring boot进行了整合并且简单使用.在本篇博客中 ...

  4. java操作mongodb & springboot整合mongodb

    简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...

  5. SpringBoot整合mongoDB

    MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 这一片文章介绍一个springboot整合mongodb,如果你了解整合mysql之类的 ...

  6. SpringMVC整合Mongodb开发,高级操作

    开发环境: 操作系统:windows xpMongodb:2.0.6依 赖 包:Spring3.2.2 + spring-data-mongodb-1.3.0 + Spring-data-1.5 +  ...

  7. spring整合mongodb

    使用spring整合mongodb maven 依赖 <dependency> <groupId>org.mongodb</groupId> <artifac ...

  8. SpringBoot非官方教程 | 第八篇:springboot整合mongodb

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/ 本文出自方志朋的博客 这篇文 ...

  9. SpringMVC整合MongoDB

    首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...

随机推荐

  1. border写一个直角三角形

    文章地址 https://www.cnblogs.com/sandraryan/ border的四条边是平分的.你可以放大试试 .box1 { width:;; border: 100px solid ...

  2. P1112 区间连续段

    题目描述 给你一个长度为n的数组 \(a_1, a_2, \dots a_n\) . 一开始这n个元素都为0. 然后有m次操作,每次操作有两种操作类型: update x y a:将区间 \([x,y ...

  3. [转]在eclipse中,用maven创建一个web项目工程

    1.在eclipse中用maven创建项目,右键new>>Maven Project 2.点击next继续 3.点击next继续,选择maven-archetype-webapp, 4.点 ...

  4. Django入门9--Django shell

  5. java 代理的概念与作用

    1.引入: 为已存在的多个具有相同接口的目标类的各个方法增加一些系统功能,例如,异常处理.日志.计算方法的运行时间.事务管理.等等,你准备如何做? 编写一个与目标类具有相同接口的代理类,代理类的每个方 ...

  6. 纯CSS绘制的图形一览

    整理网上一些使用纯CSS绘制的图形示例~~纯属抄袭,哈哈...仅仅是为了自己以后查看! Square(正方形) #square { width: 100px; height: 100px; backg ...

  7. NuGet 如何设置图标

    在找 NuGet 的时候可以看到有趣的库都有有趣的图标,那么如何设置一个 NuGet 的图标 在开始之前,请在nuget官方网站下载 NuGet.exe 同时设置环境变量 环境变量设置的方法就是将 N ...

  8. JWT之登录、登出、验证码接口

    6.2 验证码接口 验证码接口用于登录页面展示时,获取验证码图片地址及验证码标识 安装验证码功能组件(如果是官网下载的完整版框架,无需安装) composer require topthink/thi ...

  9. dotnet 通过 HttpClient 下载文件同时报告进度的方法

    本文告诉大家一个简单的方法通过 HttpClient 下载文件,同时报告下载进度 通过 HttpClient 的 ContentLength 很多时候都可以拿到下载的内容的长度,通过 ReadAsyn ...

  10. HDU - 5015 233 Matrix (矩阵快速幂)

    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...