使用PageHelper对List进行分页

时间:2022-03-20
本文章向大家介绍使用PageHelper对List进行分页,主要包括使用PageHelper对List进行分页使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

添加pom依赖:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.3.0</version>
</dependency>

复制下面的工具类就可以使用啦!

import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
 
import java.util.List;
 
/**
 * 对List进行分页
 */
public class PageUtil {
 
    public static <T> PageInfo<T> startPage(List<T> list, Integer pageNum, Integer pageSize) {
        //创建Page类
        com.github.pagehelper.Page page = new com.github.pagehelper.Page(pageNum, pageSize);
        //为Page类中的total属性赋值
        page.setTotal(list.size());
        //计算当前需要显示的数据下标起始值
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, list.size());
        //从链表中截取需要显示的子链表,并加入到Page
        page.addAll(list.subList(startIndex,endIndex));
        //以Page创建PageInfo
        PageInfo pageInfo = new PageInfo<>(page);
        return pageInfo;
    }
 
}

原文地址:https://www.cnblogs.com/LilLazy/p/16032553.html