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

mybatis-plus的saveBatch

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

mybatis-plus的saveBatch

IService.saveBatch

public interface IService {
   
    default boolean saveBatch(Collection entityList) {
        //
        return this.saveBatch(entityList, 1000);
    }
    ...
}

走到了ServiceImpl.saveBatch,最终走到了SqlHelper里面

public class ServiceImpl, T> implements IService {

    public boolean saveBatch(Collection entityList, int batchSize) {
        //"com.bai.Mapper.insert"
        String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);

        return this.executeBatch(entityList, batchSize, (sqlSession, entity) -> {
            //携带真正的插入操作。entity代表着list里的每一个对象
            //此处标记为1,
            sqlSession.insert(sqlStatement, entity);
        });
    }
    protected  boolean executeBatch(Collection list, int batchSize, BiConsumer consumer) {
        return SqlHelper.executeBatch(this.entityClass, this.log, list, batchSize, consumer);
    }

看下SqlHelper

public final class SqlHelper {
    public static  boolean executeBatch(Class entityClass, Log log, Collection list, int batchSize, BiConsumer consumer) {
        Assert.isFalse(batchSize < 1, "batchSize must not be less than one", new Object[0]);
        return !CollectionUtils.isEmpty(list) 
            //执行下面的executeBatch方法
            //此处的consumer标记为2
        && executeBatch(entityClass, log, (sqlSession) -> {
                int size = list.size();
                int i = 1;
                
                //按1000个一组批量
                for(Iterator var6 = list.iterator(); var6.hasNext(); ++i) {
                    E element = var6.next();
                    //执行标记为1的地方
                    consumer.accept(sqlSession, element);
                    if (i % batchSize == 0 || i == size) {
                        // ID预占位
                        sqlSession.flushStatements();
                    }
                }

        });
    }

    public static boolean executeBatch(Class entityClass, Log log, Consumer consumer) {
        try {
            SqlSessionFactory sqlSessionFactory = sqlSessionFactory(entityClass);
            SqlSessionHolder sqlSessionHolder = (SqlSessionHolder)TransactionSynchronizationManager.getResource(sqlSessionFactory);
            boolean transaction = TransactionSynchronizationManager.isSynchronizationActive();
            SqlSession sqlSession;
            if (sqlSessionHolder != null) {
                sqlSession = sqlSessionHolder.getSqlSession();
                sqlSession.commit(!transaction);
            }

            sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
            if (!transaction) {
                log.warn("SqlSession [" + sqlSession + "] Transaction not enabled");
            }

            boolean var7;
            try {
                //执行标记为2的地方
                consumer.accept(sqlSession);
                //所有的都执行完后进行提交
                sqlSession.commit(!transaction);
                var7 = true;
            } catch (Throwable var15) {
                sqlSession.rollback();
                Throwable unwrapped = ExceptionUtil.unwrapThrowable(var15);
                if (unwrapped instanceof PersistenceException) {
                    MyBatisExceptionTranslator myBatisExceptionTranslator = new MyBatisExceptionTranslator(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true);
                    Throwable throwable = myBatisExceptionTranslator.translateExceptionIfPossible((PersistenceException)unwrapped);
                    if (throwable != null) {
                        throw throwable;
                    }
                }

                throw ExceptionUtils.mpe(unwrapped);
            } finally {
                sqlSession.close();
            }

            return var7;
        } catch (Throwable var17) {
            throw var17;
        }
    }  

可以看到在批量插入时,如果使用saveBatch是N条sql语句,一次提交。
如果是for循环save,那是N条sql语句,N次提交。效率不可同日而语。
当然最好的应该是insert into value(1),(2),(3),(4)…这种一条sql语句的。

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

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

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