Mybatis中<trim>标签的用法和常见场景。prefix、suffix、prefixOverrides、suffixOverrides属性详解

trim标签属性

  1. prefix:拼接后续sql时需要加上的前缀
  2. suffix:拼接后续sql时需要加上的后缀
  3. prefixOverrides:完成拼接sql后,需要消除的前缀
  4. 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>
  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值