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

相关推荐
wand codemonkey21 小时前
SpringbootWeb【入门】+MySQL【安装】+【DataDrip安装 】+【连接MySQL】
java·mysql·mybatis
Mahir081 天前
Spring 循环依赖深度解密:从问题本质到三级缓存源码级解析
java·后端·spring·缓存·面试·循环依赖·三级缓存
RyFit1 天前
SpringAI 常见问题及解决方案大全
java·ai
石山代码1 天前
C++ 内存分区 堆区
java·开发语言·c++
绝知此事1 天前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
无风听海1 天前
C# 隐式转换深度解析
java·开发语言·c#
一只大袋鼠1 天前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
德思特1 天前
从 Dify 配置页理解 RAG 的重要参数
java·人工智能·llm·dify·rag
YOU OU1 天前
Spring IoC&DI
java·数据库·spring
один but you1 天前
从可变参数到 emplace:现代 C++ 性能优化的核心组合
java·开发语言