MyBatisPlus 批量添加
创始人
2024-05-28 14:42:47

文章目录

  • 现状
  • 优化
  • 效果

现状

一般来说,批量插入可以使用 MyBatisPlus 中 ServiceImpl 自带的方法 saveBatch

在这里插入图片描述
打开 sql 日志,application.yml 添加配置,mapper-locations 配置 mapper 路径

mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志mapper-locations: classpath*:mapper/**/*Mapper.xml

可以发现插入是在同一个 SqlSession,但并不是理想中的批量插入
在这里插入图片描述

它的插入算法我没有细究,但从日志观察可以看出它的插入条数是无序的,如果可以一次插入全部,效率应该更高
 

优化

MyBatisPlus 预留了 insertBatchSomeColumn 方法,可以实现批量插入,下面介绍一下如何配置

  1. MyBatisPlus 依赖
com.baomidoumybatis-plus-boot-starter3.5.2

  1. 新建 Sql 注射器 BatchSqlInjector
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import java.util.List;public class BatchSqlInjector extends DefaultSqlInjector {@Overridepublic List getMethodList(Class mapperClass, TableInfo tableInfo) {List methodList = super.getMethodList(mapperClass, tableInfo);methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));return methodList;}}
  1. MybatisPlusConfig 配置 BatchSqlInjector Bean,可忽略这里配置的分页插件
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {/*** 分页插件** @return*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);pageInterceptor.setMaxLimit(500L);pageInterceptor.setOptimizeJoin(true);interceptor.addInnerInterceptor(pageInterceptor);return interceptor;}/*** 批量插入** @return*/@Beanpublic BatchSqlInjector easySqlInjector() {return new BatchSqlInjector();}
}
  1. 配置 BatchBaseMapper 继承 BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;public interface BatchBaseMapper extends BaseMapper {/*** 批量插入 仅适用于mysql** @param entityList 实体列表* @return 影响行数*/Integer insertBatchSomeColumn(Collection entityList);
}
  1. 业务 Mapper 继承 BatchBaseMapper
@Repository
public interface ISapCustomerMapper extends BatchBaseMapper {}
  1. service 创建 createBatch 作为新的批量插入方法
public class SapCustomerServiceImpl extends ServiceImpl {void createBatch(List entityList) {if(!entityList.isEmpty()){baseMapper.insertBatchSomeColumn(entityList);}}}

注意:不建议直接用 mapper 的 insertBatchSomeColumn 方法,因为当 entityList 为空时会报错
其实就是 INSERT INTO 表名(字段1,字段2,字段3) VALUES 后面为空

NestedRuntimeException:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 1

 

效果

3600 条数据
优化前:2058 毫秒
优化后:1293 毫秒

15000 条数据
优化前:8958 毫秒
优化后:2037 毫秒

可以看出,数据越多,优化效果越明细

通过这次测试发现,打开 sql 日志后,会明细拖慢 sql 执行效率,数据越多越明细

相关内容

热门资讯

新产品新场景新业态,梵净抹茶“...   6月1日,参加2026抹茶大会的嘉宾走进“中国抹茶之都”铜仁市,实地探访从茶园到餐桌的抹茶全产业...
未来十年“再造新广州”,6万亿... 界面新闻记者 | 张熹珑2030年地区生产总值达4.5万亿元左右,2035年达到6万亿元水平;“十五...
“剑指美西方”,中方团队列出了... 【文/观察者网 阮佳琪】 自特朗普首届任期对华发起贸易战以来,美国联合西方盟友持续收紧对华高端技术准...
老人这些行为,看似“奇奇怪怪”... 疯狂网购、极度抠门、整日怀旧……一些老人“奇奇怪怪”的行为,可能不单是节俭、固执、“闲得慌”,而是隐...
中消协:解压不是宣泄暴力,情绪... 近期,“娜塔莎”玩偶在年轻群体中迅速走红,被许多人当作一种别样的“情绪出口”。从最初网友对其进行的“...