在 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
中的列名称先条件而不是传递参数。幸运的是,这个问题有解决方案,但我不确定是否有效:
它们看起来令人困惑且不明显。
我建议您检查originalVideoStatus
和modifiedVideoStatus
代码中的参数并使用一个通用查询来更新 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/