步骤①:导入springboot整合MongoDB的starter坐标

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

    步骤②:进行基础配置

    spring:
      data:
        mongodb:
          uri: mongodb://localhost/nthink
    

    操作MongoDB需要的配置与操作redis一样,最基本的信息都是操作哪一台服务器,区别就是连接的服务器IP地址和端口不同,书写格式不同而已。
    步骤③:使用springboot整合MongoDB的专用客户端接口MongoTemplate来进行操作

    @SpringBootTest
    class Springboot17MongodbApplicationTests {
        @Autowired
        private MongoTemplate mongoTemplate;
        @Test
        void contextLoads() {
            Book book = new Book();
            book.setId(2);
            book.setName("springboot2");
            book.setType("springboot2");
            book.setDescription("springboot2");
            mongoTemplate.save(book);
        }
        @Test
        void find(){
            List<Book> all = mongoTemplate.findAll(Book.class);
            System.out.println(all);
        }
    }