1、写作背景
懒惰象生锈一样,比操劳更能消耗身体。
2、参考网址
3、学习目的
- 记录工具类进行内存分页
4、核心操作
1)内存分页工具
- 分页工具类
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class TableDataInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
private Integer total;
private Integer startIndex;
private Integer pageSize;
private transient List<T> rows;
public TableDataInfo(List<T> list, Integer startIndex, Integer pageSize) {
// 兼容
startIndex = null == startIndex ? 1 : startIndex;
startIndex = startIndex == 0 ? 1 : startIndex;
pageSize = null == pageSize ? 10 : pageSize;
List<T> resultList = null;
// 赋值
this.startIndex = startIndex;
this.pageSize = pageSize;
this.rows = list;
// 手动分页
int beginIndex = (startIndex - 1) * pageSize;
int endIndex = startIndex * pageSize;
if (beginIndex > list.size() - 1) {
this.rows = null;
} else if (endIndex >= list.size()) {
resultList = list.subList(beginIndex, list.size());
} else {
resultList = list.subList(beginIndex, endIndex);
}
this.rows = resultList;
}
}
- 分页测试
public static TableDataInfo<String> getRageInfoData(){
List<String> list = Arrays.asList("01", "02", "03", "04", "05", "06", "07", "08");
return new TableDataInfo<>(list,1,2);
}
网友评论