[英]MySQLSyntaxErrorException: Mybatis :Bulk update query using foreach
Following is a bulk update query i have written using foreach
and update
statement. 以下是我使用
foreach
和update
语句编写的批量更新查询。 In this apart from update_time
any other parameter can be null. 除了
update_time
之外,其他任何参数都可以为null。 This query is supposed to take a list of objects as parameter and return void. 该查询应该将对象列表作为参数并返回void。
<update id="bulkUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";" >
UPDATE
<include refid="tableName" />
<set>
update_time=#{item.updateTime}
<if test="item.testFlg != null">, test_flg=#{item.testFlg}</if>
<if test="item.DueDate != null">, due_date=#{item.DueDate}</if>
<if test="item.versionId != null">, version_id=#{item.versionId}</if>
</set>
WHERE
<include refid="tableName" />.order_id=#{item.orderId}
</foreach>
</update>
After Debugging i found that the query is getting all the required not null values correctly. 调试后,我发现查询正在正确获取所有必需的非null值。 However I am getting this error that is driving me crazy.
但是,我遇到了这个错误,这使我发疯。
The error occurred while setting parameters\r\n### SQL: UPDATE glb_order_tbl SET update_time=? , complete_due_date=? , version_id=? WHERE glb_order_tbl .order_id=? ; UPDATE glb_order_tbl SET update_time=? , complete_due_date=? , version_id=? WHERE glb_order_tbl .order_id=? \r\n### 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 \n glb_order_tbl \n SET update_time='2015-02-24 13:01:48.608'\n ' at line 24\n; 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 \n glb_order_tbl \n SET update_time='2015-02-24 13:01:48.608'\n ' at line 24"
It seems some sort of syntactical error that i can't find out. 我似乎找不到某种语法错误。
I am using Java+spring+MyBatis+MySql
我正在使用
Java+spring+MyBatis+MySql
Updated Query and error.Please note that parameters being set(inside set block) might have changed 更新了查询和错误。请注意,正在设置的参数(在设置块内)可能已更改
Thanks in advance. 提前致谢。
The problem here is that you set the open
, close
and separator
incorrectly in your foreach
closure. 这里的问题是,你设置的
open
, close
和separator
在正确foreach
关闭。
In your sql, it appends (
at the beginning of the whole sql, and )
at the end, and separate each update
sql with ,
. 在您的sql中,它会在末尾附加
(
在整个sql的开头和)
,并使用,
分隔每个update
sql。 The generated sql surely has the syntactical error. 生成的sql肯定有语法错误。
Change it as follows, and it should work. 如下进行更改,它应该可以工作。
<foreach collection="list" item="item" index="index" separator=";">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.