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();
    }
相关推荐
w_zero_one2 分钟前
链表(2)
java·数据结构·链表
白杨尚青10 分钟前
C++入门篇(十一):string(中)——容量与增删改查:一篇吃透所有常用接口(万字详解)
java·开发语言·c++
SimonKing20 分钟前
SSE、WebSocket 连接丢 Redis 里?那可踩大坑了!
java·后端·程序员
倔强的石头10625 分钟前
【Linux指南】动静态库系列(九):动态库如何进入进程地址空间:从磁盘 .so 到共享内存映射
java·linux·服务器
TeamDev31 分钟前
JxBrowser 9.5.2 版本发布啦!
java·前端·windows·chromium·混合应用·jxbrowser·浏览器控件
鬼手点金33 分钟前
Claude Code示范案例-进阶学习路径
java·服务器·前端·学习·计算机视觉·前向传播
浪子明X34 分钟前
用 CMake Presets 固定跨平台构建:从本地开发到 CI 的依赖一致性实践
java·spring·ci/cd
苏supper1 小时前
记一次Nacos鉴权报错unknown user,403排查|SecurityProxy源码,fastjson2扩展包缺失
java·后端
\光辉岁月/1 小时前
6.mybatisplus学习-BaseMapper、Service、常用注解
java·mybatis·mybatisplus
微信开发api1 小时前
基于WTAPI构建社群运营平台:自动拉群与会话承接链路设计
java·大数据·网络·数据库·微信·自动化