手动分页工具类

package com.cguarantee.base.config;
import java.util.ArrayList;
import java.util.List;
/**
 * 手动分页工具类
 * liin
 */
public class MyPageUtils {
    /**
     * @param pageNo   第几页 (从1计数)
     * @param pageSize 每页展示几条数据
     * @param data     数据源
     * @return 分页对象
     */
    public static <T> PageInfo<T> pageMap(int pageNo, int pageSize, List<T> data) {
        PageInfo<T> pageInfo = new PageInfo<>();
        int[] ints = transToStartEnd(pageNo, pageSize, data.size());
        if (ints[0]>ints[1]){
            pageInfo.setRecords(new ArrayList<>());
            pageInfo.setTotal(data.size());
            pageInfo.setCurrent(pageNo);
            pageInfo.setSize(pageSize);
            return pageInfo;
        }
        List<T> list = data.subList(ints[0], ints[1]);
        pageInfo.setRecords(list);
        pageInfo.setTotal(data.size());
        pageInfo.setCurrent(pageNo);
        pageInfo.setSize(pageSize);
        return pageInfo;
    }
    /**
     *
     * @param pageNo 第几页 (从1计数)
     * @param pageSize 每页展示几条数据
     * @param dataSize 数据源元素个数
     * @return 第一个开始位置 第二个结束位置
     */
    private static int[] transToStartEnd(int pageNo, int pageSize, int dataSize) {
        if (pageNo < 1) {
            pageNo = 1;
        }

        if (pageSize < 1) {
            pageSize = 0;
        }
        final int start=  (pageNo - 1) * pageSize;
        if (pageSize < 1) {
            pageSize = 0;
        }
        int end = pageNo * pageSize;
        if (end > dataSize) {
            end = dataSize;
        }
        return new int[] { start, end };
    }
}

分页对象

package com.cguarantee.utils;


import io.swagger.annotations.ApiModelProperty;

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

public class PageInfo<T> implements Serializable {
    private static final long serialVersionUID = 8545991263226528798L;

    @ApiModelProperty(value = "每页数据")
    private List<T> records;

    @ApiModelProperty(value = "总记录数")
    private long total;

    @ApiModelProperty(value = "每页记录数")
    private long size;

    @ApiModelProperty(value = "当前页")
    private long current;

    @ApiModelProperty(value = "附加数据")
    private Map<String,Object> extra;

    public PageInfo() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;

    }

    public PageInfo(long current, long size) {
        this(current, size, 0L);
    }

    public PageInfo(long current, long size, long total) {
        this(current, size, total, true);
    }

    public PageInfo(long current, long size, boolean isSearchCount) {
        this(current, size, 0L, isSearchCount);
    }

    public PageInfo(long current, long size, long total, boolean isSearchCount) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        if (current > 1L) {
            this.current = current;
        }

        this.size = size;
        this.total = total;
    }


    public List<T> getRecords() {
        return this.records;
    }

    public PageInfo<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public PageInfo<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public PageInfo<T> setSize(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public PageInfo<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public Map<String, Object> getExtra() {
        return extra;
    }

    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值