springboot2 整合mongodb
在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的更多相关文章
- springboot 学习之路 14(整合mongodb的Api操作)
springboot整合mongodb: mongodb的安装和权限配置 请点击连接参考 mongodb集成 : 第一步:引如pom文件 第二步:配置文件配置mongodb路径: 第三步:关于mon ...
- 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代码 ...
- MongoDB系列:四、spring整合mongodb,带用户验证
在前面的两篇博客 MongoDB常用操作练习.springboot整合mongoDB的简单demo中,我们基本上熟悉了mongodb,也把它与spring boot进行了整合并且简单使用.在本篇博客中 ...
- java操作mongodb & springboot整合mongodb
简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...
- SpringBoot整合mongoDB
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. 这一片文章介绍一个springboot整合mongodb,如果你了解整合mysql之类的 ...
- SpringMVC整合Mongodb开发,高级操作
开发环境: 操作系统:windows xpMongodb:2.0.6依 赖 包:Spring3.2.2 + spring-data-mongodb-1.3.0 + Spring-data-1.5 + ...
- spring整合mongodb
使用spring整合mongodb maven 依赖 <dependency> <groupId>org.mongodb</groupId> <artifac ...
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/ 本文出自方志朋的博客 这篇文 ...
- SpringMVC整合MongoDB
首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...
随机推荐
- Vue导航栏在特定的页面不显示~
最近写vue项目遇到一些问题,我把导航栏组件放在了app.vue中,让他在每个页面都能显示了,但遇到了一个问题,在登录以及注册页面导航栏是不合理不允许存在的 解决方法: 公共模块的内容可以放在App. ...
- 分析JVM动态生成的类
总结思考:让jvm创建动态类及其实例对象,需要给它提供哪些信息? 三个方面: 1.生成的类中有哪些方法,通过让其实现哪些接口的方式进行告知: 2.产生的类字节码必须有个一个关联的类加载器对象: 3.生 ...
- POJ 1236 Network of Schools(tarjan)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
- 【踩坑记录】vue单个组件内<style lang="stylus" type="text/stylus" scoped>部分渲染失效
vue组件化应用,近期写的单个组件里有一个的渲染部分样式渲染不上去 因为同结构的其他组件均没有问题,所以排除是.vue文件结构的问题,应该是<style>内部的问题 <style l ...
- 2018-8-10-win10-uwp-MVVM-语义耦合
title author date CreateTime categories win10 uwp MVVM 语义耦合 lindexi 2018-08-10 19:16:53 +0800 2018-2 ...
- 2019-2-27-win10-uwp-去掉-Flyout-边框
title author date CreateTime categories win10 uwp 去掉 Flyout 边框 lindexi 2019-02-27 17:48:46 +0800 201 ...
- Gym - 101102D Rectangles (单调栈)
Given an R×C grid with each cell containing an integer, find the number of subrectangles in this gri ...
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
- AbstractRoutingDataSource动态数据源切换
操作数据一般都是在DAO层进行处理,可以选择直接使用JDBC进行编程(http://blog.csdn.net/yanzi1225627/article/details/26950615/) 或者是使 ...
- ELK学习实验003:Elasticsearch 集群安装
前面已经介绍了Elasticsearch这个工具,并对单机安装做了简单介绍,现在使用三台机器做一个elasticsearch集群 一 环境准备 1.1 机器准备 1.2 同步时间 [root@node ...