avatar

SpringBoot整合MongoDB

摘要:本文介绍了MongDB的安装以及在SpringBoot中如何使用

MongoDB的安装


这里使用docker来安装MongoDB,首先进入cmd,输入以下命令拉取镜像

1
docker pull mongo

将mongo的镜像拉取下来之后,输入以下命令启动mongo

1
docker run -d -p 27017:27017 --name mongo --restart:always -e MONGO_INITDB_ROOT_USERNAME=username -e MONGO_INITDB_ROOT_PASSWORD=password

启动mongo后就可以使用了,可以使用Robo 3T来连接mongo

在SpringBoot中使用MongoDB


添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

在application.properties中配置

1
2
3
4
5
6
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=wellenzheng
spring.data.mongodb.password=11215858
spring.data.mongodb.port=27017
spring.data.mongodb.database=JavaBoy

创建Book实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.example.mongodb.bean;

/**
* @author: WJZheng
* @date: 2020/3/25 17:55
* @description:
*/
public class Book {
private Integer id;
private String name;
private String author;

@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}
}

两种方式操纵MongoDB

MongoRepository<T, ID>

MongoRepository<T, ID>使用的是Spring Data的处理方式

创建BookDao接口

我们可以看到,dao接口只需要继承自MongoRepository<T, ID>即可,T是实体类,ID是实体类的主键。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.mongodb.dao;

import com.example.mongodb.bean.Book;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

/**
* @author: WJZheng
* @date: 2020/3/25 17:56
* @description:
*/
public interface BookDao extends MongoRepository<Book, Integer> {
List<Book> findBookByNameContaining(String s);
}

在Test中测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.mongodb;

import com.example.mongodb.bean.Book;
import com.example.mongodb.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.List;

@SpringBootTest
class MongodbApplicationTests {

@Autowired
BookDao bookDao;

@Test
void contextLoads() {
Book book = new Book();
book.setId(1);
book.setName("ThreeBody");
book.setAuthor("Liu Cixin");
bookDao.insert(book);
}

@Test
void test1(){
List<Book> bookList = bookDao.findAll();
System.out.println(bookList);
}

@Test
void test2(){
List<Book> bookList = bookDao.findBookByNameContaining("Three");
System.out.println(bookList);
}
}

MongoTemplate

MongoTemplate直接注入进来使用即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.mongodb;

import com.example.mongodb.bean.Book;
import com.example.mongodb.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.List;

@SpringBootTest
class MongodbApplicationTests {

@Autowired
MongoTemplate mongoTemplate;

@Test
void test3(){
Book book = new Book();
book.setId(2);
book.setName("BallLightning");
book.setAuthor("Liu Cixin");
mongoTemplate.insert(book);
}

@Test
void test4(){
List<Book> bookList = mongoTemplate.findAll(Book.class);
System.out.println(bookList);
}
}
Author: WJZheng
Link: https://wellenzheng.github.io/2020/03/27/SpringBoot%E6%95%B4%E5%90%88MongoDB/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Comment