mybatis-plus分页查询默认最多查询500条记录的问题
一、背景描述Springboot项目集成了Mybatis-plus,最近在做一个导出功能,我的详情里面可能会有上好几千条数据,后面的导出需要将详情中的数据导出来,单页查询(pageSize < 500时)很正常,需求是把详情中所有数据全部导出,但是每次导出的时候只能导出500条数据,估计是源码里加了默认限制。二、问题原因查看源码:@Setter@Accessors(chain = true)
·
一、背景描述
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);
/**
* COUNT SQL 解析
*/
private ISqlParser countSqlParser;
/**
* 溢出总页数,设置第一页
*/
private boolean overflow = false;
/**
* 单页限制 500 条,小于 0 如 -1 不受限制
*/
private long limit = 500L;
/**
* 方言类型
*/
private String dialectType;
/**
* 方言实现类<br>
* 注意!实现 com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect 接口的子类
*/
private String dialectClazz;
/**
* 查询SQL拼接Order By
*
* @param originalSql 需要拼接的SQL
* @param page page对象
* @return ignore
*/
public static String concatOrderBy(String originalSql, IPage<?> page) {
if (CollectionUtils.isNotEmpty(page.orders())) {
try {
List<OrderItem> orderList = page.orders();
Select selectStatement = (Select) CCJSqlParserUtil.parse(originalSql);
if (selectStatement.getSelectBody() instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
List<OrderByElement> orderByElementsReturn = addOrderByElements(orderList, orderByElements);
plainSelect.setOrderByElements(orderByElementsReturn);
return plainSelect.toString();
} else if (selectStatement.getSelectBody() instanceof SetOperationList) {
SetOperationList setOperationList = (SetOperationList) selectStatement.getSelectBody();
List<OrderByElement> orderByElements = setOperationList.getOrderByElements();
List<OrderByElement> 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 {
/**
* 分页插件
*
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setLimit(-1);
return paginationInterceptor;
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)