package cn.drhui.diaowen.util;
import java.util.List;
/**
* @ClassName: PageUtil
* @Description:
* @Author: 我是真的很不错
* @Date: 2021/1/14 11:33
*/
public class PageUtil {
/**
* 开始分页
*
// * @param list
// * @param pageNum 页码
// * @param pageSize 每页多少条数据
* @return
*/
public static PageInfo startPage(List list, Integer pageNum, Integer pageSize) {
// public static PageInfo startPage(PageInfo pageInfo) {
if(list == null){
return null;
}
if(list.size() == 0){
return null;
}
Integer count = list.size(); //记录总数
Integer pageCount = 0; //页数
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex = 0; //开始索引
int toIndex = 0; //结束索引
if(pageNum > pageCount){
pageNum = pageCount;
}
if (!pageNum.equals(pageCount)) {
fromIndex = (pageNum - 1) * pageSize;
toIndex = fromIndex + pageSize;
} else {
fromIndex = (pageNum - 1) * pageSize;
toIndex = count;
}
List pageList = list.subList(fromIndex, toIndex);
PageInfo resultPageInfo = new PageInfo();
resultPageInfo.setList(pageList);
resultPageInfo.setCount(count);
resultPageInfo.setPages(pageCount);
resultPageInfo.setPageNum(pageNum);
resultPageInfo.setPageSize(pageSize);
return resultPageInfo;
}
}
返回实体类
package cn.drhui.diaowen.util;
import lombok.Data;
import java.util.List;
/**
* @ClassName: PageInfo
* @Description:
* @Author: 我是真的很不错
* @Date: 2021/1/14 11:49
*/
@Data
public class PageInfo{
private Integer pageNum;
private Integer pageSize;
private List list;
private int count;
private int pages;
}