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

Mybatis 最大拼接数2100个字段,超出报错解决

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

Mybatis 最大拼接数2100个字段,超出报错解决

 The incoming request has too many parameters. The server supports a maximum of 2100 parameters

excel 批量导入时 mybatis 动态拼接参数不能超出2100个,超出就抛出这个异常,所以我们需要分段插入,分段根据当时的场景需要每段设置多少条



import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class SqlBatchConfig {

    private final Logger log = LoggerFactory.getLogger(SqlBatchConfig.class);

    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    
    public  void save(String packageName, List list) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
        try {
            //批量插入的数据分批处理
            int batchCount = 500; //每批commit的个数
            int batchIndex = (int) Math.ceil(list.size() / batchCount);// 每批下标
            List tempList = new ArrayList<>(batchCount);
            int start ,stop ;
            for (int i = 0; i < batchIndex ; i++) {
                tempList.clear();
                start = i * batchCount;
                stop = Math.min(i * batchCount + batchCount - 1, list.size() - 1);
                log.info("条数区间:" + start + " - " + stop);
                for (int j = start; j <= stop; j++) {
                    tempList.add(list.get(j));
                }
                sqlSession.insert(packageName, tempList);
                sqlSession.commit();
                sqlSession.clearCache();
                log.info("已经插入" + (stop + 1) + " 条");
            }
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}

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

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

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