简体   繁体   中英

MySQLSyntaxErrorException: Mybatis :Bulk update query using foreach

Following is a bulk update query i have written using foreach and update statement. In this apart from update_time any other parameter can be null. This query is supposed to take a list of objects as parameter and return 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. 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

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.

In your sql, it appends ( at the beginning of the whole sql, and ) at the end, and separate each update sql with , . The generated sql surely has the syntactical error.

Change it as follows, and it should work.

<foreach collection="list" item="item" index="index" separator=";">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM