trim标签属性
- prefix:拼接后续sql时需要加上的前缀
- suffix:拼接后续sql时需要加上的后缀
- prefixOverrides:完成拼接sql后,需要消除的前缀
- suffixOverrides:完成拼接sql后,需要消除的后缀
常用场景
拼接select语句时,在where后面需要消除可能存在的and时,就需要再where标签中使用trim标签和prefixOverrides属性。
<select id="selectHaoMan6List" parameterType="com.xxx.processingtampermonkey.domain.HaoMan6" resultType="com.xxx.processingtampermonkey.domain.HaoMan6">
<include refid="selectHaoMan6Vo" />
<where>
<trim prefixOverrides="and">
<if test="id != null and id != 0">and id = #{id}</if>
<if test="mhmc != null and mhmc != ''">and mhmc = #{mhmc}</if>
<if test="zjmc != null and zjmc != ''">and zjmc = #{zjmc}</if>
<if test="imgurl != null and imgurl != ''">and imgurl like concat('%',#{imgurl},'%')</if>
<if test="imgpx != null and imgpx != 0">and imgpx = #{imgpx}</if>
<if test="sfxz != null and sfxz != ''">and sfxz = #{sfxz}</if>
and delflag = '0'
</trim>
</where>
</select>
拼接insert语句时,表名后需要增加前缀->"(“和后缀->”)“和消除末尾不需要的后缀->”,"。然后需要前缀->“values (“和后缀->”)“和消除末尾不需要的后缀->”,”
<insert id="insertHaoMan6" parameterType="com.xxx.processingtampermonkey.domain.HaoMan6" useGeneratedKeys="true" keyProperty="id">
insert into haoman6
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null and id != 0">id,</if>
<if test="mhmc != null and mhmc != ''">mhmc,</if>
<if test="zjmc != null and zjmc != ''">zjmc,</if>
<if test="imgurl != null and imgurl != ''">imgurl,</if>
<if test="imgpx != null and imgpx != 0">imgpx,</if>
<if test="sfxz != null and sfxz != ''">sfxz,</if>
</trim>
<trim prefix=" values (" suffix=")" suffixOverrides=",">
<if test="id != null and id != 0">#{id},</if>
<if test="mhmc != null and mhmc != ''">#{mhmc},</if>
<if test="zjmc != null and zjmc != ''">#{zjmc},</if>
<if test="imgurl != null and imgurl != ''">#{imgurl},</if>
<if test="imgpx != null and imgpx != 0">#{imgpx},</if>
<if test="sfxz != null and sfxz != ''">#{sfxz},</if>
</trim>
</insert>
拼接update语句时,在set后面需要消除末尾不需要的后缀->","
<update id="updateHaoMan6" parameterType="com.xxx.processingtampermonkey.domain.HaoMan6">
update haoman6
<set>
<trim suffixOverrides=",">
<if test="mhmc != null and mhmc != ''">mhmc = #{mhmc},</if>
<if test="zjmc != null and zjmc != ''">zjmc = #{zjmc},</if>
<if test="imgurl != null and imgurl != ''">imgurl = #{imgurl},</if>
<if test="imgpx != null and imgpx != 0">imgpx = #{imgpx},</if>
<if test="sfxz != null and sfxz != ''">sfxz = #{sfxz},</if>
</trim>
</set>
where id = #{id}
</update>