• SpringBoot整合Mongodb


     

    目录

    1 创建模块

    2 配置文件

    3 使用MongoTemplate进行CRUD


    1 创建模块

    选nosql:spring data mangodb

    或手动导入依赖

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

    2 配置文件

    1. spring:
    2. data:
    3. mongodb:
    4. # 最后一个ssm_db是数据库的名字
    5. uri: mongodb://localhost/ssm_db


    3 使用MongoTemplate进行CRUD

     

    1. package com.qing;
    2. import com.domain.Book;
    3. import org.junit.jupiter.api.Test;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. import org.springframework.data.mongodb.core.MongoTemplate;
    7. import org.springframework.data.mongodb.core.query.Criteria;
    8. import org.springframework.data.mongodb.core.query.Query;
    9. import org.springframework.data.mongodb.core.query.Update;
    10. import org.springframework.data.mongodb.gridfs.GridFsCriteria;
    11. import java.util.List;
    12. @SpringBootTest
    13. class SpringbootMongoApplicationTests {
    14. @Autowired
    15. private MongoTemplate mongoTemplate;
    16. //增
    17. @Test
    18. void testSave() {
    19. Book book = new Book(2,"springboot学习手册", "工具书", "不会就翻书");
    20. mongoTemplate.save(book);
    21. }
    22. //查所有
    23. @Test
    24. void testFind() {
    25. List<Book> books = mongoTemplate.findAll(Book.class);
    26. for (Book book:books) {
    27. System.out.println(book);
    28. }
    29. }
    30. //条件查询
    31. @Test
    32. void testFindBy() {
    33. Book book = new Book();
    34. book.setId(1);
    35. Query query = new Query(Criteria.where("id").is(1)
    36. .and("name").is("springboot学习手册"));
    37. List<Book> books = mongoTemplate.find(query,Book.class);
    38. for (Book book1:books) {
    39. System.out.println(book1);
    40. }
    41. }
    42. //删
    43. @Test
    44. void testRemove() {
    45. Query query = new Query(Criteria.where("id").is(2));
    46. mongoTemplate.remove(query,Book.class);
    47. }
    48. //改
    49. @Test
    50. void testUpdate() {
    51. Query query = new Query(Criteria.where("name").is("springboot学习手册"));
    52. Update update = new Update();
    53. update.set("name","万能学习手册");
    54. update.set("type","手册");
    55. mongoTemplate.updateMulti(query,update,Book.class);
    56. }
    57. }

     最后附上

    1. package com.domain;
    2. import lombok.Data;
    3. @Data
    4. public class Book {
    5. private Integer id;
    6. private String name;
    7. private String type;
    8. private String description;
    9. public Book() {
    10. }
    11. public Book(Integer id, String name, String type, String description) {
    12. this.id = id;
    13. this.name = name;
    14. this.type = type;
    15. this.description = description;
    16. }
    17. public Book(String name, String type, String description) {
    18. this.name = name;
    19. this.type = type;
    20. this.description = description;
    21. }
    22. }

     

  • 相关阅读:
    36、Flink 的 WindowAssigner之滑动窗口示例
    centos 安装rabbitmq
    JVM——二、内存结构
    pg数据表同步到hive表数据压缩总结
    快手二面:你有没有调用过第三方接口?碰到过哪些坑?
    如何应对数仓资源不足导致的核心任务延迟
    awk编辑器
    手机网络连接性能API接口:查询手机网络连接性能状态
    React(二):Redux基本使用方法
    中央设备状态监控系统CMS如何帮助半导体晶圆厂提高产品良率
  • 原文地址:https://blog.csdn.net/m0_45877477/article/details/125520637