/** * Created by Weck */ public class PageableToolsUtil { /** * 获取基础分页对象 * @param page 获取第几页 * @param size 每页条数 * @param dtos 排序对象数组 * @return */ public static Pageable basicPage(Integer page, Integer size, SortDto... dtos) { Sort sort = SortToolsUtil.basicSort(dtos); page = (page==null || page<0)?0:page; size = (size==null || size<=0)?10:size; Pageable pageable = new PageRequest(page, size, sort); return pageable; } /** * 获取基础分页对象,每页条数默认15条 * - 默认以id降序排序 * @param page 获取第几页 * @return */ public static Pageable basicPage(Integer page) { return basicPage(page, 0, new SortDto("desc", "id")); } /** * 获取基础分页对象,每页条数默认15条 * @param page 获取第几页 * @param dtos 排序对象数组 * @return */ public static Pageable basicPage(Integer page, SortDto... dtos) { return basicPage(page, 0, dtos); } /** * 获取基础分页对象,排序方式默认降序 * @param page 获取第几页 * @param size 每页条数 * @param orderField 排序字段 * @return */ public static Pageable basicPage(Integer page, Integer size, String orderField) { return basicPage(page, size, new SortDto("desc", orderField)); } /** * 获取基础分页对象 * - 每页条数默认15条 * - 排序方式默认降序 * @param page 获取第几页 * @param orderField 排序字段 * @return */ public static Pageable basicPage(Integer page, String orderField) { return basicPage(page, 15, new SortDto("desc", orderField)); }
}
//排序封装类 public class SortToolsUtil { public static Sort basicSort() { return basicSort("desc", "id"); } public static Sort basicSort(String orderType, String orderField) { Sort sort = new Sort(Sort.Direction.fromString(orderType), orderField); return sort; } public static Sort basicSort(SortDto... dtos) { Sort result = null; for(int i=0; i<dtos.length; i++) { SortDto dto = dtos[i]; if(result == null) { result = new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()); } else { result = result.and(new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField())); } } return result; } }
//引用
/** *新闻中心(咨询、政策)分页查询 * @param page 页数 * @return */ @RequestMapping(value="/XXXXXX",method = RequestMethod.GET) @ResponseBody public String findNewslist(HttpServletResponse response, @RequestParam(value = "page", defaultValue = "0") Integer page,@RequestParam(value = "parentId", defaultValue = "1") Integer parentId){ response.setHeader("Access-Control-Allow-Origin", "*"); Page<News> all = newsRepo.findAll((root, query, cb) -> { List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.notEqual(root.get("parentId").as(Integer.class), 0)); predicates.add(cb.equal(root.get("newsStatus").as(Integer.class), 1)); predicates.add(cb.equal(root.get("parentId").as(Integer.class), parentId)); return cb.and(predicates.toArray(new Predicate[0])); }, PageableToolsUtil.basicPage(page, defaultSize, new SortDto("createDate"))); List<News> allCount = newsRepo.findAll((root, query, cb) -> { List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.notEqual(root.get("parentId").as(Integer.class), 0)); predicates.add(cb.equal(root.get("newsStatus").as(Integer.class), 1)); predicates.add(cb.equal(root.get("parentId").as(Integer.class), parentId)); return cb.and(predicates.toArray(new Predicate[0])); }); return ReturnDataDO.returnSuccessJson("分页查询成功成功",new CommonPageJsonDto(allCount.size(),all)); }