page分页 加上是否最后一页 工具类

page分页 加上是否最后一页 工具类

备用
@Data
public class Pager<t> implements Serializable {

    private static final long serialVersionUID = -8741766802354222579L;

    private int pageSize; // 每页显示多少条记录

    private int pageNum; //当前第几页数据

    private int total; // 一共多少条记录

    private int pages; // 一共多少页记录

    private boolean hasNextPage = true;//是否最后一页  如果是最后一页 显示为false
 
    private List<t> list = new ArrayList<>(); //要显示的数据

    public Pager(int pageNum, int pageSize, List<t> sourceList) {
        if (sourceList == null || sourceList.isEmpty()) {
            this.hasNextPage = false;
            return;
        }

        // 总记录条数
        this.total = sourceList.size();

        // 每页显示多少条记录
        this.pageSize = pageSize;

        //获取总页数
        this.pages = this.total / this.pageSize;
        if (this.total % this.pageSize != 0) {
            this.pages = this.pages + 1;
        }

        // 当前第几页数据
        this.pageNum = this.pages < pageNum ? this.pages : pageNum;

        // 起始索引
        int fromIndex = this.pageSize * (this.pageNum - 1);

        // 结束索引
        int toIndex =
            this.pageSize * this.pageNum > this.total ? this.total : this.pageSize * this.pageNum;

        if (this.pageNum * this.pageSize >= this.total) {
            this.hasNextPage = false;
        }

        this.list = sourceList.subList(fromIndex, toIndex);
    }

    public Pager() {

    }

    public Pager(int pageSize, int pageNum, int total, int pages,
        List<t> list) {
        super();
        this.pageSize = pageSize;
        this.pageNum = pageNum;
        this.total = total;
        this.pages = pages;
        this.list = list;
    }


}
上一篇:java lambda分页


下一篇:封装练习