使用set标签的MyBatis xml更新查询抛出org.springframework.jdbc.BadSqlGrammarException

问题描述 投票:0回答:1

在 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

正确的选择是什么?

提前致谢。

mysql mybatis spring-mybatis mybatis-mapper badsqlgrammarexception
1个回答
0
投票

问题是两个

<if>
语句都没有被选中。也就是说,
test="..."
中的两个条件都是假的。查询结果不正确:

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

发生这种情况是因为您在

test
中使用了不存在的表列:

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

您应该首先使用

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

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


我建议您检查代码中的

modifiedVideoStatus
modifiedVideoStatus
参数,并使用一个通用查询来更新
game_record_metadata.status
列。

<update id="updateGameRecordMetadataStatus">
    UPDATE game_record_metadata
       SET status = #{newStatus}
     WHERE id = #{gameRecordMetadataId}
       AND game_id = #{gameId}
</update>
© www.soinside.com 2019 - 2024. All rights reserved.