Java分页工具类

通用分页工具类:

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

/**
 *
 * <b> 分页通用类 </b>
 *
 * @author hcw
 * @param <T>
 *
 */
public class Pager<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 4542617637761955078L;

    /**
     * currentPage 当前页
     */
    private int currentPage = 1;
    /**
     * pageSize 每页大小
     */
    private int pageSize = 10;
    /**
     * pageTotal 总页数
     */
    private int pageTotal;
    /**
     * recordTotal 总条数
     */
    private int recordTotal = 0;
    /**
     * previousPage 前一页
     */
    private int previousPage;
    /**
     * nextPage 下一页
     */
    private int nextPage;
    /**
     * firstPage 第一页
     */
    private int firstPage = 1;
    /**
     * lastPage 最后一页
     */
    private int lastPage;

    /**
     * 是否最后一页
     */
    private boolean isLastPage;

    /**
     * 是否是第一页
     */
    private boolean isFirstPage;

    /**
     * data 每页的内容
     */
    private List<T> data;

    // 以下set方式是需要赋值的
    /**
     * 设置当前页 <br>
     *
     * @param currentPage
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * 设置每页大小,也可以不用赋值,默认大小为10条 <br>
     *
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 设置总条数,默认为0 <br>
     *
     *
     * @param recordTotal
     */
    public void setRecordTotal(int recordTotal) {
        this.recordTotal = recordTotal;
        otherAttr();
    }

    /**
     * 设置分页内容 <br>
     *
     * @param data
     */
    public void setData(List<T> data) {
        this.data = data;
    }

    /**
     * 设置其他参数
     *
     *
     */
    public void otherAttr() {
        // 总页数
        this.pageTotal = this.recordTotal % this.pageSize > 0 ? this.recordTotal / this.pageSize + 1 : this.recordTotal / this.pageSize;
        // 第一页
        this.firstPage = 1;
        // 最后一页
        this.lastPage = this.pageTotal;
        // 前一页
        if (this.currentPage > 1) {
            this.previousPage = this.currentPage - 1;
        } else {
            this.previousPage = this.firstPage;
        }
        // 下一页
        if (this.currentPage < this.lastPage) {
            this.nextPage = this.currentPage + 1;
        } else {
            this.nextPage = this.lastPage;
        }

        //是否是第一页
        if(this.currentPage==1){
            this.isFirstPage = true;
        }else{
            this.isFirstPage = false;
        }

        //是否是最后一页
        if(this.currentPage == this.pageTotal){
            this.isLastPage = true;
        }else{
            this.isLastPage = false;
        }

    }

    // 放开私有属性
    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    public int getRecordTotal() {
        return recordTotal;
    }

    public int getPreviousPage() {
        return previousPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getFirstPage() {
        return firstPage;
    }

    public int getLastPage() {
        return lastPage;
    }


    public boolean isLastPage() {
        return isLastPage;
    }


    public boolean isFirstPage() {
        return isFirstPage;
    }


    public List<T> getData() {
        return data;
    }


    @Override
    public String toString() {
        return "Pager [currentPage=" + currentPage + ", pageSize=" + pageSize
                + ", pageTotal=" + pageTotal + ", recordTotal=" + recordTotal
                + ", previousPage=" + previousPage + ", nextPage=" + nextPage
                + ", firstPage=" + firstPage + ", lastPage=" + lastPage
                + ", isfirstPage=" + isFirstPage() + ", islastPage=" + isLastPage()
                + ", data=" + data + "]";
    }

}

 

 

分页测试类:

import java.util.ArrayList;
import java.util.List;

/**
 *
 * <b> 分页测试类 </b>
 *
 * @author hcw
 *
 */
public class PagerTester {
    public static void main(String[] args) {

        Pager<String> pager = new Pager<String>();
        List<String> content = new ArrayList<String>();
        content.add("str1");
        content.add("str2");
        content.add("str3");
        content.add("str4");
        content.add("str5");
        content.add("str6");
        content.add("str7");
        content.add("str8");
        content.add("str9");
        content.add("str10");
        pager.setCurrentPage(7);
        pager.setPageSize(10);
        pager.setRecordTotal(62);
        pager.setData(content);
        System.out.println(pager);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值