自己实现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就可以实现批量插入了

相关推荐
周末吃鱼16 小时前
研发快速使用JMeter
java·jmeter
用户6174332731016 小时前
MySQL 表的类 Git 版本控制
后端
pany16 小时前
程序员近十年新年愿望,都有哪些变化?
前端·后端·程序员
杨宁山16 小时前
Java 解析 CDR 文件并计算图形面积的完整方案(支持 MultipartFile / 网络文件)@杨宁山
后端
朱昆鹏16 小时前
IDEA Claude Code or Codex GUI 插件【开源自荐】
前端·后端·github
HashTang16 小时前
买了专业屏只当普通屏用?解锁 BenQ RD280U 的“隐藏”开发者模式
前端·javascript·后端
明月_清风17 小时前
从"请求地狱"到"请求天堂":alovajs 如何用 20+ 高级特性拯救前端开发者
前端·后端
掘金者阿豪17 小时前
如何解决 "Required request body is missing" 错误:深度解析与解决方案
后端
William_cl17 小时前
ASP.NET Core 视图组件:从入门到避坑,UI 复用的终极方案
后端·ui·asp.net