首先定义一个简单分页信息实体
/**
* Copyright (C), 2018-2019
* FileName: CustomPageInfo
* Author: yu.gui.ming
* Date: 2019-08-05 11:42
* Description: 自定义分页信息对象
*/
public class CustomPageInfo<T> implements Serializable {
//当前页
private int pageNum;
//总行数
private Integer dataRows;
//总页数
private Integer totalPages;
//当前页记录数
private Integer pageNumSize;
//结果集
private List<T> list;
public Integer getPageNumSize() {
return pageNumSize;
}
public void setPageNumSize(Integer pageNumSize) {
this.pageNumSize = pageNumSize;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Integer getDataRows() {
return dataRows;
}
public void setDataRows(Integer dataRows) {
this.dataRows = dataRows;
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
}
具体工具类逻辑
public CustomPageInfo<T> getPageList(List<T> resultList, Integer pageIndex, Integer pageSize) {
if(resultList == null || resultList.size() <= 0 || resultList.isEmpty()){
List<T> list = new ArrayList<T>();
CustomPageInfo<T> result = new CustomPageInfo<T>();
result.setList(list);
result.setPageNum(pageIndex == null ? 0:pageIndex);
result.setPageNumSize(pageSize == null ? 0:pageSize);
result.setTotalPages(0);
result.setDataRows(0);
return result;
}
int dataRows = resultList.size();//总行数
if(pageIndex == null || pageSize == null || pageIndex <= 0 || pageSize <= 0){
pageIndex = 1;pageSize = dataRows;
}
int totalPages = (dataRows % pageSize == 0) ? (dataRows / pageSize) : (dataRows / pageSize + 1) ; //总页数
CustomPageInfo customPageInfo = new CustomPageInfo();//分页对象
customPageInfo.setDataRows(dataRows);//总行数
customPageInfo.setTotalPages(totalPages);//总页数
customPageInfo.setPageNum(pageIndex);//当前页
//如果分页索引大总页数
if(pageIndex > totalPages){
customPageInfo.setPageNumSize(pageSize);
customPageInfo.setList(new ArrayList());
return customPageInfo;
}
//设置当前页的记录数,如果是最后一页
if(totalPages == pageIndex){
customPageInfo.setPageNumSize(dataRows-(totalPages-1)*pageSize);
}
//如果不是最后一页,当前记录数等于pageSize
if(totalPages > pageIndex){
customPageInfo.setPageNumSize(pageSize);
}
if(pageIndex == null || pageSize == null || pageIndex == 0 || pageSize == 0){
customPageInfo.setList(resultList);
return customPageInfo;
}
List<T> pageList = new ArrayList();
int pageStartRows ;//起始行数
int pageEndRows;//结束行数
if (pageIndex * pageSize < dataRows) {
pageEndRows = pageIndex * pageSize;
pageStartRows = pageEndRows - pageSize;
} else {
pageEndRows = dataRows;
pageStartRows = pageSize * (totalPages - 1);
}
for (int i = pageStartRows; i < pageEndRows; i++) {
pageList.add(resultList.get(i));
}
customPageInfo.setList(pageList);
return customPageInfo;
}
具体controller调用
@ApiOperation(value = "测试")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageIndex", value = "分页索引", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "string", paramType = "query"),
})
@RequestMapping(value = "getCheckCardDetailsList.do", method = RequestMethod.POST)
public JsonResult<CustomPageInfo<AssetCardDetails>> getCheckCardDetailsList(Integer pageIndex, Integer pageSize, @RequestBody String jStr) {
ParamUtil.validateParam(jStr,"jStr is null");
CheckQueryReq req = JSON.parseObject(jStr,CheckQueryReq.class);
CustomPageInfoUtil<AssetCardDetails> customPageInfoUtil = new CustomPageInfoUtil();
List<AssetCardDetails> resultList = assetCheckService.getCheckCardDetailsList(req.getTypeIdList(),req.getUserDepartmentList(),
req.getStoragePlaceList(),req.getUseConditionList(),req.getPersonnelList());
return new JsonResult<CustomPageInfo<AssetCardDetails>>(customPageInfoUtil.getPageList(resultList,pageIndex,pageSize));
}
效果如下图
到此完毕