JAVA 分页原生代码

Pager

package com.wtz.util;

import java.util.List;

public class Pager<T> {
    
    private Integer pageSize;
    
    private Integer totalRecord;
    
    private Integer totalPage;
    
    private Integer currentPage;
    
    private List<T> list;
    
    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotalRecord() {
        return totalRecord;
    }

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

    public Integer getTotalPage() {
        return totalPage;
    }

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

    public Integer getCurrentPage() {
        return currentPage;
    }

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

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }


    public Pager(Integer pageNo,Integer pageSize,List<T> sourceList){
         if(sourceList==null){
             return;
         }
         
         //总记录数
         this.totalRecord = sourceList.size();
         
         //每页显示多小条数据
         this.pageSize = pageSize;
         
         //总页数
         this.totalPage = this.totalRecord % this.pageSize == 0?this.totalRecord/this.pageSize:this.totalRecord/this.pageSize+1;
         
         //当前第几页
         if(this.totalPage < pageNo){
             this.currentPage = this.totalPage;
         }else{
             this.currentPage = pageNo;
         }
         
         
         //起始索引
         Integer fromIndex = this.pageSize * (this.currentPage - 1);
         
         //结束索引
         Integer endIndex = null;
         if(this.pageSize * this.currentPage >this.totalRecord){
             endIndex = this.totalRecord;
         }else{
             endIndex = this.pageSize * this.currentPage;
         }
         
         this.list = sourceList.subList(fromIndex, endIndex);
    }

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

}

Test

public class Test {
	public static void main(String[] args) {
		List<BgApply> list = new ArrayList<>();
		BgApply bg1 = new BgApply();
		bg1.setBatch("1");
		list.add(bg1);
		BgApply bg2 = new BgApply();
		bg1.setBatch("2");
		list.add(bg2);
		BgApply bg3 = new BgApply();
		bg1.setBatch("3");
		list.add(bg3);
		BgApply bg4 = new BgApply();
		bg1.setBatch("4");
		list.add(bg4);
		BgApply bg5 = new BgApply();
		bg1.setBatch("5");
		list.add(bg5);
		BgApply bg6 = new BgApply();
		bg1.setBatch("6");
		list.add(bg6);
		BgApply bg7 = new BgApply();
		bg1.setBatch("7");
		list.add(bg7);
		
		Pager<BgApply> pager = new Pager<>(1, 3, list);
		
		System.out.println(pager);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_37436998/article/details/88545861