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();
    }
相关推荐
学IT的周星星3 小时前
Maven 项目和 Maven Web 项目的异同点
java·maven
摇滚侠3 小时前
Spring Boot3零基础教程,函数式 Web 新特性,笔记51
java·spring boot·笔记
mustfeng3 小时前
VCS & Verdi 2023安装
java·服务器·前端
Geoffrey4 小时前
maven中的pom详述
java
练习时长一年4 小时前
jdk动态代理实现
java·开发语言
爬山算法4 小时前
Redis(83)Redis的缓存击穿是什么?
java·redis·缓存
Elieal4 小时前
IDEA使用教程
java·ide·intellij-idea
执笔论英雄4 小时前
【大模型推理】ScheduleBatch 学习
java·spring boot·学习
CRMEB系统商城4 小时前
【新版发布】标准版PHP v5.6.4正式版,优化部分用户体验
java·大数据·小程序·php·ux