mybatis批量插入、mybatis-plus批量插入、mybatis实现insertList、mybatis自定义实现批量插入
创始人
2024-12-13 23:05:30
0

文章目录

  • 一、mybatis新增批量插入
    • 1.1、引入依赖
    • 1.2、自定义通用批量插入Mapper
    • 1.3、把通用方法注册到mybatisplus注入器中
    • 1.4、实现InsertList类
    • 1.5、需要批量插入的dao层继承批量插入Mapper
  • 二、可能遇到的问题
    • 2.1、Invalid bound statement

众所周知,mybatisplus提供的BaseMapper里只有单条插入的方法,没有批量插入的方法,
而在Service层的批量插入并不是真的批量插入,实际上是遍历insert,但也不是一次insert就一次IO,而是到一定数量才会去IO一次,性能不是很差,但也不够好。

怎么才能实现真正的批量插入呢?

这里是mybatisplus官方的演示仓库,可以先去了解一下。

一、mybatis新增批量插入

1.1、引入依赖

本文基于mybatis-plus3.5.0进行讲解,不同的版本写法稍微有点不同。

       com.baomidou      mybatis-plus-boot-starter      3.5.0   

1.2、自定义通用批量插入Mapper

1.新建一个InsertListMapper文件,提供insertList方法,后续需要用到批量插入的dao继承该Mapper即可拥有批量插入接口。

import org.apache.ibatis.annotations.Param; import java.util.List;  /**  * 通用Mapper接口,特殊方法,批量插入,支持批量插入的数据库都可以使用,例如mysql,h2等  *  * @param  不能为空  * @author  */ public interface InsertListMapper {     /**      * 批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等      * 

* 注意: * 1.若实体类中设置了@TableId注解,则需要设置value值 如:@TableId(value = "id") * 2.若建表语句中设置了default, 需要在插入时手动设置默认值,否则存储的是null。 * 如:delete_flag tinyint default 0 comment '删除标志 0-未删除 1-已删除',这种需要在insert的时候设置实体类改字段值。setDeleteFlag(0) *

* * @param recordList * @return */ int insertList(@Param("list") List recordList); }

1.3、把通用方法注册到mybatisplus注入器中

import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.mybatisplus.core.metadata.TableInfo; import org.springframework.stereotype.Component;  import java.util.List;  /**  * 添加Sql注入方法,支持空字段更新  */ @Component public class CustomerSqlInjector extends DefaultSqlInjector {     @Override     public List getMethodList(Class mapperClass, TableInfo tableInfo){         List methodList=super.getMethodList(mapperClass, tableInfo);         //注册自定义方法         //注意:InsertList中的name需要与xxxMapper中的方法名一致,即insertList         methodList.add(new InsertList("insertList"));          return methodList;     } } 

关键的一句在于methodList.add(new InsertList("insertList"));,意为注册一个新的方法叫insertList,具体实现在InsertList类。

1.4、实现InsertList类

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="";          //要插入的字段 即insert into table(要插入的字段) values         StringBuilder insertColumnSql=new StringBuilder();         insertColumnSql.append(tableInfo.getKeyColumn()).append(",");          StringBuilder valueSql=new StringBuilder();         valueSql.append("\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("");          return  String.format(batchInsertSql,tableInfo.getTableName(),insertColumnSql,valueSql);     }  } 

1.5、需要批量插入的dao层继承批量插入Mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.demo.common.mapper.InsertListMapper; import com.demo.entity.User;  /** * @Entity generator.domain.User */ public interface UserMapper extends BaseMapper, InsertListMapper {   } 

以上代码全都放在gitee仓库,详情见:
代码仓库地址

二、可能遇到的问题

2.1、Invalid bound statement

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.demo.mapper.User1Mapper.insertList  	at org.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:235) 	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.(MybatisMapperMethod.java:50) 	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedInvoker$0(MybatisMapperProxy.java:111) 	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) 	at com.baomidou.mybatisplus.core.toolkit.CollectionUtils.computeIfAbsent(CollectionUtils.java:115) 	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedInvoker(MybatisMapperProxy.java:98) 	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89) 	at com.sun.proxy.$Proxy70.insertList(Unknown Source) 	at com.demo.User1Test.test1(User1Test.java:50) 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 	at java.lang.reflect.Method.invoke(Method.java:498) 	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 

解决方法: :https://blog.csdn.net/weixin_49114503/article/details/140645048







参考文章:https://blog.csdn.net/m0_51390969/article/details/134730527
https://blog.csdn.net/m0_63297646/article/details/131843517

相关内容

热门资讯

一分钟带你了解!sohoo辅助... 一分钟带你了解!sohoo辅助(透视)好像有开挂辅助软件是一款可以让一直输的玩家,快速成为一个“必胜...
教程攻略!wepoker免费脚... 教程攻略!wepoker免费脚本(透视)德州之星扫描器(曝光开挂辅助安装);1、这是跨平台的德州之星...
必备教程!wepoker怎么增... 必备教程!wepoker怎么增加运气(透视)起初有开挂辅助安装;wepoker怎么增加运气免费下载原...
玩家必备科技!海盗来了辅助器最... 玩家必备科技!海盗来了辅助器最新(透视)wpk俱乐部怎么透视挂(揭露开挂辅助挂)相信很多朋友都在电脑...
必备攻略!德普之星辅助器怎么用... 必备攻略!德普之星辅助器怎么用(透视)原先有开挂辅助器;必备攻略!德普之星辅助器怎么用(透视)原先有...
科技揭秘!随意玩俱乐部辅助(透... 科技揭秘!随意玩俱乐部辅助(透视)wepoker怎么增加运气(详情开挂辅助安装);随意玩俱乐部辅助免...
总算了解!wepoker分析(... 总算了解!wepoker分析(透视)wepoker私人局规律(普及开挂辅助器);wepoker私人局...
今日公布!aapoker ai... 今日公布!aapoker ai插件(透视)固有有开挂辅助挂;aapoker ai插件免费下载原版,在...
记者发布!hhpoker透视挂... 记者发布!hhpoker透视挂靠谱吗(透视)一直有开挂辅助软件;1、完成hhpoker透视挂靠谱吗透...
如何分辨真伪!福麻圈辅助(透视... 如何分辨真伪!福麻圈辅助(透视)竞技联盟辅助插件(解谜开挂辅助神器);竞技联盟辅助插件软件透明挂作为...