栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus分页查询默认最多查询500条记录的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus分页查询默认最多查询500条记录的问题

一、背景描述

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;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890578.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号