其他分享
首页 > 其他分享> > 对List进行分页工具类

对List进行分页工具类

作者:互联网

对List<?>进行分页工具类

package com.soft.mpms.zfream.util;
import java.util.List;

/**
 * @ClassName 对List集合进行份分页
 * @description: TODO
 * @author: hhs
 * @Date 2021/10/28 0028 上午 11:18
 **/
public class ListPagingUtil {
    private Integer currentPage;//当前页
    private Integer pageSize;//每页显示记录条数
    private Integer totalPage;//总页数
    private Integer star;//开始数据
    private Integer total;//总条数
    private List<?> dataList;//每页显示的数据

    public Integer getCurrentPage() {
        return currentPage;
    }

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

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getTotalPage() {
        return totalPage;
    }

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

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

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

    public Integer getStar() {
        return star;
    }

    public void setStar(Integer star) {
        this.star = star;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "ListPagingUtil{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalPage=" + totalPage +
                ", dataList=" + dataList +
                ", star=" + star +
                ", total=" + total +
                '}';
    }

    public void pageStartInfo(Integer currentPage, Integer pageSize){
        //如果传入的pageNumber为null给pageNumber赋为1
        currentPage = currentPage == null ? 1 : currentPage;
        //如果传入的pageSize为null给pageSize赋为10
        pageSize = pageSize == null ? 10 : pageSize;
        this.setCurrentPage(currentPage);
        this.setPageSize(pageSize);
    }

    public static ListPagingUtil getListPagingUtil(Integer currentPage, Integer pageSize, List<?> list) {
        ListPagingUtil pagingUtil = new ListPagingUtil();
        //初始化
        pagingUtil.pageStartInfo(currentPage, pageSize);
        //设置起始数据
        pagingUtil.setStar((pagingUtil.getCurrentPage()-1)*pagingUtil.getPageSize());
        //设置总数
        pagingUtil.setTotal(list.size());
        //设置总页数
        pagingUtil.setTotalPage(pagingUtil.getTotal() % pagingUtil.getPageSize() == 0 ? pagingUtil.getTotal()/pagingUtil.getPageSize() :pagingUtil.getTotal()/pagingUtil.getPageSize()+1);
        //截取list
        pagingUtil.setDataList(list.subList(pagingUtil.getStar(), pagingUtil.getTotal()-pagingUtil.getStar()>pagingUtil.getPageSize()?pagingUtil.getStar()+pagingUtil.getPageSize():pagingUtil.getTotal()));
        return pagingUtil;
    }
}

标签:return,分页,pageSize,pagingUtil,List,currentPage,Integer,工具,public
来源: https://www.cnblogs.com/hhs-5120/p/16656853.html