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;
}
}