您的位置:首页 > 其它

Mybatis动态更新数据

2017-02-05 15:24 411 查看

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis