返回顶部
首页 > 资讯 > 后端开发 > JAVA >MyBatisPlus 批量添加
  • 206
分享到

MyBatisPlus 批量添加

mybatisjavaspringboot 2023-08-19 10:08:02 206人浏览 泡泡鱼
摘要

文章目录 现状优化效果报错 现状 一般来说,批量插入可以使用 mybatisPlus 中 ServiceImpl 自带的方法 saveBatch 打开 sql 日志,application.yml 添加配置,mapper-lo

现状

一般来说,批量插入可以使用 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 依赖
<dependency>    <groupId>com.baomidougroupId>    <artifactId>mybatis-plus-boot-starterartifactId>    <version>3.5.2version>dependency>
  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 {    @Override    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {        List<AbstractMethod> 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@Configurationpublic class MybatisPlusConfig {       @Bean    public 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;    }            @Bean    public BatchSqlInjector easySqlInjector() {        return new BatchSqlInjector();    }}
  1. 配置 BatcHBaseMapper 继承 BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;import java.util.Collection;public interface BatchBaseMapper<T> extends BaseMapper<T> {        Integer insertBatchSomeColumn(Collection<T> entityList);}
  1. 业务 Mapper 继承 BatchBaseMapper
@Repositorypublic interface ISapCustomerMapper extends BatchBaseMapper<SapCustomerPO> {}
  1. service 创建 createBatch 作为新的批量插入方法
public class SapCustomerServiceImpl extends ServiceImpl<ISapCustomerMapper, SapCustomerPO> {    void createBatch(List<SapCustomerPO> 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 执行效率,数据越多越明细

报错

Packet for query is too large (82,807,536 > 67,108,864). You can change this value on the server by setting the max_allowed_packet’ variable

原因: 一次插入数量太多的数据,超出了 mysql 默认设置

解决办法: 在 service 层限制插入数量

static int batchSize = 10000;public void createBatch(List<TestPO> entityList) {    if (!entityList.isEmpty()) {        int size = entityList.size();        int idxLimit = Math.min(batchSize, size);        int i = 1;        List<TestPO> oneBatchList = new ArrayList<>();        for (Iterator<TestPO> var7 = entityList.iterator(); var7.hasNext(); ++i) {            TestPOelement = var7.next();            oneBatchList.add(element);            if (i == idxLimit) {                baseMapper.insertBatchSomeColumn(oneBatchList);                oneBatchList.clear();                idxLimit = Math.min(idxLimit + batchSize, size);            }        }    }}

来源地址:https://blog.csdn.net/weixin_42555971/article/details/129323292

--结束END--

本文标题: MyBatisPlus 批量添加

本文链接: https://www.lsjlt.com/news/375281.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作