第一步:在Repository继承JpaSpecificationExecutor接口,PagingAndSortingRepository接口
例如:
第二步:实现service分页业务
@Override
public Map<String, Object> findPagebyConn(SystemsForm form) throws Exception {
Pageable pageable = new PageRequest(form.getPageNumber(), form.getPageSize(), Sort.Direction.DESC, "createDate");
Page<Systems> page = systemRepository.findAll(new Specification<Systems>(){
@Override
public Predicate toPredicate(Root<Systems> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = new ArrayList<>();
if(StringUtils.isNotBlank(form.getName())){
Predicate p1 = criteriaBuilder.like(root.get("name").as(String.class), "%"+(form.getName()!=null?form.getName():"")+"%");
predicates.add(p1);
}
if(form.getIsEnabled()>0){
Predicate p2 = criteriaBuilder.equal(root.get("isEnabled").as(int.class), form.getIsEnabled());
predicates.add(p2);
}
Predicate p3 = criteriaBuilder.equal(root.get("isdel").as(String.class), "0");
predicates.add(p3);
Predicate[] pre = new Predicate[predicates.size()];
query.where(predicates.toArray(pre));
return query.getRestriction();
}
},pageable);
return PageUtils.getPageMap(page);
}
第三步:封装PageUtils
public class PageUtils {
public static Map<String, Object> getPageMap(Page<?> objPage) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("resultList", objPage.getContent()); // 数据集合
resultMap.put("totalNum", objPage.getTotalElements()); // 总记录数
resultMap.put("totalPage", objPage.getTotalPages()); // 总页数
resultMap.put("pageNum", objPage.getNumber()); // 当前页码
resultMap.put("pageSize", objPage.getSize()); // 每页显示数量
return resultMap;
}
}
第四步:controller 调用
@RequestMapping(value="/list",method=RequestMethod.GET)
public String findPage(SystemsForm form,Model model){
try {
Map<String, Object> pages = systemsService.findPagebyConn(form);
model.addAttribute("pages", pages);
model.addAttribute("form", form);
} catch (Exception e) {
// TODO Auto-generated catch block
logger.info(e.getMessage());
e.printStackTrace();
}
return "system/list";
“`
页面分页使用的是Kkpager分页插件
例如
如果你有什么不了解的,尽可以提问,我尽可以回到你