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 分钟前
SpringBoot 基础入门
java·spring boot·spring
风吹迎面入袖凉39 分钟前
【Redis】Redisson的可重入锁原理
java·redis
w61001046643 分钟前
cka-2026-ConfigMap
java·linux·cka·configmap
语戚1 小时前
力扣 968. 监控二叉树 —— 贪心 & 树形 DP 双解法递归 + 非递归全解(Java 实现)
java·算法·leetcode·贪心算法·动态规划·力扣·
quxuexi2 小时前
网络通信安全与可靠传输:从加密到认证,从状态码到可靠传输
java·安全·web
hrhcode2 小时前
【java工程师快速上手go】二.Go进阶特性
java·golang·go
小碗羊肉4 小时前
【从零开始学Java | 第三十一篇下】Stream流
java·开发语言
❀͜͡傀儡师5 小时前
Spring AI Alibaba vs. AgentScope:两个阿里AI框架,如何选择?
java·人工智能·spring
aq55356005 小时前
Laravel10.x重磅升级,新特性一览
android·java·开发语言