public Page<Task> getUserTask(Long userId, Map<String, Object> searchParams, int pageNumber, int pageSize,
String sortType) {
PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
Specification<Task> spec = buildSpecification(userId, searchParams);
return taskDao.findAll(spec, pageRequest);
}
/**
* 创建分页请求.
*/
private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
Sort sort = null;
if ("auto".equals(sortType)) {
sort = new Sort(Direction.DESC, "id");
} else if ("title".equals(sortType)) {
sort = new Sort(Direction.ASC, "title");
}
return new PageRequest(pageNumber - 1, pagzSize, sort);
}
/**
* 创建动态查询条件组合.
*/
private Specification<Task> buildSpecification(Long userId, Map<String, Object> searchParams) {
Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
filters.put("user.id", new SearchFilter("user.id", Operator.EQ, userId));
Specification<Task> spec = DynamicSpecifications.bySearchFilter(filters.values(), Task.class);
return spec;
}
spring data jpa 分页
原创
©著作权归作者所有:来自51CTO博客作者mb64216b23e45d0的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:搜集Java工具
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章