mybatis批量更新报错 MySQLSyntaxErrorException: You have an error in your SQL syntax; check the near 'UPDAT

转自:https://blog.csdn.net/qq_25838503/article/details/52400072?locationNum=14&fps=1

问题

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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

解决方案

用mybatic 批量更新必须加上参数&allowMultiQueries=true,如下

jdbc:mysql://192.168.68.23:3306/smi_am?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true;

这参数意思是允许多个查询。

其中有业务涉及到批量update问题,我用mybatic foreach实现批量update,但sql一直没问题,但是偏偏报以下奇葩问题:

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 `am_counpons_details`
	         SET `cd_custId` = '555' 
	         WHERE ' at line 6
### The error may involve com.smi.am.dao.AmCounponsDetailsMapper.batchUpdatePackageIdByCouponsIds-Inline
### The error occurred while setting parameters
### SQL: update `am_counpons_details`           SET `cd_custId` = ?            WHERE `cd_detailId` = ?            ;    update `am_counpons_details`           SET `cd_custId` = ?            WHERE `cd_detailId` = ?            ;
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 `am_counpons_details`
	         SET `cd_custId` = '555' 
	         WHERE ' at line 6
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 `am_counpons_details`
	         SET `cd_custId` = '555' 
	         WHERE ' at line 6
	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:231)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
	at com.sun.proxy.$Proxy87.update(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:295)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:59)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
	at com.sun.proxy.$Proxy119.batchUpdatePackageIdByCouponsIds(Unknown Source)
	at com.smi.am.service.RedisTest.testBatchUpdate(RedisTest.java:59)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606

猜你喜欢

转载自blog.csdn.net/yueguanyun/article/details/82631144