1. 程式人生 > >mybatis批量更新報錯 MySQLSyntaxErrorException: You have an error in your SQL syntax; check the near 'UPDAT

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

問題

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