[英]MySQLSyntaxErrorException: Mybatis :Bulk update query using foreach
以下是我使用foreach
和update
語句編寫的批量更新查詢。 除了update_time
之外,其他任何參數都可以為null。 該查詢應該將對象列表作為參數並返回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>
調試后,我發現查詢正在正確獲取所有必需的非null值。 但是,我遇到了這個錯誤,這使我發瘋。
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"
我似乎找不到某種語法錯誤。
我正在使用Java+spring+MyBatis+MySql
更新了查詢和錯誤。請注意,正在設置的參數(在設置塊內)可能已更改
提前致謝。
這里的問題是,你設置的open
, close
和separator
在正確foreach
關閉。
在您的sql中,它會在末尾附加(
在整個sql的開頭和)
,並使用,
分隔每個update
sql。 生成的sql肯定有語法錯誤。
如下進行更改,它應該可以工作。
<foreach collection="list" item="item" index="index" separator=";">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.