一、添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.edu.tju</groupId>
    <artifactId>springbootmongo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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



    </dependencies>


</project>

二、定义实体类来映射mongodb中的collection

package cn.edu.tju.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "userInfo")
public class UserInfo {
    @Id
    private String id;
    private String username;
    private String password;

    public String getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

三、配置文件中配置mongodb

server.port=9006
#spring.data.mongodb.uri=mongodb://xx.xx.xx.xx:27017/test?maxPoolSize=512
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
spring.data.mongodb.username=root
spring.data.mongodb.password=pass
spring.data.mongodb.authenticationDatabase=admin
spring.data.mongodb.host=xx.xx.xx.xx
spring.data.mongodb.port=27017
spring.data.mongodb.database=test

四、注入自动配置的MongoOperations并使用

package cn.edu.tju.controller;

import cn.edu.tju.domain.UserInfo;
import cn.edu.tju.repository.UserInfoRepository;
import com.mongodb.MongoClientSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.ExecutableFindOperation;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import java.util.UUID;

import static org.springframework.data.mongodb.core.query.Criteria.where;

@RestController
public class DemoController {
/*    @Autowired
    private UserInfoRepository userInfoRepository;*/

    @Autowired
    private MongoDatabaseFactory mongo;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoClientSettings mongoClientSettings;

    @Autowired
    private MongoOperations operations;

    @RequestMapping("/hay")
    public String hay(){
        UserInfo userInfo = new UserInfo();
        userInfo.setId(UUID.randomUUID().toString());
        userInfo.setUsername("kean");
        userInfo.setPassword("passwd");

        operations.insert(userInfo);
        UserInfo result = operations.query(UserInfo.class)
                .matching(where("username").is("kean")).firstValue();
        return result.toString();
    }
}

这段代码创建了一个username为kean的文档,然后再通过username为kean这个条件去查询文档并返回给接口