PageIndex 计算页码

/**
 * 计算页码如: 1,2,3,4...
 * @author Bert
 *
 */
public class PageIndex {
    private long startindex;
    private long endindex;  
    
    public PageIndex(long startindex, long endindex) {
        this.startindex = startindex;
        this.endindex = endindex;
    }
    public long getStartindex() {
        return startindex;
    }
    public void setStartindex(long startindex) {
        this.startindex = startindex;
    }
    public long getEndindex() {
        return endindex;
    }
    public void setEndindex(long endindex) {
        this.endindex = endindex;
    }
    
    public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
            long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
            long endpage = currentPage+viewpagecount/2;
            if(startpage<1){
                startpage = 1;
                if(totalpage>=viewpagecount) endpage = viewpagecount;
                else endpage = totalpage;
            }
            if(endpage>totalpage){
                endpage = totalpage;
                if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
                else startpage = 1;
            }
            return new PageIndex(startpage, endpage);        
    }

}



public class PageView<T> {
    
    
    /** 分页数据成功! * */
    private List<T> records;
    /** 页码开始索引和结束索引 * */
    private PageIndex pageindex;

    /** 总页数 * */
    private long totalpage = 1;
    /** 每页显示记录数 * */
    private int maxresult = 10;
    /** 当前页 * */
    private int currentpage = 1;
    /** 总记录数 * */
    private long totalrecord;
    /** 页码数量 * */
    private int pagecode = 6;

    public PageView(int maxresult, int currentpage) {
        this.maxresult = maxresult; // 每页显示的记录数
        this.currentpage = currentpage; // 当前页
    }

    /** 要获取记录的开始索引 * */
    public int getFirstResult() {
        return (this.currentpage - 1) * this.maxresult;
    }

    public int getPagecode() {
        return pagecode;
    }

    public void setPagecode(int pagecode) {
        this.pagecode = pagecode;
    }

    public void setQueryResult(QueryResult<T> qr) {
        setTotalrecord(qr.getTotalrecord());
        setRecords(qr.getResultlist());
    }

    public long getTotalrecord() {
        return totalrecord;
    }

    public void setTotalrecord(long totalrecord) {
        this.totalrecord = totalrecord;
        setTotalpage(this.totalrecord % this.maxresult == 0 ? this.totalrecord
                / this.maxresult : this.totalrecord / this.maxresult + 1);
    }

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

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

    public PageIndex getPageindex() {
        return pageindex;
    }

    public long getTotalpage() {
        return totalpage;
    }

    public void setTotalpage(long totalpage) {
        this.totalpage = totalpage;
        this.pageindex = PageIndex.getPageIndex(pagecode, currentpage,
                totalpage);
    }

    public int getMaxresult() {
        return maxresult;
    }

    public int getCurrentpage() {
        return currentpage;
    }
}



/**
 * 存放分页的结果集
 * @author Bert
 * @param 实体类
 */
public class QueryResult<T> {
    /** 查询结果记录 **/
    private List<T> resultlist;

    /** 总记录数 **/
    private long totalrecord;

    public List<T> getResultlist() {
        return resultlist;
    }

    public void setResultlist(List<T> resultlist) {
        this.resultlist = resultlist;
    }

    public long getTotalrecord() {
        return totalrecord;
    }

    public void setTotalrecord(long totalrecord) {
        this.totalrecord = totalrecord;
    }

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值