分页结果集封装
public class PageResult<T> {
private long total;//总条数
private Integer totalPage;//总页数
private List<T> list;
public PageResult() {
}
public PageResult(long total, List<T> list) {
this.total = total;
this.list = list;
}
public PageResult(long total, Integer totalPage, List<T> list) {
this.total = total;
this.totalPage = totalPage;
this.list = list;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
@RequestMapping(value = "/list",method = RequestMethod.POST)
public Result getBrandList(@RequestBody Map<String,String> map ){
String search = map.get("search");
String pageNum = map.get("pageNum");
String pageSize = map.get("pageSize");
if (StringUtils.isEmpty(pageNum)){
pageNum="1";
}
if (StringUtils.isEmpty(pageSize)){
pageSize="5";
}
PageResult<Brand> list=brandService.getBrandList(search,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
return ResultUtil.success(list);
}
PageResult<Brand> getBrandList(String search, Integer pageNum, Integer pageSize);
@Autowired
private BrandDao brandDao;
@Override
public PageResult<Brand> getBrandList(String search, Integer pageNum, Integer pageSize) {
Specification<Brand> specification=new Specification<Brand>() {
//select * from tb_brand where name like? limit 0,10;
@Override
public Predicate toPredicate(Root<Brand> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Path<Object> name = root.get("name");
List<Predicate> predicateList = new ArrayList<>();
if (search!=null){
Predicate p1 = criteriaBuilder.like(name.as(String.class),"%"+search+"%");
predicateList.add(p1);
return criteriaQuery.where(p1).getRestriction();
}
Predicate[] pre = new Predicate[predicateList.size()];
return criteriaQuery.where(predicateList.toArray(pre)).getRestriction();
}
};
Pageable pageable=new PageRequest(pageNum-1,pageSize);
Page<Brand> page=brandDao.findAll(specification,pageable);
List<Brand> list = page.getContent();//数据
int totalPages = page.getTotalPages();//总页数
long total = page.getTotalElements();//总条数
return new PageResult<>(total,totalPages,list);
}
public interface BrandDao extends JpaRepository<Brand,Long>, JpaSpecificationExecutor<Brand> { }


浙公网安备 33010602011771号