Mybatis 批量更新遇到的小问题
小问题#
记一个开发过程中因为小细节的遗漏而引发的 "莫名其妙",公司中有个2B(to B)供应链项目,持久层用的是 JPA,这里我就不比较 JPA 和 Mybatis 的优劣了,但 JPA 对于多表关联的系统和后期维护性真的是差。
由于是新功能的开发,查询的功能中需要多字段的条件查询,涉及到多张表的关联操作。我试着用 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
再次调试,这时就可以正常执行了,数据得以更新成功,细节决定成败。

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?