java框架中分页问题 工具类

来源:互联网 发布:音乐调音器软件 编辑:程序博客网 时间:2024/05/22 00:37

直接上代码吧!话不多说,中间代码都有详细解释:

package com.yinkailong.vrv.utils;import java.util.List;/** * 分页工具类(Google式分页) * @author yinkailong */public class PageViewUtil {    /*用户指定(或配置)*/    private int currentPage;  //当前页    private int pageSize;  //每页显示的数据的数量    /*从数据库中查询*/    private int recordCount; // 总记录数    private List<Object> recordList; // 当前页的数据列表    /*计算*/    private int pageCount; // 总页码    private int beginPageIndex; // 显示的页码列表的开始索引    private int endPageIndex; // 显示的页码列表的结束索引    /*默认构造方法*/    public PageViewUtil() {//无内容}    /*实类化时初始化*/    public PageViewUtil(int currentPage, int pageSize, int recordCount, List<Object> recordList){        this.currentPage = currentPage;        this.pageSize = pageSize;        this.recordCount = recordCount;        this.recordList = recordList;        /*计算其他3个属性的值*/        //1.总页码        pageCount = (recordCount + pageSize -1)/pageSize;        //添加默认值         if( this.currentPage == 0 && pageCount == this.currentPage){            this.currentPage = pageCount = 1 ;        }        //2.页码列表的开始索引和结束索引        if(pageCount<=10){            //2.1 总页数不够10页时            beginPageIndex = 1;            endPageIndex = pageCount;        }else{            //2.2 总页数大于10页时            //2.2.1 显示前4页,后5页            beginPageIndex = currentPage - 4;            endPageIndex = currentPage + 5;            if(beginPageIndex < 1){                //2.2.1.1 前面不足4页时,显示前10页                beginPageIndex = 1;                endPageIndex = 10;            }else if(endPageIndex > pageCount){                //2.2.1.2 后面不足5页时,显示后10页                //beginPageIndex = pageCount + 9;                //下面是原理实现                beginPageIndex = pageCount - 10 + 1;                   endPageIndex = pageCount;            }        }    }    //一些初始化方法    public int getCurrentPage() {        return currentPage;    }    public void setCurrentPage(int currentPage) {        this.currentPage = currentPage;    }    public int getPageSize() {        return pageSize;    }    public void setPageSize(int pageSize) {        this.pageSize = pageSize;    }    public int getRecordCount() {        return recordCount;    }    public void setRecordCount(int recordCount) {        this.recordCount = recordCount;    }    public List<Object> getRecordList() {        return recordList;    }    public void setRecordList(List<Object> recordList) {        this.recordList = recordList;    }    public int getPageCount() {        return pageCount;    }    public void setPageCount(int pageCount) {        this.pageCount = pageCount;    }    public int getBeginPageIndex() {        return beginPageIndex;    }    public void setBeginPageIndex(int beginPageIndex) {        this.beginPageIndex = beginPageIndex;    }    public int getEndPageIndex() {        return endPageIndex;    }    public void setEndPageIndex(int endPageIndex) {        this.endPageIndex = endPageIndex;    }}

主要代码就是分页那方法,其他地方没有什么经典的地方,需要的直接copy过去就能用了!