自己实现mybatisplus的批量插入

java 复制代码
package net.data.exchange.config.mybatisplus;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import java.util.Collections;

public class InsertList extends AbstractMethod {

    public InsertList(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        KeyGenerator keyGenerator = new NoKeyGenerator();
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, getBatchInsertSql(tableInfo,modelClass), Collections.class);
        return this.addInsertMappedStatement(mapperClass,modelClass,"insertList",sqlSource,keyGenerator,null,null);

    }

    private String getBatchInsertSql(TableInfo tableInfo, Class<?> modelClass){
        String batchInsertSql="<script> INSERT INTO %s (%s) values %s</script>";

        //要插入的字段 即insert into table(要插入的字段) values
        StringBuilder insertColumnSql=new StringBuilder();
        insertColumnSql.append(tableInfo.getKeyColumn()).append(",");

        StringBuilder valueSql=new StringBuilder();
        valueSql.append("<foreach collection='list' item='item' open='(' separator='),(' close=')' >\n");
        valueSql.append("#{item."+tableInfo.getKeyProperty()+"},");

        tableInfo.getFieldList().forEach(x->{
            insertColumnSql.append(x.getColumn()).append(",");
            valueSql.append("#{item."+x.getProperty()+"},");
        });

        insertColumnSql.delete(insertColumnSql.length()-1,insertColumnSql.length());
        valueSql.delete(valueSql.length()-1,valueSql.length());
        valueSql.append("</foreach>");

        return  String.format(batchInsertSql,tableInfo.getTableName(),insertColumnSql,valueSql);
    }

}
java 复制代码
package net.data.exchange.config.mybatisplus;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;

import java.util.List;

public class MySqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new InsertList("insertList"));
        return methodList;
    }
}
java 复制代码
package net.data.exchange.config.mybatisplus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


    @Bean
    public MySqlInjector sqlInjector() {
        return new MySqlInjector();
    }

}
java 复制代码
package net.data.exchange.config.mybatisplus;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.Collection;

public interface MyBaseMapper<T> extends BaseMapper<T> {
    int insertList(Collection<T> entityList);
}

到这里只需要mapper继承我们的MyBaseMapper调用inserList就可以实现批量插入了

相关推荐
知守观1 分钟前
feign-core 就在依赖树里,运行时却找不到类:一次 provided scope 引发的启动失败排查
java·spring cloud·maven
她的男孩2 分钟前
企业接口照样拦得住:独立 Flyway、@RequiresFeature 与离线许可证
java·spring boot·后端
十年Java程序媛3 分钟前
Lambda 与函数式接口|别只会复制 ()->{},底层规则和坑一次性讲清
java·spring boot·后端
yunwei374 分钟前
eBPF 入门开发实践教程一:Hello World,基本框架和开发流程
linux·后端·性能优化
全栈Agent 小李7 分钟前
【无标题】
前端·后端·agent·ai编程·全栈·cursor·mcp
高频因子挖掘机9 分钟前
历史 K 线突然少一天?用交易日历、停牌信息和数据校验逐步排查
后端·github·api
Nozokime10 分钟前
ThreadLocal 在流式 Agent 里静默失效:LangChain4j 轨迹埋点的零侵入方案
java
小宋102114 分钟前
OpenTelemetry GenAI可观测性实战:串起模型、工具、Token与错误
java·人工智能·算法·贪心算法
m0_5873830033 分钟前
工业场景设备维修维护实战技巧 全流程标准化落地与常见问题排查指南
java·spring·小程序·架构·需求分析
行者全栈架构师34 分钟前
从 55% 到 6%:一个快餐营养规划器的算法迭代实录
后端·算法·架构