SpringBoot中Example的動態條件查詢


一、無匹配器的情況:
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);
//分頁
二、匹配器多條件組合(自己多動手試試各種各樣的條件組合)
        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?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM