Mybatis 使用<update>中<set>标签的简明例子

注:xxx为某某某的意思

        update xxx
        <set>
            <if test="xx !=null and xx !='' ">
                SQL1,
            </if>
            <if test="x !=null and x !='' ">
                SQL2,
            </if>
        </set>
        where SQL3

如果<if test="xx !=null and xx !='' ">成立,<if test="x !=null and x !='' ">不成立

则最终SQL语句为 update xxx set SQL1 where SQL3;

<set>会自动去掉SQL1,尾部的逗号,并加上set

如果均成立

则最终SQL语句为 update xxx set SQL1, SQL2 where SQL3;

实际例子

UPDATE table_name SET column1 = value1, column2 = value2  WHERE id=36;

逗号<set>里判断后结果不能为空,否则SQL语句语法错误

对于<set>,同<where>一样可以用<trim>代替

        update xxx
        <trim prefix="set" suffixOverrides=",">
            <if test="xx !=null and xx !='' ">
                SQL1,
            </if>
            <if test="x !=null and x !='' ">
                SQL2,
            </if>
        </trim>
        where SQL3

完整例子

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.xxx.xxxMapper">
    <update id="updateXxxBySet"
            parameterType="com.xxx.xxx.xxx">


        update xxx
        <set>
            <if test="xx !=null and xx !='' ">
                SQL1,
            </if>
            <if test="x !=null and x !='' ">
                SQL2,
            </if>
        </set>
        where SQL3


    </update>

    <update id="updateXxxBySetTRIM"
            parameterType="com.xxx.xxx.xxx">


        update xxx
        <trim prefix="set" suffixOverrides=",">

            <if test="xx !=null and xx !='' ">
                SQL1,
            </if>
            <if test="x !=null and x !='' ">
                SQL2,
            </if>
        </trim>
        where SQL3


    </update>
</mapper>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值