java后端逻辑分页工具类

/**
 * @Description: 分页工具类:用于逻辑分页(数据量较小时使用)
 * @author: erlang
 * @date: 2021年07月29日 13:39
 */
public class PageUtil implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 总记录数
     */
    private int totalCount;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;
    /**
     * 当前页数
     */
    private int current;
    /**
     * 列表数据
     */
    private List<?> records;

    /**
     * 分页
     *
     * @param list       列表数据
     * @param totalCount 总记录数
     * @param pageSize   每页记录数
     * @param current    当前页数
     */
    public PageUtil(List<?> list, Integer totalCount, Integer pageSize, Integer current) {
        if (null == list || list.size() == 0) {
            this.records = new ArrayList<>();
        } else {
            //计算总页数
            Integer pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
            // 开始索引位置
            int beginIndex = 0;
            // 结束索引位置
            int endIndex = 0;
            //计算索引位置
            if (!current.equals(pageCount)) {
                beginIndex = (current - 1) * pageSize;
                endIndex = beginIndex + pageSize;
            } else {
                beginIndex = (current - 1) * pageSize;
                endIndex = totalCount;
            }
            this.records = list.subList(beginIndex, endIndex);
            this.totalCount = totalCount;
            this.pageSize = pageSize;
            this.current = current;
            this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
        }
    }


    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public List<?> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    @Override
    public String toString() {
        return "PageUtilJb{" +
                "totalCount=" + totalCount +
                ", pageSize=" + pageSize +
                ", totalPage=" + totalPage +
                ", current=" + current +
                ", records=" + records +
                '}';
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值