Mybatis 批量更新遇到的小问题

小问题

记一个开发过程中因为小细节的遗漏而引发的 "莫名其妙",公司中有个2B(to B)供应链项目,持久层用的是 JPA,这里我就不吐槽 JPA 了,这种 SQL 嵌入在代码里的方式真的不够简洁。

由于是新功能的开发,查询的功能中需要多字段的条件查询,涉及到多张表的关联操作。我试着用 JPA 来写,发现写的很头疼,于是干脆来个改造,将 Mybatis 引入到了项目中,自己掌控 SQL 的感觉还是蛮好的,而且 SQL 和业务代码分离。

报错了

查询,插入,删除,更新这些功能都还好,但是在一次需要批量更新的时候报错了,提示我 SQL语法错误。

Mapper接口的代码如下

@Repository
public interface BasCustomerReservationOrderItemMapper {
     void UpdateBatchByIds(@Param("list") List<BasCustomerReservationOrderItem> basCustomerReservationOrderItemList);
}

SQL 映射 xml 文件内容如下

<update id="UpdateBatchByIds">
  <foreach collection="list" item="record" index="index" open="" close="" separator=";">
    update bas_customer_reservation_order_item
    <set>
      <if test="record.artno != null">
        artno = #{record.artno,jdbcType=VARCHAR},
      </if>
      <if test="record.quantity != null">
        quantity = #{record.quantity,jdbcType=DECIMAL},
      </if>
      <if test="record.sort != null">
        sort = #{record.sort,jdbcType=INTEGER},
      </if>
      <if test="record.unit != null">
        unit = #{record.unit,jdbcType=VARCHAR},
      </if>
      <if test="record.createBy != null">
        create_by = #{record.createBy,jdbcType=VARCHAR},
      </if>
      <if test="record.createDate != null">
        create_date = #{record.createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateBy != null">
        update_by = #{record.updateBy,jdbcType=VARCHAR},
      </if>
      <if test="record.updateDate != null">
        update_date = #{record.updateDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{record.id,jdbcType=BIGINT}
  </foreach>
</update>

这个批量更新语句很简单,但是执行的时候确总是报语法错误,报错信息如下

### SQL: update bas_customer_reservation_order_item        SET artno = ?,                             quantity = ?,                             sort = ?,                             unit = ?,                                               update_by = ?,                             update_date = ?        where id = ?      ;        update bas_customer_reservation_order_item        SET artno = ?,                             quantity = ?,                             sort = ?,                             unit = ?,                                               update_by = ?,                             update_date = ?        where id = ?
### Cause: java.sql.SQLSyntaxErrorException: 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 'update bas_customer_reservation_order_item
       SET artno = '100014',
        ' at line 22
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 'update bas_customer_reservation_order_item
       SET artno = '100014',
        ' at line 22

字段类型,中英文符号,……,我全部检查了一遍都没发现错误,按照提示的line 22那里反复检查也确实没有错误呀。

原来如此

报错信息误导了我,导致我一直纠结在 SQL 语法那块,后来突然智慧之光一闪,单个的查询更新都没问题,只是这个批量处理的时候有问题,是不是不支持批量的呢?

通过查询相关文档后发现,原来如此

Mybatis 映射文件中的 sql 语句默认是不支持以" ; " 结尾的,也就是不支持多条 sql 语句的执行。如果要支持这种语句,需要在连接 mysql 的 url 上加 &allowMultiQueries=true 这个才可以执行。

所以只需要在application.yml的数据源连接配置的地方加上&allowMultiQueries=true即可

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

再次调试,这时就可以正常执行了,数据得以更新成功,细节决定成败。

原文地址:https://www.cnblogs.com/jajian/p/13949968.html