自定义条件查询测试

 1     //自定义条件查询测试
 2     @Test
 3     public void testFindAllByExample() {
 4         //分页参数
 5         int page = 0;//从0开始
 6         int size = 10;
 7         /*
 8         //分页参数
 9         if (page <= 0) {
10             page = 1;
11         }
12         if (size <= 0) {
13             size = 10;
14         }
15         //构造分页查询方法
16         page = page - 1;
17         */
18         Pageable pageable = PageRequest.of(page, size);
19 
20         //条件值对象
21         CmsPage cmsPage = new CmsPage();
22         //要查询5a751fab6abb5044e0d19ea1站点的页面
23 //        cmsPage.setSiteId("5a751fab6abb5044e0d19ea1");
24         //设置模板id条件
25 //        cmsPage.setTemplateId("5ad9a24d68db5239b8fef199");
26         //设置页面别名
27         cmsPage.setPageAliase("轮播");
28         //条件匹配器
29         /*ExampleMatcher exampleMatcher = ExampleMatcher.matching();
30         exampleMatcher = exampleMatcher.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());
31 //        exampleMatcher = exampleMatcher.withMatcher("templateId", ExampleMatcher.GenericPropertyMatchers.contains());
32         */
33         ExampleMatcher exampleMatcher = ExampleMatcher.matching()
34                 .withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains())
35                 .withMatcher("templateId", ExampleMatcher.GenericPropertyMatchers.contains());
36                                                      //ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字(模糊匹配)
37                                                      //ExampleMatcher.GenericPropertyMatchers.startsWith() 前缀匹配
38                                                      //ExampleMatcher.GenericPropertyMatchers.exact() 处理状态精确匹配(默认)
39 
40         //定义条件对象Example
41         Example<CmsPage> example = Example.of(cmsPage, exampleMatcher);
42         Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);
43         List<CmsPage> content1 = all.getContent();//数据列表
44         long totalElements = all.getTotalElements();//数据总记录数
45 
46         List<CmsPage> content = all.getContent();
47         System.out.println(content);
48     }

猜你喜欢

转载自www.cnblogs.com/foshuo-cv/p/12036795.html