1.Mybatis-plus配置类,通过拦截器实现分页
@Configuration
public class MyBatisConfiguration extends BaseAdminController {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
List<ISqlParser> sqlParserList = new ArrayList<>();
// 添加租户sql解析链,如果是开发业务系统,可以去掉这个配置,不去也可以。
TenantSqlParser tenantSqlParser = new TenantSqlParser() {
};
tenantSqlParser.setTenantHandler(tenantHandler());
sqlParserList.add(tenantSqlParser);
// 设置sql解析链
paginationInterceptor.setSqlParserList(sqlParserList);
// 设置过滤器链
paginationInterceptor.setSqlParserFilter(sqlParserFilter());
return paginationInterceptor;
}
}
2.subList查询结果分页
private void getSelectResIPage(PageDutySelectParam param, IPage<PageDutySelectRes> selectResIPage) {
//手动组装分页方法
List<PageDutySelectRes> pageDutySelectRes = getManualPage(param, selectResIPage, getCopyFilterList);
selectResIPage.setRecords(pageDutySelectRes);
selectResIPage.setPages(param.getPageNumber());
selectResIPage.setSize(param.getPageSize());
selectResIPage.setTotal(getCopyFilterList.size());
}
private List<PageDutySelectRes> getManualPage(PageDutySelectParam param, IPage<PageDutySelectRes> selectResIPage, List<PageDutySelectRes> getCopyFilterList) {
int start = (param.getPageNumber() - 1) * param.getPageSize();
int end = param.getPageNumber() * param.getPageSize();
//看看会不会越界
if (start > getCopyFilterList.size() - 1) {
selectResIPage.setRecords(new ArrayList<>());
}
if (end > getCopyFilterList.size() - 1) {
end = getCopyFilterList.size();
}
//手动分页设置序号
for (int i = 0; i < getCopyFilterList.size(); i++) {
getCopyFilterList.get(i).setNum(i+1);
}
List<PageDutySelectRes> pageDutySelectRes = getCopyFilterList.subList(start, end);
return pageDutySelectRes;
}