一、使用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>