当前位置: 首页 > news >正文

MybatisPlus批量插入(伪批量),增强为真实批量插入

项目基于优秀开源项目:若依

项目背景:项目中牵扯到数据批量导入,为提高性能,先考虑将MybatisPlus伪批量插入增强为真实批量插入

MybatisPlus源码:

MybatisPlus支持批量插入,但是跟踪源码发现底层是将批量插入的数据循环执行了N次单条插入:

IService源码:

    @Transactional(rollbackFor = {Exception.class})default boolean saveBatch(Collection<T> entityList) {return this.saveBatch(entityList, 1000);}boolean saveBatch(Collection<T> entityList, int batchSize);

ServiceImpl源码:

    @Transactional(rollbackFor = {Exception.class})public boolean saveBatch(Collection<T> entityList, int batchSize) {String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);return this.executeBatch(entityList, batchSize, (sqlSession, entity) -> {sqlSession.insert(sqlStatement, entity);});}

底层支持增强改造:

1、创建自定义sql注入类:MySqlInjector.java
public class MySqlInjector extends DefaultSqlInjector {public MySqlInjector() {}@Overridepublic 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;}}
2、创建自定义Mapper    MyBaseMapper.java
public interface MyBaseMapper<T> extends BaseMapper<T> {/*** 批量插入 仅适用于mysql** @param entityList 实体列表* @return 影响行数*/Integer insertBatchSomeColumn(Collection<T> entityList);
}

3、业务Mapper实现自定义的Mapper

@Repository
public interface TestMapper extends MyBaseMapper<Test>
{}
4、业务中批量插入,使用insertBatchSomeColumn函数
insertBatchSomeColumn(list);
5、新建MyBatisPlus配置类,将自定义sql注入   MybatisPlusConfig.java
@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();//指定使用数据库类型interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}@Beanpublic DefaultSqlInjector mySqlInjector() {return new MySqlInjector();}}

在使用默认MybatisPlus配置默认 sessionFactory时,以上即可

此处基于若依,需要将自定义sessionFactory设置为自定义sql注入器

配置文件位置,参考

framwork模块下config包MyBatisConfig

    @Beanpublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");String mapperLocations = env.getProperty("mybatis.mapperLocations");String configLocation = env.getProperty("mybatis.configLocation");typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);VFS.addImplClass(SpringBootVFS.class);final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);sessionFactory.setTypeAliasesPackage(typeAliasesPackage);sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));//自定义批量插入GlobalConfig globalConfig = new GlobalConfig();GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();globalConfig.setDbConfig(dbConfig);globalConfig.setSqlInjector(new MySqlInjector());sessionFactory.setGlobalConfig(globalConfig);return sessionFactory.getObject();}

基于以上,大功告成。快去试试效果吧

http://www.lryc.cn/news/252112.html

相关文章:

  • 【零基础入门Python】Python If Else流程控制
  • 新手零基础学习彩铅画,彩铅快速入门教程合集
  • 线程池的拒绝策略
  • Redis7--基础篇5(管道、发布订阅)
  • Unity中Shader指令优化(编译后指令解析)
  • 单个 Zip 文件体积超过 40GB
  • pandas 基础操作3
  • 开发知识点-Maven包管理工具
  • 104. 二叉树的最大深度
  • JAVA毕业设计113—基于Java+Springboot+Vue的体育馆预约系统(源代码+数据库+12000字论文)
  • 【自动化测试】pytest 用例执行中print日志实时输出
  • 【深度学习】KMeans中自动K值的确认方法
  • github问题解决(持续更新中)
  • 如何创建一个vue工程
  • 50 代码审计-PHP无框架项目SQL注入挖掘技巧
  • 基于Spring、SpringMVC、MyBatis的企业博客网站
  • spring日志输出到elasticsearch
  • 谷歌 Gemini 模型发布计划推迟:无法可靠处理部分非英语沟通
  • Ubuntu显卡及内核更新问题
  • SpringBoot错误处理机制解析
  • 牛客剑指offer刷题模拟篇
  • Locust单机多核压测,以及主从节点的数据通信处理!
  • ERROR: [pool www] please specify user and group other than root
  • 京东商品详情接口在电商行业中的重要性及实时数据获取实现
  • WT2003H MP3语音芯片方案:强大、灵活且易于集成的音频解决方案
  • 机器学习深度学学习分类模型中常用的评价指标总结记录与代码实现说明
  • fastapi 后端项目目录结构 mysql fastapi 数据库操作
  • 研习代码 day47 | 动态规划——子序列问题3
  • L1-017:到底有多二
  • Python多线程使用(二)