最近经常使用后台分页功能,就写了一个分页工具类。
public class PageUtil {
//List<?> objectList 泛型类List,自动适配任何类型
public static ResultUtil getPageList(List<?> objectList, Integer pageIndex,Integer pageSize){
if( null != objectList && objectList.size() > 0){
//当前页面第一条数据的编号: startIndex = pageIndex * pageSize
int startIndex = (pageIndex - 1) * pageSize;
//当前页面最后一条数据的理论编号: tempIndex = startIndex + pageSize
int tempIndex = startIndex + pageSize;
//当前页面最后一条数据的实际编号(防止编号下表越界)
int endIndex = tempIndex < objectList.size() ? tempIndex : objectList.size();
//截取当前页面数据
objectList = objectList.subList(startIndex,endIndex);
return new ResultUtil(CodeUtil.QUERY_OK , objectList , "查询成功");
}
else{
return new ResultUtil(CodeUtil.QUERY_ERRO , null , "查询失败");
}
}
}