Bootstrap

PageUtil 分页工具类

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;

/**
 * @author chenlong
 * @create 2022-03-15 19:52
 */
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);
        }
    }

}

;