public class Func {
// 使用Lambda表达式对编写出来的代码可读性较好,毕竟程序是给人看的,给机器执行的。
public static void main(String[] args) {
{
// 写法一
List<String> result = Func.pageFunc(2, 10, String.class).count(() -> {
return 1;// 执行数据库count调用
}).find((p) -> {// 数据库查询
return Lists.newArrayList("1", "2");// xxx.find(p.getStart(), p.getPageSize());
}).done();
System.out.println(Arrays.toString(result.toArray()));
}
{
// 写法二
Func.pageFunc(2, 10, int.class).count(() -> {
return 0;// 只有这里返回大于零,才会执行下面的find查询
}).find((p) -> {
return Lists.newArrayList(p.getStart(), p.getPageSize());
}).forEach((e) -> {// 消费返回列表
System.out.println("返回元素:" + e);
});
}
}
/**
* 分页编排函数调用
*
* @param pageNo 请求页码(1,2 ...)
* @param findMethodRtnType 数据库查询返回类型(->find())
* @return
*/
public static <T> Paged<T> pageFunc(int pageNo, Class<T> findMethodRtnType) {
return new Paged<T>(pageNo);
}
/**
* 分页编排函数调用
*
* @param pageNo 请求页码(1,2 ...)
* @param pageSize 每页返回数据条数
* @param findMethodRtnType 数据库查询返回类型(->find())
* @return
*/
public static <T> Paged<T> pageFunc(int pageNo, int pageSize, Class<T> findMethodRtnType) {
return new Paged<T>(pageNo, pageSize);
}
}
/**
* 分页器
*
* @author KEVIN LUAN
*
* @param <T>
*/
public class Paged<T> {
// 数据总条数
@Getter
private int totalSize = 0;
@SuppressWarnings("unchecked")
private List<T> list = Collections.EMPTY_LIST;
// 当前页面
private int pageNo = 0;
// 每页数据条数
@Getter
private int pageSize = 10;
// 总页数
@Getter
private int totalPage = 0;
public Paged(int pageNo) {
this.pageNo = pageNo;
}
/**
* 获取Limit $start
*
* @return
*/
public int getStart() {
return PaginationHelper.calcStart(pageNo, pageSize);
}
public Paged(int pageNo, int pageSize) {
if (pageSize < 1) {
throw new IllegalArgumentException("`pageSize` 必须大于零");
}
this.pageNo = pageNo;
this.pageSize = pageSize;
}
/**
* 统计总数量 <br/>
*
* <pre>
* 示例
* public int XxxMapper.count(..);
* </pre>
*
* @param func
* @return
*/
public Paged<T> count(CountFunc<?> func) {
Objects.requireNonNull(func);
this.totalSize = func.exec();
return this;
}
private void adjustPageNo() {
this.pageNo = PaginationHelper.ajustPageNo(pageNo, getStart(), pageNo);
}
/**
* 执行数据查询
*
* <pre>
* 示例:
* public List<Promotion> XxxMapper.findList(..);
* </pre>
*
* @param func
* @return
*/
public Paged<T> find(FindListFunc<T> func) {
Objects.requireNonNull(func);
this.adjustPageNo();
if (totalSize > 0) {
this.list = func.exec(this);
}
return this;
}
/**
* 查询并返回List
*
* @param func
* @return
*/
public List<T> findAndRtn(FindListFunc<T> func) {
this.find(func);
return list;
}
/**
* 消费数据查询
*
* @param f
* @return
*/
public Paged<T> forEach(Consumer<T> action) {
Objects.requireNonNull(action);
for (T t : list) {
action.accept(t);
}
return this;
}
/**
* 消费数据查询
*
* @param f
* @return
*/
public Paged<T> forEach(SyntheticConsumer<T> action) {
Objects.requireNonNull(action);
for (T t : list) {
action.accept(t, this);
}
return this;
}
@FunctionalInterface
public static interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
}
@FunctionalInterface
public static interface SyntheticConsumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t, Paged<T> value);
}
@FunctionalInterface
public static interface CountFunc<T> {
/**
* 数据统计执行
*
* @return
*/
public int exec();
}
@FunctionalInterface
public static interface FindListFunc<T> {
/**
* 数据列表查询
*
* @param _this
* @return
*/
public List<T> exec(Paged<T> page);
}
@FunctionalInterface
public static interface PaginationFunc {
public void load(Pagination page);
}
}
public class PaginationHelper {
/**
* 构造分页对象
*
* @param totalSize 总条数
* @param pageSize 每页条数
* @return
*/
public static Pagination make(int totalSize, int pageNo, int pageSize) {
int totalPage = calcTotalPage(totalSize, pageSize);
return Pagination.newBuilder().setLimit(pageSize).setPn(pageNo).setTotalItem(totalSize)
.setTotalPage(totalPage).build();
}
/**
* 计算总页数
* @param totalSize 总条数
* @param pageSize 每页条数
* @return
*/
private static int calcTotalPage(int totalSize, int pageSize) {
if (pageSize <= 0) {
throw ErrorCode.ILLEGAL_ARGUMENT.throwError("分页pageSize必须大于零");
}
int totalPage = 0;// 总页码
if (totalSize > 0) {
totalPage = totalSize / pageSize;
if (totalSize % pageSize != 0) {
totalPage++;
}
}
return totalPage;
}
public static int ajustPageNo(int pageNo, int totalSize, int pageSize) {
if (pageNo < 2) {
return 1;
} else {
int totalPage = calcTotalPage(totalSize, pageSize);
if (pageNo > totalPage) {
return totalPage;
}
}
return pageNo;
}
public static int calcStart(int pageNo, int pageSize) {
if (pageNo < 1) {
pageNo = 1;
}
return (pageNo - 1) * pageSize;
}
}