繁体   English   中英

MySQLSyntaxErrorException:Mybatis:使用foreach批量更新查询

[英]MySQLSyntaxErrorException: Mybatis :Bulk update query using foreach

以下是我使用foreachupdate语句编写的批量更新查询。 除了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

更新了查询和错误。请注意,正在设置的参数(在设置块内)可能已更改

提前致谢。

这里的问题是,你设置的opencloseseparator在正确foreach关闭。

在您的sql中,它会在末尾附加(在整个sql的开头和) ,并使用,分隔每个update sql。 生成的sql肯定有语法错误。

如下进行更改,它应该可以工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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