maven 依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
工具类
package com.kurumi.util;
import com.github.pagehelper.PageInfo;
import com.kurumi.pojo.BlogCategoryPO;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class PageUtil {
public static void listToPageInfo(List<?> list, Integer pageSize, Integer pageNum, PageInfo pageInfo) {
pageInfo.setPageSize(pageSize);
pageInfo.setPageNum(pageNum);
if (!CollectionUtils.isEmpty(list)) {
int total =list.size();
pageInfo.setTotal(total);
int pages = (int) Math.ceil((double) total/pageSize);
pageInfo.setPages(pages);
if (pageNum > pages) {
pageNum = pages;
pageInfo.setPageNum(pageNum);
}
int fromIndex = (pageNum - 1) * pageSize;
int toIndex = pageNum * pageSize;
if (toIndex > total) {
toIndex = total;
}
pageInfo.setList(list.subList(fromIndex,toIndex));
pageInfo.setSize(toIndex - fromIndex);
pageInfo.setIsFirstPage(pageNum == 1);
pageInfo.setIsLastPage(pageNum == pages);
pageInfo.setHasPreviousPage(pageNum >= 2 && pageNum <= pages);
pageInfo.setHasNextPage(pageNum <= pages - 1);
pageInfo.setPrePage(pageNum == 1 ? 1 : pageNum - 1);
pageInfo.setNextPage(pageNum >= pages ? pageNum : pageNum +1);
}
}
}