Mybatis动态更新数据

Mybatis动态更新数据

原理同上一篇的 Mybatis动态插入数据(使用trim标签) ,控制同一张表,但传入的参数不固定,操作表的字段不固定,就要用到mybatis动态更新,普通的的方法会出现多余的字符。

方法一:使用set标签

 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} 

方法二:使用trim标签

id="updateMessage"  parameterType="com.sf.ccsp.member.client.request.MessageReq" >
     update cx_customer_message
     "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>    
       
     where ID = #{id, jdbcType=VARCHAR}
     and MEMBERID = #{memberId, jdbcType=VARCHAR} 

你可能感兴趣的:(mybatis)