Spring Data MongoDB 模糊查询+精确查询

    xiaoxiao2022-07-04  112

    实体类Person @Document @Data @NoArgsConstructor @AllArgsConstructor public class Person { @Id private String id; private String name; private String nickName; } StudentRepository public interface PersonRepository extends MongoRepository<Person, String> { } 按照姓名精确查询,按照昵称模糊查询 private Page<Student> query(Pageable pageable){ Student student = new Student(); student.setName("张三"); student.setNickName("xx"); Example<ChargeEmployeeRead> example = Example.of(student, generateStringContainingAndNullIgnoreMatcher()); return studentRepository.findAll(example, pageable); } private ExampleMatcher generateStringContainingAndNullIgnoreMatcher() { return ExampleMatcher.matching() .withMatcher("name", match->match.exact()) // 姓名精确查询 .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) // 其他为模糊查询 .withIgnoreNullValues(); } 对于pageable参数,只要正确传参,Spring MVC会自动组装,Controller方法只需要申明pageable参数即可,传参格式:?page=0&size=10&sort=id,asc
    最新回复(0)