前言

MongoDB 1 是可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库。基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种。

正文

Spring Boot 对 MongoDB 的数据源操作进行了封装。

加入依赖

在 pom.xml 加入:

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

配置连接参数

在系统配置文件中配置:

spring:
data:
mongodb:
uri: mongodb://wuwii:123456@localhost:27017/learn

测试使用

  1. 创建实体
@Data
@Document(collection = "pet") // 标识要持久化到MongoDB的域对象。模型名是 pet
public class Pet implements Serializable {
@Id
//@Indexed(unique = true) // 使用MongoDB的索引特性标记一个字段
private Long id;
@Field("pet_name") //自定义设置对应MongoDB中的key
private String name;
private String species;
}
  1. 创建 dao 接口完成基础操作
@Repository
public class PetDaoImpl implements PetDao {
@Autowired
private MongoTemplate mongoTemplate; @Override
public Pet find(Long id) {
return mongoTemplate.findById(id, Pet.class);
} @Override
public List<Pet> findAll() {
return mongoTemplate.findAll(Pet.class);
} @Override
public void add(Pet pet) {
mongoTemplate.insert(pet);
} @Override
public void update(Pet pet) {
Query query = new Query();
Criteria criteria = new Criteria("id");
criteria.is(pet.getId());
query.addCriteria(criteria);
Update update = new Update();
update.set("pet_name", pet.getName())
.set("species", pet.getSpecies());
mongoTemplate.updateFirst(query, update, Pet.class); // 条件,更新的数据,更新的类型
} @Override
public void delete(Long id) {
Criteria criteria = new Criteria("id");
criteria.is(id);
Query query = new Query();
query.addCriteria(criteria);
mongoTemplate.remove(query, Pet.class); // 删除的条件、删除的类型
}
}
  1. 简单测试下
@SpringBootTest
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PetDaoTest { @Autowired
private PetDao petDao; private Pet pet; @Before
public void before() {
pet = new Pet();
pet.setId(1L);
pet.setName("Tom");
pet.setSpecies("cat");
} @After
public void after() {
} @Test
public void test01Add() {
Pet pet = new Pet();
pet.setId(1L);
pet.setName("Tom");
pet.setSpecies("cat");
petDao.add(pet);
} @Test
public void test02Find() {
Assert.assertThat(pet, Matchers.equalTo(petDao.find(pet.getId())));
} @Test
public void test03FindAll() {
System.out.println(petDao.findAll());
} @Test
public void test04Update() {
pet.setName("KronChan");
petDao.update(pet);
Assert.assertThat(pet, Matchers.equalTo(petDao.find(pet.getId())));
} @Test
public void test05Delete() {
petDao.delete(pet.getId());
Assert.assertThat(null, Matchers.equalTo(petDao.find(pet.getId())));
} }

去数据库验证结果

> use learn
switched to db learn
> db.pet.find()
{ "_id" : NumberLong(1), "_class" : "com.wuwii.testmongodb.Pet", "pet_name" : "KronChan", "species" : "cat" }

多数据源的使用

未完成


  1. 来自于英文单词“Humongous”,中文含义为“庞大”

