mybits更新update之set标签用法

  • 2017年5月4日
  • Mysql

一、使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。
没有使用if标签时,如果有一个参数为null,都会导致错误,如下示例:

<update id="updateById" parameterType="com.longlonggo.entity.Permission">
    UPDATE conf_user t
    <set> 
        <if test="userName != null">
            t.user_name = #{userName},
        </if>
        <if test="userUrl != null">
            t.user_url = #{userUrl},
        </if>
        <if test="userLogo != null">
            t.user_logo = #{userLogo},
        </if>
        <if test="userDescription != null">
            t.user_description = #{userDescription},
        </if>
        <if test="sort != null">
            t.sort = #{sort},
        </if>
         <if test="isEnabled != null">
            t.is_enabled = #{isEnabled},
        </if>
    </set>
    where t.user_id = #{userId}
</update>

二、当然也可以这么写,注意set标签的第一句,主键等于本身,这样也可以

<update id="updateById" parameterType="com.longlonggo.entity.Permission">
    UPDATE conf_user t
    <set> 
        t.user_id = t.user_id
        <if test="userName != null">
            ,t.user_name = #{userName}
        </if>
        <if test="userUrl != null">
            ,t.user_url = #{userUrl}
        </if>
        <if test="userLogo != null">
            ,t.user_logo = #{userLogo}
        </if>
        <if test="userDescription != null">
            ,t.user_description = #{userDescription}
        </if>
        <if test="sort != null">
            ,t.sort = #{sort}
        </if>
         <if test="isEnabled != null">
            ,t.is_enabled = #{isEnabled}
        </if>
    </set>
    where t.user_id = #{userId}
</update>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注