如何在mybatis上使用update语句

vmpqdwk3  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(430)

我想知道如何使用像下面这样的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>

这句话适用于=null和a.length()>0。
但如果a==null,则更新整行集合1,因为不存在where条件。
如果有合适的条件,是否有方法更新,否则跳过或忽略?
谢谢

neekobn8

neekobn81#

<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时,其中失败且未进行任何更新

yvfmudvl

yvfmudvl2#

<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就好了

相关问题