美文网首页JavaWeb
SubList分页-010-Pager类

SubList分页-010-Pager类

作者: 53b3f4658edc | 来源:发表于2017-11-08 10:15 被阅读6次

概述

1.属性+get、set方法
2.增加泛型到类(getList使用)
3.空参/参数构造器-参数构造器很重要,核心
4.toString方法
5.实现Serializable接口

测试代码

package top.itcourse.page.model;


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


public class Pager<T> implements Serializable{

    private static final long serialVersionUID = 6890057936400025118L;

    // 1.每页显示的记录数目
    private int pageSize;
    
    // 2.当前数据的页数
    private int currentPage;
    
    // 3.记录总数目
    private int totalRecord;
    
    // 4.页面总数目
    private int totalPage;
    
    // 5.数据List
    private List<T> dataList;

    // 空参构造器
    public Pager() {

    }
    
    /*
     * 根据List<T> dataList, int currentPage, int pageSize
     * 三个参数算出另外两个
     */
    // 有参构造器
    public Pager(List<T> dataList, int currentPage, int pageSize) {
        if( dataList == null ) {
            return ;
        }
        
        System.out.println("currentPage: " + currentPage);  
        
        // 分页总数
        this.pageSize = pageSize;
        
        // 总记录条数
        this.totalRecord = dataList.size();
        System.out.println("totalRecord: " + this.totalRecord);
        
        // 页面总数目
        this.totalPage = totalRecord / pageSize;
        if( totalRecord % pageSize != 0 ) {
            totalPage += 1;
        }
        
        
        // 当前页:如果传进来的当前页大于总的数目,我们就认为它是最后一页
        System.out.println("totalPage: " + this.totalPage);
        this.currentPage = currentPage;
        if( this.totalPage < currentPage ) {
            this.currentPage = this.totalPage;
        }
        
        // 数据列(这就是重点了,subList)
            // 当前页 起始记录位置
        System.out.println(this.pageSize + ":" + this.currentPage);
        int fromIndex = this.pageSize*(this.currentPage - 1);       // 好好理解
            // 当前页 结束的记录位置
        int toIndex = this.pageSize*this.currentPage;               // 好好理解
        if( toIndex > totalRecord ) {
            toIndex = totalRecord;
        }
            // 数据列赋值
        this.dataList = dataList.subList(fromIndex, toIndex);
    }
    

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    @Override
    public String toString() {
        return "Page [pageSize=" + pageSize + ", currentPage=" + currentPage + ", totalRecord=" + totalRecord
                + ", totalPage=" + totalPage + ", dataList=" + dataList + "]";
    }
    
    
}

源码下载

关注下方的微信公众号,回复:java_div_page.code





欢迎加入交流群:451826376


更多信息:www.itcourse.top

完整教程PDF版本下载

相关文章

网友评论

    本文标题:SubList分页-010-Pager类

    本文链接:https://www.haomeiwen.com/subject/gidemxtx.html