SpringDataJPA -07- example的基本使用

29 篇文章 3 订阅

目录


简单查询

@Test
public void testExample(){
    Customer customer=new Customer();
    customer.setCustName("飞飞飞");
    Example<Customer> example=Example.of(customer);

    List<Customer> customers = this.customerRepository.findAll(example);
    customers.forEach(value -> System.out.println(value));
}

使用ExampleMatcher

/**
     * 测试ExampleMatcher
     */
@Test
public void testExampleMatcher(){
    Customer customer=new Customer();
    customer.setCustName("记忆");
    customer.setCustIndustry("溜溜");
    customer.setCustPhone("5566");
    customer.setCustAddress("china");
    customer.setCustSource("aaa");

    ExampleMatcher exampleMatcher=ExampleMatcher.matching()
        .withMatcher("custName",ExampleMatcher.GenericPropertyMatchers.startsWith())   //模糊查询匹配开头
        .withMatcher("custIndustry",ExampleMatcher.GenericPropertyMatchers.contains()) //全部模糊查询
        .withIgnoreCase("custAddress")   //忽略大小写
        .withMatcher("custPhone",ExampleMatcher.GenericPropertyMatchers.exact())  //精准匹配
        .withIgnorePaths("custSource");  //忽略字段,即不管custSource是什么值都不加入查询条件


    Example<Customer> example=Example.of(customer,exampleMatcher);
    List<Customer> customers = this.customerRepository.findAll(example);
    customers.forEach(value-> System.out.println(value));
}

输出


lambda的方式

ExampleMatcher exampleMatcher2=ExampleMatcher.matching()
    .withMatcher("custName",match->match.startsWith())
    .withMatcher("custIndustry",match->match.contains())
    .withMatcher("custAddress",match->match.ignoreCase())
    .withMatcher("custPhone",match->match.exact())
    .withIgnorePaths("custSource");

该种方式和上面的ExampleMatcher效果是相同的


StringMatcher 参数

Matching生成的语句说明
DEFAULT (case-sensitive)firstname = ?0默认(大小写敏感)
DEFAULT (case-insensitive)LOWER(firstname) = LOWER(?0)默认(忽略大小写)
EXACT (case-sensitive)firstname = ?0精确匹配(大小写敏感)
EXACT (case-insensitive)LOWER(firstname) = LOWER(?0)精确匹配(忽略大小写)
STARTING (case-sensitive)firstname like ?0 + ‘%’前缀匹配(大小写敏感)
STARTING (case-insensitive)LOWER(firstname) like LOWER(?0) + ‘%’前缀匹配(忽略大小写)
ENDING (case-sensitive)firstname like ‘%’ + ?0后缀匹配(大小写敏感)
ENDING (case-insensitive)LOWER(firstname) like ‘%’ + LOWER(?0)后缀匹配(忽略大小写)
CONTAINING (case-sensitive)firstname like ‘%’ + ?0 + ‘%’模糊查询(大小写敏感)
CONTAINING (case-insensitive)LOWER(firstname) like ‘%’ + LOWER(?0) + ‘%’模糊查询(忽略大小写)

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值