一、背景描述
Springboot项目集成了Mybatis-plus,最近在做一个导出功能,我的详情里面可能会有上好几千条数据,后面的导出需要将详情中的数据导出来,单页查询(pageSize < 500时)很正常,需求是把详情中所有数据全部导出,但是每次导出的时候只能导出500条数据,估计是源码里加了默认限制。
二、问题原因
查看源码:
@Setter
@Accessors(chain = true)
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class PaginationInterceptor extends AbstractSqlParserHandler implements Interceptor {
protected static final Log logger = LogFactory.getLog(PaginationInterceptor.class);
private ISqlParser countSqlParser;
private boolean overflow = false;
private long limit = 500L;
private String dialectType;
private String dialectClazz;
public static String concatOrderBy(String originalSql, IPage> page) {
if (CollectionUtils.isNotEmpty(page.orders())) {
try {
List orderList = page.orders();
Select selectStatement = (Select) CCJSqlParserUtil.parse(originalSql);
if (selectStatement.getSelectBody() instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
List orderByElements = plainSelect.getOrderByElements();
List orderByElementsReturn = addOrderByElements(orderList, orderByElements);
plainSelect.setOrderByElements(orderByElementsReturn);
return plainSelect.toString();
} else if (selectStatement.getSelectBody() instanceof SetOperationList) {
SetOperationList setOperationList = (SetOperationList) selectStatement.getSelectBody();
List orderByElements = setOperationList.getOrderByElements();
List orderByElementsReturn = addOrderByElements(orderList, orderByElements);
setOperationList.setOrderByElements(orderByElementsReturn);
return setOperationList.toString();
} else if (selectStatement.getSelectBody() instanceof WithItem) {
// todo: don't known how to resole
return originalSql;
} else {
return originalSql;
}
} catch (JSQLParserException e) {
logger.warn("failed to concat orderBy from IPage, exception=" + e.getMessage());
}
}
return originalSql;
}
MyBatis-Plus低版本的PaginationInterceptor,定义了默认limit为500。当size超出limit时设置size为limit的值,即当size大于500时,就默认使用 size = 500。
三、解决方案
关于这个问题的解决方案:配置分页插件时重新设置limit的值,源码中有提示 paginationInterceptor.setLimit(-1)表示不受限制:
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setLimit(-1);
return paginationInterceptor;
}
}



