MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写,旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
1、mongodb服务
mongodb的安装支持windows和linux等平台,具体在不同平台的下载安装请查看相关大佬博文。
2、创建springboot项目
此处通过idea工具创建,springboot选择2.7.0版本,具体不再描述。
3、添加mongodb依赖
Spring Data MongDB 是Spring Data的下的一个模块,在springboot中整合mongodb就需要添加Spring Data MongDB的依赖,添加Spring Data MongDB依赖的方式有两种:
创建springboot项目时勾选Spring Data MongoDB
在pom.xml文件中添加Spring Data MongoDB依赖
1
2
3
4
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-mongodb</ artifactId > </ dependency > |
4、application.yml配置
spring:
data:
mongodb:
uri: mongodb://127.0.0.1:27017/mongodb_test
或
spring:
data:
mongodb:
host: 127.0.0.1 #指定MongoDB服务地址
port: 27017 #指定端口,默认就为27017
database: mongodb #指定使用的数据库(集合)
authentication-database: admin # 登录认证的逻辑库名
username: admin #用户名
password: abc123456 #密码
mongodb数据库与mysql不一样 mysql 一个普通用户可以管理多个数据库,但是mongo每一个库都有一个独立的管理用户,连接时需要输入对应用户密码。
5、实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Data @Document (collection = "article" ) //指定要对应的文档名(表名) @Accessors (chain = true ) //链式访问 public class Article { /** * 文章主键 */ @Id private String id; /** * 文章名 */ private String name; /** * 文章内容 */ private String content; } |
1
2
3
4
5
6
7
8
9
|
@Data @Document (collection = "book" ) public class Book { @Id private String id; private String name; private String type; private String description; } |
其中注解含义如下:
- @Document,文档是 MongoDB 中最基本的数据单元,由键值对组成,类似于 JSON 格式,可以存储不同字段,字段的值可以包括其他文档,数组和文档数组;
- @Id(主键):用来将成员变量的值映射为文档的_id的值;
- @Indexed(索引): 索引是一种特殊的数据结构,存储在一个易于遍历读取的数据集合中,能够对数据库文档中的数据进行排序。索引能极大提高文档查询效率,如果没有设置索引,MongoDB 会遍历集合中的整个文档,选取符合查询条件的文档记录。这种查询效率是非常低的;
- @Field(字段): 文档中的字段,类似于 MySql 中的列;
- @Aggregation(聚合): 聚合主要用于数据处理,例如统计平均值、求和等;
6、通过MongoRepository实现增删改查
持久层继承MongoRepository
1
2
3
4
5
6
7
8
|
public interface ArticleRepository extends MongoRepository<Article,String> { /** * 根据id查询文章 * @param id * @return */ List<Article> findByid(String id); } |
注意,接口需要起名findBy + 主键名,否则会报找不到属性的错误。
业务层
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
|
@Service @RequiredArgsConstructor public class ArticleService { private final ArticleRepository articleRepository; /** * 添加文章 * * @param article * @return */ public int create(Article article) { Article save = articleRepository.save(article); return 1 ; } /** * 删除文章 * * @param ids */ public int delete(List<String> ids) { List<Article> deleteList = new ArrayList<>(); for (String id:ids){ Article article = new Article(); article.setId(id); deleteList.add(article); } articleRepository.deleteAll(deleteList); return ids.size(); } /** * 查询文章 * @param id * @return */ public List<Article> get(String id) { return articleRepository.findByid(id); } } |
控制层
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
|
@RestController @RequestMapping ( "/article" ) @RequiredArgsConstructor public class ArticleController { private final ArticleService articleService; @PostMapping ( "/create" ) public String create( @RequestBody Article article) { int result = articleService.create(article); if (result > 0 ) { return "文章创建成功" ; } else { return "文章创建失败" ; } } @PostMapping ( "/delete" ) public String delete( @RequestParam ( "ids" ) List<String> ids) { int count = articleService.delete(ids); if (count > 0 ) { return "删除了" + count + "篇文章" ; } else { return "删除文章失败" ; } } @PostMapping ( "/get" ) public List<Article> get(String id) { List<Article> list = articleService.get(id); return list; } } |
7、通过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
33
34
35
36
|
@Service @RequiredArgsConstructor public class BookService { private final MongoTemplate mongoTemplate; /** * 添加文章 * * @param book * @return */ public int create(Book book) { Book save = mongoTemplate.save(book); return 1 ; } /** * 删除文章 * * @param id */ public int delete(String id) { List<Book> deleteList = new ArrayList<>(); Query query = new Query(); query.addCriteria(Criteria.where( "id" ).is(id)); mongoTemplate.remove(query,Book. class ); return 1 ; } /** * 查询文章 * @param id * @return */ public Book get(String id) { Book byId = mongoTemplate.findById(id, Book. class ); return byId; } } |
控制层
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
|
@RestController @RequestMapping ( "/book" ) @RequiredArgsConstructor public class BookController { private final BookService bookService; @PostMapping ( "/create" ) public String create( @RequestBody Book book) { int result = bookService.create(book); if (result > 0 ) { return "图书创建成功" ; } else { return "图书创建失败" ; } } @PostMapping ( "/delete" ) public String delete( @RequestParam ( "id" ) String id) { int count = bookService.delete(id); if (count > 0 ) { return "删除了" + count + "篇图书" ; } else { return "删除图书失败" ; } } @PostMapping ( "/get" ) public Book get(String id) { Book book = bookService.get(id); return book; } } |
注意:通过mongoTemplate查询时,实体类的id字段类型需为String类型,否则查询为null。
8、测试
通过postman工具测试
创建文章和图书,分布对应两种方式
数据库中记录如下
查询文章和图书
删除文章和图书
到此这篇关于SpringBoot分布式文件存储数据库mongod的文章就介绍到这了,更多相关SpringBoot mongod内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/leijie0322/article/details/128830017