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

相关推荐
冷雨夜中漫步8 分钟前
Claude Code源码分析——Claude Code Agent Loop 详细设计文档
java·开发语言·人工智能·ai
直奔標竿14 分钟前
Java开发者AI转型第二十六课!Spring AI 个人知识库实战(五)——联网搜索增强实战
java·开发语言·人工智能·spring boot·后端·spring
等风来_shy38 分钟前
如何写好一个 Skill
后端
one_love_zfl1 小时前
java面试-微服务组件篇
java·微服务·面试
一只大袋鼠1 小时前
Java进阶:CGLIB动态代理解析
java·开发语言
ailab1 小时前
研发人员如何写好 AI 提示词:从“问问题”到“驱动研发闭环”
后端
环流_1 小时前
HTTP 协议的基本格式
java·网络协议·http
爱滑雪的码农1 小时前
Java基础十三:Java中的继承、重写(Override)与重载(Overload)详解
java·开发语言
ltl1 小时前
【大模型基础设施工程】25:大模型基础设施未来
后端
ltl1 小时前
【大模型基础设施工程】二十四:成本、合规与安全
后端