Java 简单的分页

paging.jpg
简单的分页,顺便测试代码高亮插件

Page.java

package entity;

public class Page {

    private int pageIndex = 1;  //要显示的页码
    private int totalPage = 1;  //总页码
    private int pageSize = 5;   //每页显示数量
    private int totalRecords = 0;   //数据总数
    private int beginRows = 1;  //开始数据行数
    private int endRows = 5;    //结束数据行数
    private boolean isHavePrePage = false;  //是否有上一页
    private boolean isHaveNextPage = false; //是否有下一页
    
    public Page() {
        super();
    }
    
    public Page(Integer pageIndex, Integer totalRecords) {
        this.pageIndex = pageIndex;
        this.totalRecords = totalRecords;
        
        //计算总页码
        if(this.totalRecords % this.pageSize == 0){
            this.totalPage = this.totalRecords / this.pageSize;
        } else {
            this.totalPage = this.totalRecords / this.pageSize + 1;
        }
        
        //排除错误行号
        if(this.pageIndex < 1){
            this.pageIndex = 1;
        }
        if(this.pageIndex > this.totalPage){
            this.pageIndex = this.totalPage;
        }
        
        //计算开始、结束行数
        this.beginRows = (this.pageIndex - 1) * this.pageSize + 1;
        if(this.pageIndex == this.totalPage){
            this.endRows = this.totalRecords;
        }
        
        //是否有上、下一页
        if(this.pageIndex < this.totalPage){
            this.isHaveNextPage = true;
        }
        if(this.pageIndex > 1){
            this.isHavePrePage = true;
        }
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getTotalPage() {
        return totalPage;
    }

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

    public int getPageSize() {
        return pageSize;
    }

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

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getBeginRows() {
        return beginRows;
    }

    public void setBeginRows(int beginRows) {
        this.beginRows = beginRows;
    }

    public int getEndRows() {
        return endRows;
    }

    public void setEndRows(int endRows) {
        this.endRows = endRows;
    }

    public boolean getIsHavePrePage() {
        return isHavePrePage;
    }

    public void setIsHavePrePage(boolean isHavePrePage) {
        this.isHavePrePage = isHavePrePage;
    }

    public boolean getIsHaveNextPage() {
        return isHaveNextPage;
    }

    public void setIsHaveNextPage(boolean isHaveNextPage) {
        this.isHaveNextPage = isHaveNextPage;
    }

    @Override
    public String toString() {
        return "Page [pageIndex=" + pageIndex + ", totalPage=" + totalPage
                + ", pageSize=" + pageSize + ", totalRecords=" + totalRecords
                + ", beginRows=" + beginRows + ", endRows=" + endRows
                + ", isHavePrePage=" + isHavePrePage + ", isHaveNextPage="
                + isHaveNextPage + "]";
    }
}

仅有 1 条评论
  1. 姜智超

    else { this.endRows = this.pageIndex * this.pageSize; }

    姜智超 October 26th, 2017 at 09:45 pm回复
发表新评论