Mybatis动态更新数据
原理同上一篇的 Mybatis动态插入数据(使用trim标签) ,控制同一张表,但传入的参数不固定,操作表的字段不固定,就要用到mybatis动态更新,普通的<if test=" ">
的方法会出现多余的字符。
方法一:使用set标签
<update id="updateMessage" parameterType="com.sf.ccsp.member.client.request.MessageReq" >
update cx_customer_message
<set>
<if test='messageClassify != null and messageClassify != "" '>
MESSAGEE_CLASSIFY = #{messageClassify, jdbcType=VARCHAR},
</if>
<if test='messageCode != null and messageCode != "" '>
MESSAGE_CODE = #{messageCode, jdbcType=VARCHAR},
</if>
<if test='messageContent != null and messageContent != "" '>
MESSAGE_CONTENT = #{messageContent, jdbcType=VARCHAR},
</if>
</set>
where ID = #{id, jdbcType=VARCHAR}
and MEMBERID = #{memberId, jdbcType=VARCHAR}
</update>
方法二:使用trim标签
<update id="updateMessage" parameterType="com.sf.ccsp.member.client.request.MessageReq" >
update cx_customer_message
<trim prefix="set" suffixOverrides=",">
<if test='messageClassify != null and messageClassify != "" '>
MESSAGEE_CLASSIFY = #{messageClassify, jdbcType=VARCHAR},
</if>
<if test='messageCode != null and messageCode != "" '>
MESSAGE_CODE = #{messageCode, jdbcType=VARCHAR},
</if>
<if test='messageContent != null and messageContent != "" '>
MESSAGE_CONTENT = #{messageContent, jdbcType=VARCHAR},
</if>
</trim>
where ID = #{id, jdbcType=VARCHAR}
and MEMBERID = #{memberId, jdbcType=VARCHAR}
</update>