我想知道如何使用 if 语句,如下所示的情况
<update id="update"
parameterType="com.MyClass">
UPDATE
Board SET Status = 1
<where>
<if test="A != null and A.length() > 0">AND A = ${A}</if>
</where>
</update>
当 A!=null 且 A.length((> 0 时,此语句有效。
但是如果 A==null,则更新整行集 1,因为没有 where 条件。
如果有适当的条件,有什么方法可以更新,否则跳过或忽略?
谢谢
<update id="update"
parameterType="com.MyClass">
UPDATE
Board SET Status = 1
<where>
<choose>
<when test="A != null and A.length() > 0">
AND A = ${A}
</when>
<otherwise>
AND 0 = 1
</otherwise>
</choose>
</where>
</update>
条件 0 = 1 时,where 失败且未完成任何更新
<update id="update"
parameterType="com.MyClass">
UPDATE
Board SET Status = 1
<where>
<![CDATA[<if test="A != null and A.length() > 0">AND A = ${A}</if>]]>
</where>
</update>
尝试CDATA会很好