分割list 批量插入数据指定条数数据

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

复制代码
// package org.apache.commons.collections4; 先将list切成1000条一份
List<List<DeptDO>> p1 = ListUtils.partition(deptList, 1000);
for (List<DeptDO> 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));
    }
相关推荐
camellias_13 小时前
【无标题】
java·tomcat
咸鱼2.014 小时前
【java入门到放弃】需要背诵
java·开发语言
椰猫子14 小时前
Java:异常(exception)
java·开发语言
win x15 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
星晨雪海15 小时前
基于 @Resource 的支付 Service 多实现类完整示例
java·开发语言
阿维的博客日记15 小时前
什么是逃逸分析
java·juc
Ricky_Theseus16 小时前
C++右值引用
java·开发语言·c++
Rick199316 小时前
Java内存参数解析
java·开发语言·jvm
我是大猴子16 小时前
Spring代理类为何依赖注入失效?
java·后端·spring
勿忘,瞬间16 小时前
多线程之进阶修炼
java·开发语言