springboot+mongodb的基本操作 CRUD-基于MongoRepository操作 mongodb

时间:2024-12-16 07:03:04

配置mongodb上一个文件有提现,这里就不写了。

Docker创建一个mongodb实例,并用springboot连接 mongodb进行读写文件-****博客

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>
</dependencies>
@Data
@Document(collection = "User")
public class User {
    @Id
    private String id;
    private String name;
    private Integer age;
    private String email;
    private String createDate;
}
@Repository
public interface UserRepository extends MongoRepository<User, String> {
}
@ActiveProfiles("test")
@SpringBootTest
public class MongoRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void createTest() {
        User user = new User();
        user.setAge(20);
        user.setName("张三");
        user.setEmail("123@qq.com");

        User user1 = userRepository.save(user);
        System.out.println(user1);
    }

    @Test
    public void findAllTest() {
        List<User> list = userRepository.findAll();
        System.out.println(list);
    }

    @Test
    public void findByIdTest() {
        Optional<User> optional = userRepository.findById("6195b9a5c6e3b8f8a0e8c6e3");
        optional.ifPresent(System.out::println);
    }

    @Test
    public void findConditionTest() {
        //name="张三" and age = 20
        User user = new User();
        user.setAge(20);
        user.setName("张三");
        Example<User> example = Example.of(user);

        userRepository.findAll(example);
    }

    @Test
    public void findLikeTest() {
        //设置模糊查询规则
        ExampleMatcher matcher = ExampleMatcher.matching();
        matcher.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        User user = new User();
        user.setAge(20);
        user.setName("m");
        Example<User> example = Example.of(user,matcher);

        userRepository.findAll(example);
    }

    @Test
    public void findPageTest(){
        int pageNo = 1;
        int pageSize = 2;
        Pageable pageable = PageRequest.of(pageNo, pageSize);

        User user = new User();
        user.setAge(20);
        user.setName("张三");
        Example<User> example = Example.of(user);

        Page<User> page = userRepository.findAll(example, pageable);
        System.out.println(page.getTotalElements());
    }

    @Test
    public void updateTest(){
        User user = userRepository.findById("6195b9a5c6e3b8f8a0e8c6e3").get();
        user.setName("李四");
        User user1 = userRepository.save(user);
        System.out.println(user1);
    }

    @Test
    public void removeTest(){
        userRepository.deleteById("6195b9a5c6e3b8f8a0e8c6e3");
    }

}