SpringDataJPA -07- example的基本使用

    技术2026-02-02  8

    目录

    ORMHibernate,JPA和SpringDataJpaJPA入门案列JPA的API介绍getOne和findOne的比较JPQL的基本使用specification的基本使用example的基本使用 ★ 多表设计 表之间的划分 分析步骤 多表设计(一对多) 多表设计(一对多) 对象导航查询

    简单查询

    @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) + ‘%’模糊查询(忽略大小写)
    Processed: 0.034, SQL: 9