背景:提供的insertBatch是假批量,重复的IO连接与断开效率极低,提供了insertBatchSomeColumn真批量需要自己手动配置

直接上代码:

public interface MyMapper<T> extends BaseMapper<T> {/*** 默认批次提交数量*/int DEFAULT_BATCH_SIZE = 1000;/*** 批量新增数据,自选字段 insert. 自动按每批1000插入数据库* 此填充不会填充 FieldFill.UPDATE 的字段。* 注意数据库默认更新的字段也需要手工设置** @param entityList 数据* @return 插入条数*/@Transactional(rollbackFor = Exception.class)default int insertBatch(List<T> entityList) {return this.insertBatchSomeColumn(entityList, DEFAULT_BATCH_SIZE);}/*** 批量新增数据,自选字段 insert* 不会分批插入,需要分批请调用方法insertBatch或者 insertBatchSomeColumn(List<T> entityList, int size)* 此填充不会填充 FieldFill.UPDATE 的字段。* 注意数据库默认更新的字段也需要手工设置** @param entityList 数据* @return 插入条数*/int insertBatchSomeColumn(List<T> entityList);/*** 分批插入。每次插入* @param entityList 原实体对象* @param size 分批大小* @return 总插入记录*/@Transactional(rollbackFor = Exception.class)default int insertBatchSomeColumn(List<T> entityList, int size) {if (CollUtil.isEmpty(entityList)) {return 0;}List<List<T>> split = CollUtil.split(entityList, size);return split.stream().mapToInt(this::insertBatchSomeColumn).sum();}
}

有使用工具类,可以引入以下依赖

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.3</version>
</dependency>

以下的操作也需要,要不可能会有其他bug:

public class MySqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass) {List<AbstractMethod> methodList = super.getMethodList(mapperClass);// 例: 不要指定了 update 填充的字段methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));return methodList;}
}
@Configuration
public class MybatisPlusConfig {/*** 自定义内置选装件* @return*/@Beanpublic MySqlInjector sqlInjector() {return new MySqlInjector();}
}

Mybatis-Plus批量新增相关推荐

  1. mybatis高级查询,批量新增

    review sql脚本 实体类 sql watch out mapper mapper test 之前的比较分散,自己用... sql脚本 -- auto-generated definition ...

  2. mybatis批量新增和批量更新的效率对比

    今天,为了更多了解下,mybatis批量新增和批量更新在simple/batch模式 + MySQL的rewriteBatchedStatements下效率有什么区别,做了一次实验. 实验结果,让人意 ...

  3. 基于ruoyi+vue+elementUI实现列表,新增,附件上传,tab+springBoot+mybatis+oracle序列+批量新增

    基于ruoyi+vue+elementUI实现列表,新增,附件上传,tab+springBoot+mybatis+oracle序列+批量新增 页面效果 列表页面 新增页面 详情页面 代码实现 列表+新 ...

  4. Mybatis Plus重写批量新增和批量删除

    批量新增: import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus. ...

  5. MyBatis + Oracle 实现批量新增和批量修改

    MyBatis + Oracle 实现批量新增(基于序列化自增长主键) mapper 接口 void batchInsert(List<ASingleProject> list); map ...

  6. oracle批量新增字段工具,mybatis 中oracle 批量新增三种方法

    第一种 < insert  id =" insert_table "  parameterClass ="java.util.List" > ins ...

  7. c mysql批量添加数据类型_mybatis学习之路----mysql批量新增数据

    原文:https://blog.csdn.net/xu1916659422/article/details/77971867 接下来两节要探讨的是批量插入和批量更新,因为这两种操作在企业中也经常用到. ...

  8. mybatis-sqlserver批量新增返回id

    mybatis-SqlServer批量新增返回id 遇到的问题 解决思路 代码 遇到的问题 公司最近接到项目需要使用SqlServer,在做SQL兼容的时候遇到问题.批量新增数据时只返回的第一条记录的 ...

  9. MyBatisPlus批量新增或修改执行器

    @Component public class ExecutorBatch {@Autowiredprivate SqlSessionTemplate sqlSessionTemplate;/*** ...

  10. mybatis mysql 批量更新_mysql批量update更新,mybatis中批量更新操作

    在日常开发中,有时候会遇到批量更新操作,这时候最普通的写法就是循环遍历,然后一条一条地进行update操作.但是不管是在服务端进行遍历,还是在sql代码中进行遍历,都很耗费资源,而且性能比较差,容易造 ...

最新文章

  1. 两个时间计算毫秒在线_SPL 的日期时间函数(下)
  2. 百度计算生物研究登上Nature子刊!将3D结构引入分子表征,结果超越斯坦福MIT,已落地制药领域...
  3. c++ 多重背包状态转移方程_背包问题之零钱兑换
  4. 滚动图片广告_张韶涵霸屏兴发广场,户外LED大屏广告:投放价值在哪?
  5. c#跨线程操作控件(有UI操作)|及多线程操作
  6. java if else过多_Spring Boot中如何干掉过多的if else!
  7. 原来搞单片机也可以面向对象
  8. C++将地址转换为字符串
  9. dropbox pac规则_来自Dropbox的Zulip聊天,Linux Foundation报告,FCC规则以及更多新闻
  10. Java之static的内容
  11. js/jquery判断浏览器的方法小结
  12. Go 网络 TCP Client (一)
  13. 如何安装IDEA主题
  14. Unity3D C#数学系列之求点到直线的距离
  15. App拉起:h5打开app指定页面
  16. unity3d UI粒子特效裁剪
  17. 绕口令 - 专项练习
  18. 你及格了吗?史上最难云原生冷知识大挑战真题解析
  19. 在win7系统下使用TortoiseGit(乌龟git)简单操作Git@OSC
  20. AutoCAD哪个版本好

热门文章

  1. echarts实现颜色渐变
  2. C语言中 \0 代表什么?
  3. 50个MySql语句
  4. static关键字分析(含解析图)
  5. oracle的12012,Oracle 18.3 ORA-12012 ORA-20001
  6. 哪些因素决定了CRM客户管理系统的价格?
  7. QQ动态表情包如何制作 堪比沙漠骆驼gif
  8. opencv-python Shi-Tomasi角点检测和特征追踪
  9. 最后一个自己_拔剑-浆糊的传说_新浪博客
  10. 有意思的六度分割理论