一个分页工具类代码

package cn.piaole.mybatis.demo.common.pager;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 * 
 */
public class Pager implements Serializable {

    private static final long serialVersionUID = 8337463880134937842L;

    /**
     * 当前页数据
     */
    @SuppressWarnings("rawtypes")
    private List data;

    /**
     * 当前页码
     */
    private int currentPage;

    /**
     * 第一条记录(this.currentPage - 1) * this.onePageSize
     */
    private int firstResult;

    /**
     * 总记录数
     */
    private int totalResults;

    /**
     * 每页条数
     */
    private int onePageSize;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 下一页页码
     */
    private int nextPage;

    /**
     * 上一页页码
     */
    private int previousPage;

    //根据当前页、每页条数得到  开始数
    public Pager(int currentPage, int onePageSize) {
        if (currentPage > 1)
            this.currentPage = currentPage;
        else
            this.currentPage = 1;
        this.onePageSize = onePageSize;
        this.firstResult = (this.currentPage - 1) * this.onePageSize;
    }


    public int getCurrentPage() {
        return currentPage;
    }


    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
        if (currentPage <= 0)
            this.currentPage = 1;
        if (currentPage > this.totalPage)
            this.currentPage = totalPage;
        this.firstResult = (this.currentPage - 1) * this.onePageSize;

    }


    public int getOnePageSize() {
        return onePageSize;
    }


    public void setOnePageSize(int onePageSize) {
        this.onePageSize = onePageSize;
    }


    public int getTotalResults() {
        return totalResults;
    }


    /**
     * 设置总记录数,会自动计算出分页数据
     */
    public void setTotalResults(int totalResults) {
        this.totalResults = totalResults;
        if (totalResults % this.onePageSize == 0) {
            this.totalPage = totalResults / this.onePageSize;
        } else {
            this.totalPage = (int) Math.floor(totalResults / this.onePageSize) + 1;
        }

        if (this.totalPage == 0) {
            this.totalPage = 1;
        }
        if (this.currentPage > totalPage) {
            this.currentPage = totalPage;
            this.firstResult = (this.currentPage - 1) * this.onePageSize;

        }
        if (this.currentPage > 1) {
            this.previousPage = this.currentPage - 1;
        } else {
            this.previousPage = 1;
        }
    }


    public int getFirstResult() {
        return firstResult;
    }


    public int getNextPage() {
        if (this.currentPage < this.totalPage) {
            this.nextPage = this.currentPage + 1;
        } else {
            this.nextPage = this.totalPage;
        }
        return nextPage;
    }


    public int getPreviousPage() {
        return previousPage;
    }


    public int getTotalPage() {
        return totalPage;
    }


    @SuppressWarnings("rawtypes")
    public List getData() {
        return data;
    }


    @SuppressWarnings("rawtypes")
    public void setData(List data) {
        this.data = data;
    }


    /**
     * 前台分页导航(返回分页导航条的html源码)
     * 
     * @param total
     *            总记录数
     * @param pageSize
     *            一页显示长度
     * @param currentPage
     *            当前页码
     * @param query_string
     *            链接字符串
     * @return Writer:王军舰 2013年4月8日 09:47:07
     */
    public String pageNavigation(int total, int pageSize, int currentPage, String query_string) {

        boolean flag = false;
        if (query_string.indexOf("%s") > -1) {
            flag = true;
        }

        int allpage = 0; // 总页数
        int next = 0; // 下一页
        int pre = 0; // 上一页
        int startcount = 0; // 开始数
        int endcount = 0; // 结束数

        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize != 0) {
            allpage = (total / pageSize);
            allpage = ((total % pageSize) != 0 ? allpage + 1 : allpage);
            allpage = (allpage == 0 ? 1 : allpage);
        }
        next = currentPage + 1;
        pre = currentPage - 1;

        startcount = currentPage - 2;
        endcount = currentPage + 2;
        // 校正,如果页数小于5则显示全部、大于5最多显示5条
        if (startcount < 1) {
            startcount = 1;
            if (allpage >= 5) {
                endcount = 5;
            } else {
                endcount = allpage;
            }
        }
        // 如果当前页为最后一页,总页数大于5则起始页为总页数-4,否则起始页为第一页
        if (allpage < endcount) {
            endcount = allpage;
            if (allpage >= 5) {
                startcount = allpage - 4;
            } else {
                startcount = 1;
            }
        }
        // 判断是否大于最后一页
        if (currentPage > allpage) {
            currentPage = allpage;
        }

        StringBuilder sb = new StringBuilder();
        if (currentPage > 1) {
            sb.append("<a href=\"").append(getQueryUrl(query_string, "" + pre, flag)).append("\" class=\"pre-page\" >上一页</a> ");
        } else {
            sb.append(" <span class=\"pre-page disabled\">上一页</span> ");
        }
        // 中间页处理,这个增加时间复杂度,减小空间复杂度
        for (int i = startcount; i <= endcount; i++) {
            if (currentPage == i) {
                sb.append("<a class=\"page-p on\">").append(i).append("</a> ");
            } else {
                sb.append(" <a class=\"page-p\" href=\"").append(getQueryUrl(query_string, "" + i, flag)).append("\">").append(i).append("</a> ");
            }
        }
        if (currentPage != allpage) {
            sb.append(" <a  href=\"").append(getQueryUrl(query_string, "" + next, flag)).append("\" class=\"next-page\">下一页</a> ");
        } else {
            sb.append("<span class=\"next-page disabled\">下一页</span> ");
        }
        sb.append(" <span class=\"ml10\"> 共").append(allpage).append("页 </span>");

        return sb.toString();
    }


    private String getQueryUrl(String query, String pageNo, boolean flag) {
        String valueString = "";
        if (flag) {
            valueString = String.format(query, pageNo);
        } else {
            valueString = query + pageNo;
        }

        return valueString;

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值