我有以下声明,我需要更新数据库中的条目:
<update id="setAsDefault" >
START TRANSACTION;
UPDATE cms_wall SET is_default = FALSE WHERE owner = #{ownerId} AND is_default= TRUE;
UPDATE cms_wall SET is_default = TRUE WHERE brand = #{brandId};
COMMIT;
</update>
这两个陈述都是单独的。此外,如果我在mysql中手动输入它们,它们会合并。当我尝试从ibatis运行时,我收到以下错误:
### Error updating database. 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 cms_wall SET is_default = FALSE WHERE owner = '123_test_user_' at line 2
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: START TRANSACTION; UPDATE cms_wall SET is_default = FALSE WHERE owner= ? AND is_default= TRUE; UPDATE cms_wall SET is_default = TRUE WHERE brand = ?; COMMIT;
### 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 cms_wall SET is_default = FALSE WHERE owner = '123_test_user_' at line 2
答案 0 :(得分:1)
使用if
测试语句更清晰:
<update id="setAsDefault" >
START TRANSACTION;
<if test="ownerId != null">
UPDATE cms_wall SET is_default = FALSE WHERE owner = #{ownerId} AND is_default= TRUE;
</if>
<if test="brandId != null">
UPDATE cms_wall SET is_default = TRUE WHERE brand = #{brandId};
</if>
COMMIT;
</update>
答案 1 :(得分:0)
这应该有效:
UPDATE cms_wall SET is_default = IF(owner = #{ownerId} AND is_default=TRUE,FALSE,IF(brand = #{brandId},TRUE,is_default));