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?

转载于:https://www.cnblogs.com/zxg-6/p/10059224.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值