基于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语句后携带分号,即可以实现多语句执行