当前位置:首页 > 开发 > 编程语言 > Spring > 正文

spring data jpa 分页

发表于: 2014-08-28   作者:endual   来源:转载   浏览:
摘要: public Page<Task> getUserTask(Long userId, Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) { PageRequest pageRequest = buildPageRequest(pageNumber, pag
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 分页

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号