【问题标题】:MySQLSyntaxErrorException: Mybatis :Bulk update query using foreachMySQLSyntaxErrorException: Mybatis : 使用 foreach 批量更新查询
【发布时间】:2015-04-24 17:51:57
【问题描述】:

以下是我使用foreachupdate 语句编写的批量更新查询。在此,除了update_time,任何其他参数都可以为空。 此查询应该将对象列表作为参数并返回 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>

调试后我发现查询正确地获取了所有必需的非空值。但是我收到了这个让我发疯的错误。

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

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

提前致谢。

【问题讨论】:

    标签: java mysql mybatis


    【解决方案1】:

    这里的问题是您在foreach 闭包中错误地设置了opencloseseparator

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

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

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

    【讨论】:

    • 谢谢@Landys。在正确理解 foreach 语句并根据您的回答修改了我的语句后,我能够弄清楚这一点。但是,尽管在不同的行上,它仍然给出相同的错误。而且我能够通过工作台使用“错误”查询进行多次更新。我想指出我正在使用简单的 mybatis 执行器。我需要使用批处理执行器或其他东西吗..??
    • 你是否在 jdbc url 中启用了多重更新?如果没有,你应该将参数allowMultiQueries=true附加到jdbc的url中。
    猜你喜欢
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多