首页 > 技术文章 > Mybatis的update使用动态标签

zhsv 2021-09-18 14:05 原文

1.使用set标签

 <update id="update" parameterType="com.bootdo.system.domain.UserDO">
        update sys_user
        <set>
            <if test="username != null">`username` = #{username},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="password != null">`password` = #{password},</if>
            <if test="deptId != null">`dept_id` = #{deptId},</if>
            <if test="email != null">`email` = #{email},</if>
            <if test="mobile != null">`mobile` = #{mobile},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="userIdCreate != null">`user_id_create` = #{userIdCreate},</if>
            <if test="gmtCreate != null">`gmt_create` = #{gmtCreate},</if>
            <if test="gmtModified != null">`gmt_modified` = #{gmtModified},</if>
            <if test="sex != null">`sex` = #{sex},</if>
            <if test="birth != null">`birth` = #{birth},</if>
            <if test="picId != null">`pic_id` = #{picId},</if>
            <if test="liveAddress != null">`live_address` = #{liveAddress},</if>
            <if test="hobby != null">`hobby` = #{hobby},</if>
            <if test="province != null">`province` = #{province},</if>
            <if test="city != null">`city` = #{city},</if>
            <if test="district != null">`district` = #{district}</if>
        </set>
        where user_id = #{userId}
    </update>

2.使用trim方式

<update id="update" parameterType="com.bootdo.system.domain.UserDO">
        update sys_user
        <trim prefix="set" suffixOverrides=",">
            <if test="username != null">`username` = #{username},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="password != null">`password` = #{password},</if>
            <if test="deptId != null">`dept_id` = #{deptId},</if>
            <if test="email != null">`email` = #{email},</if>
            <if test="mobile != null">`mobile` = #{mobile},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="userIdCreate != null">`user_id_create` = #{userIdCreate},</if>
            <if test="gmtCreate != null">`gmt_create` = #{gmtCreate},</if>
            <if test="gmtModified != null">`gmt_modified` = #{gmtModified},</if>
            <if test="sex != null">`sex` = #{sex},</if>
            <if test="birth != null">`birth` = #{birth},</if>
            <if test="picId != null">`pic_id` = #{picId},</if>
            <if test="liveAddress != null">`live_address` = #{liveAddress},</if>
            <if test="hobby != null">`hobby` = #{hobby},</if>
            <if test="province != null">`province` = #{province},</if>
            <if test="city != null">`city` = #{city},</if>
            <if test="district != null">`district` = #{district}</if>
        </trim >
        where user_id = #{userId}
    </update>

 

推荐阅读