//自定义条件查询
@Test
public void test05(){
int page = 0;
int size = 5;
Pageable pageable = PageRequest.of(page, size);
//条件值对象(定义一个查询实体,给相对应的属性赋值表示要查询的条件, 对象属性为空时代表全部查询)
CmsPage cmsPage = new CmsPage();
// cmsPage.setPageId("5a754adf6abb500ad05688d9"); //要查询 PageId 为 5ada939168db524a909d30a8 的页面
// cmsPage.setSiteId("5a751fab6abb5044e0d19ea1");
// cmsPage.setTemplateId("5a962bf8b00ffc514038fafa");
cmsPage.setPageAliase("轮播");
//表示创建一个空的条件匹配器(初始化)(进行模糊查询或等其它查询配置)
ExampleMatcher exampleMatcher = ExampleMatcher.matching()
//创建匹配条件, 加入匹配条件的对应实体属性和查询规则(查询pageAliase属性包含×××的数据)
.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());
// ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字
// ExampleMatcher.GenericPropertyMatchers.startsWith() 前缀匹配
// ExampleMatcher.GenericPropertyMatchers.ignoreCase() 忽略大小写
//定义查询条件 Example<T> 是JPA中装载查询条件的实体类
Example<CmsPage> example = Example.of(cmsPage, exampleMatcher); //第一个参数是查询实体的查询条件, 第二个参数是条件匹配器(模糊查询等)
Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);
System.out.println(all.getContent());
}
1、配合ExampleMatcher(查询方法)Example(查询条件)和 Pageable(分页)可方便多条件查询
2、
对于非字符串属性的只能精确匹配,比如想查询在某个时间段内注册的用户信息,就不能通过Example来查询