码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis update set 多个字段

时间:2017-03-19 17:16:18      阅读:1418      评论:0      收藏:0      [点我收藏+]

标签:删掉   class   param   span   time   报错   efi   arc   mobile   

<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> 

 

mybatis update set 多个字段

标签:删掉   class   param   span   time   报错   efi   arc   mobile   

原文地址:http://www.cnblogs.com/Alwaysbecoding/p/6580674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!