Springboot中根据分页查询

编程

public QueryResponseResult findList(int page, int size, QueryPageRequest queryPageRequest){

//定义条件匹配器

ExampleMatcher exampleMatcher = ExampleMatcher.matching()

.withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());

//条件值对象

CmsPage cmsPage = new CmsPage();

//设置条件值(站点id)

if(StringUtils.isNotEmpty(queryPageRequest.getSiteId())){

cmsPage.setSiteId(queryPageRequest.getSiteId());

}

//设置模板id作为查询条件

if(StringUtils.isNotEmpty(queryPageRequest.getTemplateId())){

cmsPage.setTemplateId(queryPageRequest.getTemplateId());

}

//设置页面别名作为查询条件

if(StringUtils.isNotEmpty(queryPageRequest.getPageAliase())){

cmsPage.setPageAliase(queryPageRequest.getPageAliase());

}

//定义条件对象Example

Example<CmsPage> example = Example.of(cmsPage,exampleMatcher);

//分页参数

if(page <=0){

page = 1;

}

page = page -1;

if(size<=0){

size = 10;

}

Pageable pageable = PageRequest.of(page,size);

Page<CmsPage> all = cmsPageRepository.findAll(example,pageable);//实现自定义条件查询并且分页查询

QueryResult queryResult = new QueryResult();

queryResult.setList(all.getContent());//数据列表

queryResult.setTotal(all.getTotalElements());//数据总记录数

QueryResponseResult queryResponseResult = new QueryResponseResult(CommonCode.SUCCESS,queryResult);

return queryResponseResult;

}

注意:需要判断页码和每页大小是否符合

以上是 Springboot中根据分页查询 的全部内容, 来源链接: utcz.com/z/513169.html

回到顶部