com.jeesuite.mybatis.plugin.pagination.PageExecutor Maven / Gradle / Ivy
package com.jeesuite.mybatis.plugin.pagination;
import java.util.ArrayList;
import java.util.List;
import com.jeesuite.common.model.Page;
import com.jeesuite.common.model.PageParams;
public class PageExecutor {
private static ThreadLocal pageParamsHolder = new ThreadLocal<>();
public static interface PageDataLoader {
List load();
}
public static interface ConvertPageDataLoader extends PageDataLoader{
V convert(E e);
}
@SuppressWarnings("unchecked")
private static Page doPagination(PageParams pageParams,PageDataLoader dataLoader ){
try {
pageParamsHolder.set(pageParams);
List list = dataLoader.load();
return (Page) list.get(0);
} finally {
pageParamsHolder.remove();
}
}
public static Page pagination(PageParams pageParams,PageDataLoader dataLoader ){
return doPagination(pageParams, dataLoader);
}
public static Page pagination(PageParams pageParams,ConvertPageDataLoader dataLoader ){
Page page = doPagination(pageParams, dataLoader);
List convertDatas = new ArrayList<>(page.getData().size());
for (E e : page.getData()) {
convertDatas.add(dataLoader.convert(e));
}
return new Page<>(pageParams, page.getTotal(), convertDatas);
}
public static PageParams getPageParams(){
return pageParamsHolder.get();
}
public static void clearPageParams(){
pageParamsHolder.remove();
}
}