分页实体

1.通过该实体,只需传入当前页就能获得起始记录数,默认每页10条 ,传入总记录数,就能得到总页数

package com.****.util.mybatis;

import java.io.Serializable;

public final class Pager implements Serializable {

    /**
     * 默认的序列化版本 id.
     */
    private static final long serialVersionUID = 1L;
    /**
     * 分页查询开始记录位置.
     */
    private int begin=0;
    /**
     * 分页查看下结束位置.
     */
    private int end=10;
    /**
     * 每页显示记录数.
     */
    private int rows = 10;
    /**
     * 查询结果总记录数.
     */
    private int totalRecords;
    /**
     * 当前页码.
     */
    private int page=1;
    /**
     * 总共页数.
     */
    private int pageCount=0;
    
    private String sort; //排序的列
    private String order; //顺序或倒序

    public Pager() {
        this.begin=0;
        this.end=10;
    }

    /**
     * 构造函数.
     * 
     * @param begin 起始位置
     * @param length 每页显示记录数
     */
    public Pager(int begin, int length) {
        this.begin = begin;
        this.rows = length;
        this.end = this.begin + this.rows;
        this.page = (int) Math.floor((this.begin * 1.0d) / this.rows) + 1;
    }

    /**
     * @param begin 起始位置
     * @param length 每页显示记录数
     * @param totalRecords 查询结果总记录数.
     */
    public Pager(int begin, int length, int totalRecords) {
        this(begin, length);
        this.totalRecords = totalRecords;
    }

    /**
     * 设置页数,自动计算数据范围.
     * @param pageNo
     */
    public Pager(int pageNo) {
        this.page = pageNo;
        pageNo = pageNo > 0 ? pageNo : 1;
        this.begin = this.rows * (pageNo - 1);
        this.end = this.rows * pageNo;
    }

    /**
     * @return the begin
     */
    public int getBegin() {
        return begin;
    }

    /**
     * @return the end
     */
    public int getEnd() {
        return end;
    }

    /**
     * @return the length
     */
    public int getRows() {
        return rows;
    }

    /**
     * @param length
     * the length to set
     */
    public void setRows(int rows) {
        this.rows = rows;
        this.begin = this.rows * (page - 1);
        this.end = this.rows * page;
    }

    public void setBegin(int begin) {
        this.begin = begin;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    /**
     * @return the totalRecords
     */
    public int getTotalRecords() {
        return totalRecords;
    }

    /**
     * @param totalRecords
     * 传入总记录数得到就能得到页数
     */
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
        this.pageCount = (int) Math.floor((this.totalRecords * 1.0d)
                / this.rows);
        if (this.totalRecords % this.rows != 0) {
            this.pageCount++;
        }
    }

    /**
     * @return the pageNo
     */
    public int getPage() {
        return page;
    }

    /**
     * @param pageNo the page to set
     * 传到当前页就能得到开始记录数
     */
    public void setPage(int page) {
        this.page = page;
        page = page > 0 ? page : 1;
        this.begin = this.rows * (page - 1);
        this.end = this.rows * page;
    }

    /**
     * @return the pageCount
     */
    public int getPageCount() {
        if (pageCount == 0) {
            return 1;
        }
        return pageCount;
    }

    /**
     * @param pageCount
     * the pageCount to set
     */
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
    
    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder("begin=").append(begin)
                .append(", end=").append(end).append(", length=")
                .append(rows).append(", totalRecords=").append(totalRecords)
                .append(", pageNo=").append(page).append(", pageCount=")
                .append(pageCount);
        return builder.toString();
    }
    
}

 

posted @ 2017-11-10 11:10  孟夏草木长  阅读(499)  评论(0编辑  收藏  举报