SpringBoot中Example的動態條件查詢

1、無匹配器的狀況:
Person person = new Person();
person.setName("test");
Role role = new Role();
role.setName("經理");
person.setRole(role);
... 
Example<Person> ex = Example.of(person); //動態查詢 
personRepository.findAll(ex);
personRepository.findAll(ex,pageable);
//分頁
2、匹配器多條件組合(本身多動手試試各類各樣的條件組合)
        Person person = new Person();
        person.setName(name);
        //默認匹配器:字符串採用精準查詢,忽略大小寫(文檔說不忽略大小寫,本人測試時發現是忽略大小寫的)
        ExampleMatcher matcher = ExampleMatcher.matching()
//                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)  //改變默認字符串匹配爲:模糊查詢
//                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())  //name字段模糊匹配
//                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.startsWith())  //name字段開頭模糊匹配
//                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.endsWith())  //name字段結尾模糊匹配
//                .withIgnorePaths("id","phone"); //忽略id,phone字段
        Example<Person> ex = Example.of(person,matcher); //動態查詢
        return personRepository.findAll(ex);
        retuen personRepository.fiadAll(ex,pageable)  //分頁
總結:   (Person) 一對一,多對一能夠經過set另一個實體(Role)的字段來實現動態查詢,可是(Person)一對多經過Example來動態查詢,本人試過不行(JpaSpecificationExecutor能夠),大家又沒有idea?
相關文章
相關標籤/搜索