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