学习Spring Boot:(二十)使用 MongoDB的更多相关文章

  1. Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控

    Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...

  2. spring boot(二十)使用spring-boot-admin对服务进行监控

    上一篇文章<springboot(十九):使用Spring Boot Actuator监控应用>介绍了Spring Boot Actuator的使用,Spring Boot Actuato ...

  3. (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控

    http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...

  4. 学习Spring Boot:(二十五)使用 Redis 实现数据缓存

    前言 由于 Ehcache 存在于单个 java 程序的进程中,无法满足多个程序分布式的情况,需要将多个服务器的缓存集中起来进行管理,需要一个缓存的寄存器,这里使用的是 Redis. 正文 当应用程序 ...

  5. 学习Spring Boot:(二十六)使用 RabbitMQ 消息队列

    前言 前面学习了 RabbitMQ 基础,现在主要记录下学习 Spring Boot 整合 RabbitMQ ,调用它的 API ,以及中间使用的相关功能的记录. 相关的可以去我的博客/RabbitM ...

  6. spring boot / cloud (十二) 异常统一处理进阶

    spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...

  7. 学习 Spring Boot 知识看这一篇就够了

    从2016年因为工作原因开始研究 Spring Boot ,先后写了很多关于 Spring Boot 的文章,发表在技术社区.我的博客和我的公号内.粗略的统计了一下总共的文章加起来大概有六十多篇了,其 ...

  8. spring boot / cloud (十六) 分布式ID生成服务

    spring boot / cloud (十六) 分布式ID生成服务 在几乎所有的分布式系统或者采用了分库/分表设计的系统中,几乎都会需要生成数据的唯一标识ID的需求, 常规做法,是使用数据库中的自动 ...

  9. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  10. Linux学习之CentOS(二十六)--Linux磁盘管理:LVM逻辑卷的创建及使用

    在上一篇随笔里面 Linux学习之CentOS(二十五)--Linux磁盘管理:LVM逻辑卷基本概念及LVM的工作原理,详细的讲解了Linux的动态磁盘管理LVM逻辑卷的基本概念以及LVM的工作原理, ...

随机推荐

  1. Bitcoin 使用及配置记录

    常用配置 bitcoin-qt.exe -testnet -printtoconsole -conf=D:\Bitcoin\bitcoin.conf -datadir=D:\Bitcoin\Data ...

  2. BZOJ 4804: 欧拉心算

    数论题不多BB,直接开始推导吧: \(\sum_{i=1}^n \sum_{j=1}^n \phi(gcd(i,j))\) \(=\sum_{i=1}^n \sum_{j=1}^n \sum_{d=1 ...

  3. GNU构建系统和AutoTools

    注:本篇博客是阅读文末[参考博客]的讲解所写,内容非原创,仅是学习笔记 1. 概述2. 不同视角的程序构建2.1 用户视角2.2 开发者视角3. 导图图片4. configure选项参考博客 1. 概 ...

  4. ssh实现办公室电脑连接家中的电脑

    友情提示:如果您不知道您家路由器管理页面的密码,请您忽略此文. 问题背景: 家中有台笔记本电脑,它是通过家中的路由器与外界联网的,这时,我想通过ssh服务让公司的电脑能连上我家中的笔记本. 可以画个图 ...

  5. Crackme006 - 全新160个CrackMe学习系列(图文|视频|注册机源码)

    知乎:逆向驿站 原文链接 CrackMe006 | 难度适中适合练手 |160个CrackMe深度解析(图文+视频+注册机源码) crackme006,依然是delphi的,而且没壳子,条线比较清晰, ...

  6. nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理

    Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案.nginx可以用轮询.IP哈希.URL哈希等方式调度后端服务器,同时也能提供健康检查功能 ...

  7. rrd文件及rrd文件与实际数据的对比研究。

    一,什么是rrd文件? 所 谓的“Round Robin” 其实是一种存储数据的方式,使用固定大小的空间来存储数据,并有一个指针指向最新的数据的位置.我们可以把用于存储数据的数据库的空间看成一个圆,上 ...

  8. Beta阶段爬取数目预估

    预计于12月29号能进行Beta版本发布. Beta阶段我们的爬取动作应该更有针对性,在爬取期间如若数据处理小组有需求,会优先爬取数据处理小组提供的种子链接.预估在项目展示之前能够爬取的数目: 普通网 ...

  9. Linux内核分析(第七周)

    可执行程序的装载 一.预处理.编译.链接和目标文件的格式 1.可执行程序怎么来的? 预处理: gcc -E -o hello.cpp hello.c -m32 *负责把include的文件包含进来及宏 ...

  10. 第八章Jdk代理 cglib代理

    什么是代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能. 这 ...