本文介绍: 二、使用mybatisplus批量插入方法。一、代码层面切割好list然后插入。

一、代码层面切割好list然后插入

// package org.apache.commons.collections4; 先将list切成1000条一份
List<List<DeptDO&gt;&gt; p1 = ListUtils.partition(deptList, 1000);
for (List<DeptDO&gt; deptDOS : p1) {
// 1000条一次批量插入
      systemDeptMapper.insertBatch(deptDOS);
}

二、使用mybatisplus批量插入方法

    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     */
    public static <T> boolean saveBatch(Collection<T> entityList) {
        return saveBatch(entityList, IService.DEFAULT_BATCH_SIZE);
    }

    /**
     * 插入(批量)
     *
     * @param entityList 实体对象集合
     * @param batchSize  插入批次数量
     */
    public static <T> boolean saveBatch(Collection<T> entityList, int batchSize) {
        if (CollectionUtils.isEmpty(entityList)) {
            return false;
        }
        Class<T> entityClass = getEntityClass(entityList);
        Class<?> mapperClass = ClassUtils.toClassConfident(getTableInfo(entityClass).getCurrentNamespace());
        String sqlStatement = SqlHelper.getSqlStatement(mapperClass, SqlMethod.INSERT_ONE);
        return SqlHelper.executeBatch(entityClass, LogFactory.getLog(Db.class), entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
    }

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注