mysql - 使用设置标记的 MyBatis xml 更新查询抛出 org.springframework.jdbc.BadSqlGrammarException

标签 mysql mybatis spring-mybatis mybatis-mapper badsqlgrammarexception

在 MyBatis xml 中构建更新查询,但没有成功传递 BadSqlGrammarException

这是我的查询

    <update id="updateRecordingVideoStatus">
        UPDATE
            game_record_metadata
            <set>
                <if test="modifiedVideoStatus = null">
                        status = #{originalVideoStatus}
                </if>
                <if test="modifiedVideoStatus != null">
                        status = #{modifiedVideoStatus}
                </if>
            </set>
        WHERE id = #{gameRecordMetadataId}
        AND game_id = #{gameId}
    </update>

我已经尝试过以下(没有设置标签)但不起作用

                <if test="modifiedVideoStatus = null">
                       SET status = #{originalVideoStatus}
                </if>
                <if test="modifiedVideoStatus != null">
                       SET status = #{modifiedVideoStatus}
                </if>

编辑

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: 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 'WHERE id = 2
        AND game_id = 204' at line 5
### The error may exist in file [/Users/asd/admin-api/build/resources/main/mybatis/rel/game_recording.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: UPDATE             game_record_metadata                                     WHERE id = ?         AND game_id = ?
### Cause: java.sql.SQLSyntaxErrorException: 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 'WHERE id = 2
        AND game_id = 204' at line 5
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 'WHERE id = 2
        AND game_id = 204' at line 5

正确的选择是什么?

提前致谢。

最佳答案

问题是 <if>未选择语句。即 test="..." 中的两个条件是假的。查询结果不正确:

UPDATE game_record_metadata
 WHERE id = #{gameRecordMetadataId}
   AND game_id = #{gameId}

发生这种情况是因为您在 test 中使用了不存在的表列。 :

  • test="modifiedVideoStatus = null"
  • test="modifiedVideoStatus != null"

您应该使用 test 中的列名称先条件而不是传递参数。幸运的是,这个问题有解决方案,但我不确定是否有效:

它们看起来令人困惑且不明显。


我建议您检查originalVideoStatusmodifiedVideoStatus代码中的参数并使用一个通用查询来更新 game_record_metadata.status专栏。

<update id="updateGameRecordMetadataStatus">
    UPDATE game_record_metadata
       SET status = #{newStatus}
     WHERE id = #{gameRecordMetadataId}
       AND game_id = #{gameId}
</update>

关于mysql - 使用设置标记的 MyBatis xml 更新查询抛出 org.springframework.jdbc.BadSqlGrammarException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77148995/

相关文章:

mysql - 如何优化 MySQL 以处理小型数据库,即 < 100mb?

sql - MyBatis 重用查询来获取 count(*)

java - 如何在带有注释的mybatis和oracle中插入时返回ID

php - 通过 PHP 删除记录而不重新加载页面(使用 AJAX)

jquery - 使用来自 ajax 响应的变量作为 json

使用 MyBatis 的 SQL 异常

java - 在 MyBatis 中返回一个列表<MyClass>

mysql - 为什么这个sql在prepare之后就能执行成功呢?

mysql - 如何在 Mybatis 的 select sql 中连接一个数字类型?

mysql - 建立父子关系时更新不起作用