java 实现分页查询的实体类

/**
 * 分页实体类
 */
@Data
public class PageInfo<T> {
    private Integer pno; //当前页码
    private Integer pageSize; //每页记录数
    private Integer rowCount;  //总记录数

    private Integer sumPages; //总页数
    private int prev;   //上一页
    private int next;   // 下一页
    private int startLine;  //起始行
    private int startPage;  //起始页码
    private int endPage;  //结束页码
    private List<T> list;   //数据列表


    public PageInfo(Integer pno,Integer pageSize,Integer rowCount){
        this.pno = pno;
        this.pageSize = pageSize;
        this.rowCount = rowCount;

        //确定总页数
        this.sumPages = this.rowCount%this.pageSize==0 ?
                this.rowCount/this.pageSize :
                this.rowCount/this.pageSize+1;

        //判断pno是否超出有效范围
        if(this.pno > this.sumPages){
            this.pno = this.sumPages;
        }

        if(this.pno<1){
            this.pno=1;
        }

        //上一页与下一页
        this.next = this.pno+1;
        this.prev = this.pno-1;

        //起始行
        this.startLine = (this.pno-1)*this.pageSize;

        //计算起始页码与结束页码
        if(this.sumPages<5) {  // 不足5页
            this.startPage = 1;
            this.endPage = this.sumPages;
        }else { // 5页以上
            this.startPage = this.pno-2;
            this.endPage = this.pno+2;
            //如果当前页码是第 1 页呢
            if(this.startPage<1) {
                this.startPage = 1;
                this.endPage = 5;
            }

            //如果当前码是末尾,此时 endPage比最大页码还大
            if(this.endPage > this.sumPages) {
                this.startPage = this.sumPages - 4;
                this.endPage = this.sumPages;
            }
        }

    }

   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值