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 ...
随机推荐
- CodeForces 825G"Tree Queries"(选根建树)
传送门 •参考资料 [1]:CodeForces 825G Educational Round #25 G :建树选根大法+O1大法+iostream解绑了还是慢 •题意 给定一颗包含 n 个节点的树 ...
- 【u128】又一个数字游戏
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小明拿出了一个素数集合,{2, 3, 5, 7, 11, 13, -, 127, -},他发现,从小到 ...
- tensorflow在文本处理中的使用——辅助函数
代码来源于:tensorflow机器学习实战指南(曾益强 译,2017年9月)——第七章:自然语言处理 代码地址:https://github.com/nfmcclure/tensorflow-coo ...
- background:url(./images.png) no-repeat 0 center的用法
background:url(./images.png) no-repeat 0 center; //图像地址 不重复 水平位置0 垂直位置居中 background:url(./images.png ...
- vue-learning:0 - 目录
Vue-learning vue.js学习路径 Vue的API地图 点击查看vue的API地图 视图层 点击可直接到达详情页面 指令 {{ }} / v-html v-if / v-else / v- ...
- HDU 6662 Acesrc and Travel (换根dp)
Problem Description Acesrc is a famous tourist at Nanjing University second to none. During this sum ...
- 2019牛客多校第一场 I Points Division(动态规划+线段树)
2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...
- int32 无符号范围 -2147483648~2147483647
int32 无符号范围 -2147483648~2147483647
- 在Android上为所欲为的一些技术
https://www.jianshu.com/p/87ce6f565d37Android JNI(一)——NDK与JNI基础 https://www.android-doc.com/guide/co ...
- Service Mesh服务网格清单
Service Mesh服务网格清单 Istio Istio官网 Istio中文官网 Istio开源 无需太多介绍Service Mesh明日之星,扛把子,截止2019.11还有太多问题没解决 复杂性 ...