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();
    }
相关推荐
亓才孓10 小时前
[Maven]Maven基础
java·maven
hello 早上好10 小时前
05_Java 类加载过程
java·开发语言
echoVic10 小时前
多模型支持的架构设计:如何集成 10+ AI 模型
java·javascript
橙露10 小时前
Java并发编程进阶:线程池原理、参数配置与死锁避免实战
java·开发语言
echoVic10 小时前
AI Agent 安全权限设计:blade-code 的 5 种权限模式与三级控制
java·javascript
PPPPickup10 小时前
easymall---图片上传以及图片展示
java
历程里程碑10 小时前
Linux 库
java·linux·运维·服务器·数据结构·c++·算法
Wpa.wk10 小时前
接口自动化 - 接口鉴权处理常用方法
java·运维·测试工具·自动化·接口自动化
Pluchon10 小时前
硅基计划4.0 简单模拟实现AVL树&红黑树
java·数据结构·算法
2501_9160088910 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone