MybatisPlus rewriteBatchedStatements=true 批量插入失效,依然是单条插入问题解决

默认情况下,MybatisPlus的批量插入saveBatch方法实际上是for循环单条插入。

如果想要实现真实的批量插入,需要在spring.datasource.url配置中加

rewriteBatchedStatements=true

此时调用saveBatch方法,就会是真实的批量插入:

INSERT INTO T () VALUES (?), (?), (?);

此外,批量更新和批量删除也会是真实的批处理。

需要注意的是:

提交的list中,entity对象的所有字段都必须不为null,否则全部会转换为for循环单条插入。

即使数据库的表设置了默认值,即使表中字段允许为空,提交的entity对象也必须都set值。

可以不set值的情况:

1.如果表是自增主键AUTO_INCREMENT,在entity的id字段上加IdType.AUTO后,可以不setId。

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

2.如果在entity字段上面使用了忽略策略,可以不set相关字段。

    @TableField(insertStrategy = FieldStrategy.IGNORED)
    private Date expireTime;

3.如果在entity字段上面使用了自动填充字段功能,并维护了自动填充接口,可以不set相关字段。


    @TableField(fill = FieldFill.INSERT)
    @Version
    private Integer version;

    @TableField(fill = FieldFill.INSERT)
    private String createUser;

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private String updateUser;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    @TableField(fill = FieldFill.INSERT)
    @TableLogic
    private Long deleteFlag;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mybatis-Plus的批量插入速度可以通过添加参数"rewriteBatchedStatements=true"来提高。这个参数可以让多个insert语句在同一批次内提交,而不是分开多次提交。通常情况下,使用Mybatis-Plus的saveBatch函数进行批量插入会比较慢,特别是在插入上万条数据时。为了解决这个问题,可以通过在jdbc URL后追加参数"rewriteBatchedStatements=true"来优化性能。此外,还需要自定义insert sql,不使用Mybatis-Plus的saveXXX函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mybatis-Plus批量插入数据太慢,使用rewriteBatchedStatements属性优化,堪称速度与激情!](https://blog.csdn.net/qq_41107231/article/details/117898982)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatisPlus 批量插入速度慢问题解决方案](https://blog.csdn.net/qq_41113081/article/details/120641038)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

抓手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值