mybatis新增处理逗号

        我们有时候考虑到代码复用,新增的时候希望可以选择字段,就需要对字段进行判断,但是字段后面会跟上逗号,如果是最后一个字段为空,那么会多一个逗号。代码片段如下:

    <if test="phone != null and phone != ''">
        #{phone},
    </if>
    <if test="idCard != null and idCard != ''">
        #{idCard}
    </if>

       如果idCard为空,#{phone}的后面就会多一个逗号。我们可以用一个<trim suffixOverrides=","></trim>标签来处理这种情况。完整代码如下:

     <!--新增广告  -->
  <insert id="addAd"  parameterType="com.xddManagement.entity.xdd.XddAdposition">
    insert into xdd_adposition (<trim suffixOverrides=",">
    <if test="advertisingId != null and advertisingId != ''">
     advertisingId,
    </if>
    <if test="advertisingNo != null and advertisingNo != ''">
     advertisingNo,
    </if>
    <if test="content != null and content != ''">
     content,
    </if>
    <if test="type != null and type != ''">
     type,
    </if>
    <if test="period != null and period != ''">
     period,
    </if>
    <if test="status != null and status != ''">
     status,
    </if>
    <if test="createTime != null and createTime != ''">
     createTime,
    </if>
    <if test="putTime != null and putTime != ''">
     putTime,
    </if>
    <if test="outTime != null and outTime != ''">
     outTime,
    </if>
    <if test="expireTime != null and expireTime != ''">
     expireTime,
    </if>
    <if test="remark != null and remark != ''">
     remark,
    </if>
    <if test="title != null and title != ''">
     title,
    </if>
    <if test="compName != null and compName != ''">
     compName,
    </if>
    <if test="compAddress != null and compAddress != ''">
     compAddress,
    </if>
    <if test="person != null and person != ''">
     person,
    </if>
    <if test="phone != null and phone != ''">
     phone,
    </if>
    <if test="idCard != null and idCard != ''">
     idCard
    </if></trim>
    )values(<trim suffixOverrides=",">
    <if test="advertisingId != null and advertisingId != ''">
     #{advertisingId},
    </if>
    <if test="advertisingNo != null and advertisingNo != ''">
     #{advertisingNo},
    </if>
    <if test="content != null and content != ''">
     #{content},
    </if>
    <if test="type != null and type != ''">
     #{type},
    </if>
    <if test="period != null and period != ''">
     #{period},
    </if>
    <if test="status != null and status != ''">
     #{status},
    </if>
    <if test="createTime != null and createTime != ''">
        #{createTime},
    </if>
    <if test="putTime != null and putTime != ''">
        #{putTime},
    </if>
    <if test="outTime != null and outTime != ''">
        #{outTime},
    </if>
    <if test="expireTime != null and expireTime != ''">
        #{expireTime},
    </if>
    <if test="remark != null and remark != ''">
        #{remark},
    </if>
    <if test="title != null and title != ''">
        #{title},
    </if>
    <if test="compName != null and compName != ''">
        #{compName},
    </if>
    <if test="compAddress != null and compAddress != ''">
        #{compAddress},
    </if>
    <if test="person != null and person != ''">
        #{person},
    </if>
    <if test="phone != null and phone != ''">
        #{phone},
    </if>
    <if test="idCard != null and idCard != ''">
        #{idCard}
    </if></trim>)   
  </insert>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值