首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在MyBatis上通过<if>语句使用update语句

如何在MyBatis上通过<if>语句使用update语句
EN

Stack Overflow用户
提问于 2018-01-09 15:07:04
回答 2查看 1.1K关注 0票数 0

我想知道像下面这样的if语句的用法

代码语言:javascript
运行
复制
<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条件。

如果有合适的条件,有没有办法更新,否则跳过或忽略?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-09 16:05:29

代码语言:javascript
运行
复制
<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将失败,并且不执行任何更新

票数 0
EN

Stack Overflow用户

发布于 2018-06-28 16:18:45

<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会很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48162933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档