mybatis-plus的insertBatchSomeColumn方法实现批量插入

1、编写SQL注入器

java 复制代码
@Component("batchInsertInjector")
public class BatchInsertInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new InsertBatchSomeColumn());
        return methodList;
    }
}

2、自定义Mapper继承BaseMapper

java 复制代码
public interface AbstractMapper<T>  extends BaseMapper<T> {

    Integer SIZE_LIMIT = 3000;

    /**
     * 批量插入,借助mybatis-plush的injector实现
     * @param entities
     */
    void insertBatchSomeColumn(@Param("list") List<T> entities);

    /**
     * 分段批量插入
     * @param entities
     */
    default void segmentBatchInsert(List<T> entities){
        if (CollectionUtils.isEmpty(entities)) {
            return;
        }
        List<List<T>> partition = Lists.partition(entities, SIZE_LIMIT);
        for (List<T> part : partition) {
            insertBatchSomeColumn(part);
        }
    }
}

3、实体类的mapper继承自定义mapper

java 复制代码
@Mapper
public interface LibraryBusinessInfoMapper extends AbstractMapper<LibraryBusinessInfo> {
}

4、使用批量插入方法

java 复制代码
    @Autowired
    private LibraryBusinessInfoMapper libraryBusinessInfoMapper;
    @Override
    @Transactional
    public int batchAdd(List<AddLibraryInfoReqVo> addLibraryInfoReqVos) {
        List<LibraryInfo> libraryInfos = ReqVo2Entity.INSTANCE.addLibraryInfos2LibraryInfos(addLibraryInfoReqVos);
        libraryInfoMapper.segmentBatchInsert(libraryInfos);
        return libraryInfos.size();
    }
相关推荐
q***787842 分钟前
Spring学习——新建module模块
java·学习·spring
q***11651 小时前
在Nginx上配置并开启WebDAV服务的完整指南
java·运维·nginx
Jaising6661 小时前
MySQL 与 Clickhouse 多数据源切换技术分析
数据库·后端·mybatis
白起那么早1 小时前
我又开发了一款idea插件-ContiNewGenerator
java·后端
装不满的克莱因瓶1 小时前
【Java架构师体系课 | MySQL篇】③ Explain执行计划详解
java·数据库·mysql·架构·优化·索引·explain
王煜苏1 小时前
最新版idea2025 配置docker 打包spring-boot项目到生产服务器全流程,含期间遇到的坑
java·docker·容器
李玮豪Jimmy1 小时前
Day18:二叉树part8(669.修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树)
java·服务器·算法
后端小张2 小时前
【AI 学习】AI Agent 开发进阶:架构、规划、记忆与工具编排
java·人工智能·ai·架构·系统架构·agent·智能体
西岭千秋雪_2 小时前
Kafka客户端整合
java·spring boot·分布式·kafka·linq
leonardee2 小时前
Golang笔记——Interface类型
java·后端