MyBatis update set 多个字段
<update id="updateCustomer" parameterType="com.entrym.domain.Customer"> UPDATE customer set <if test="name!=null">name=#{name,jdbcType=VARCHAR},</if> <if test="role!=null">role=#{role,jdbcType=VARCHAR},</if> <if test="userId != null">user_id = #{userId,jdbcType=INTEGER},</if> <if test="qq != null">qq = #{qq,jdbcType=VARCHAR},</if> <if test="mobile != null">mobile = #{mobile,jdbcType=VARCHAR}</if> WHERE id =#{id,jdbcType=BIGINT}
如果上面的mobile字段为null,执行下面的SQL语句
UPDATE customer set name=?,role=?,userId=?,qq=?, where id=?
where 前面有逗号“,”就会报错
使用trim可以删掉最后字段的逗号“,”
set已被包含在trim中,所以不用重复写了:
<update id="updateCustomer" parameterType="com.entrym.domain.Customer"> UPDATE customer <trim prefix="set" suffixOverrides=","> <if test="claimTime!=null">claim_time=#{claimTime,jdbcType=VARCHAR},</if> <if test="claimState!=null">claim_state=#{claimState,jdbcType=INTEGER},</if> <if test="name!=null">name=#{name,jdbcType=VARCHAR},</if> <if test="role!=null">role=#{role,jdbcType=VARCHAR},</if> <if test="platformAccount!=null">platform_account=#{platformAccount,jdbcType=VARCHAR},</if> <if test="collaborateTime!=null">collaborate_time=#{collaborateTime,jdbcType=VARCHAR},</if> <if test="collaborateState!=null">collaborate_state=#{collaborateState,jdbcType=INTEGER},</if> <if test="userId != null">user_id = #{userId,jdbcType=INTEGER},</if> <if test="qq != null">qq = #{qq,jdbcType=VARCHAR},</if> <if test="mobile != null">mobile = #{mobile,jdbcType=VARCHAR}</if> </trim> WHERE id =#{id,jdbcType=BIGINT} </update>
转义字符:
< 小于号 <
> 大于号 >
& 和 &
' 单引号 ’
" 双引号 "
-------------------------------------充足的睡眠、均衡饮食和适当的运动是健康生活的三个鼎足----------------------------------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步