基于springboot的项目,在进行批量更新操作时报错,但将打印出来的sql复制到mysql直接执行时,正常执行不报错
报错内容:
Error updating database. 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 `t_chain_source_use_settle` SET SETTLE_STATE = 1
WHERE PERIOD' at line 4
### The error may exist in file [D:\jxd\lulan\lulan-chain\target\classes\com\jxdinfo\hussar\sewageStation\dao\mapping\RawUseSettleMapper.xml]
### The error may involve com.jxdinfo.hussar.sewageStation.dao.RawUseSettleMapper.updateSettleStatu-Inline
### The error occurred while setting parameters
### SQL: update `t_chain_source_use_settle` SET SETTLE_STATE = 1 WHERE PERIOD = ? AND GOODS_CODE = ? AND BATCH = ? ; update `t_chain_source_use_settle` SET SETTLE_STATE = 1 WHERE PERIOD = ? AND GOODS_CODE = ? AND BATCH = ? ;
### 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 `t_chain_source_use_settle` SET SETTLE_STATE = 1
WHERE PERIOD' at line 4
; 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 `t_chain_source_use_settle` SET SETTLE_STATE = 1
WHERE PERIOD' at line 4] with root causejava.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 `t_chain_source_use_settle` SET SETTLE_STATE = 1
WHERE PERIOD' at line 4
原因:
Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行
解决方案:
在连接mysql的url上加 &allowMultiQueries=true
url: jdbc:mysql://ip:端口号/数据库?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC&allowMultiQueries=true
allowMultiQueries=true表示可以在sql语句后携带分号,即可以实现多语句执行
本文解决了一个基于SpringBoot项目的批量更新操作时出现的SQL语法错误问题。问题表现为在MyBatis中执行带有分号的多条SQL语句时出现语法错误,但在MySQL客户端单独执行时却能成功。通过修改数据库连接URL参数允许多条SQL语句的执行解决了该问题。
501

被折叠的 条评论
为什么被折叠?



