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();
    }
相关推荐
程序猿DD18 小时前
JUnit 5 中的 @ClassTemplate 实战指南
java·后端
爱吃山竹的大肚肚18 小时前
EasyPOI 大数据导出
java·linux·windows
panzer_maus19 小时前
归并排序的简单介绍
java·数据结构·算法
Smartdaili China19 小时前
掌握Java网页抓取:技术与示例完整指南
java·网络·学习·指南·网页·住宅ip·爬虫api
程序员游老板19 小时前
基于SpringBoot3_vue3_MybatisPlus_Mysql_Maven的社区养老系统/养老院管理系统
java·spring boot·mysql·毕业设计·软件工程·信息与通信·毕设
小二·20 小时前
MyBatis基础入门《十五》分布式事务实战:Seata + MyBatis 实现跨服务数据一致性
分布式·wpf·mybatis
福尔摩斯张20 小时前
C++核心特性精讲:从C语言痛点出发,掌握现代C++编程精髓(超详细)
java·linux·c语言·数据结构·c++·驱动开发·算法
小二·20 小时前
MyBatis基础入门《十四》多租户架构实战:基于 MyBatis 实现 SaaS 系统的动态数据隔离
数据库·架构·mybatis
@淡 定20 小时前
Spring中@Autowired注解的实现原理
java·后端·spring