Mybatis中trim标签的理解

trim标签的作用是可以在trim标签内容前面加上prefix定义的前缀,也可以在其后加上suffix定义的后缀;
prefixOverrides和suffixOverrides可以把包含内容的首部或者尾部某些内容覆盖,也就是忽略掉。



    
    
  • 1
  • 2
  • 3
  • 4
  • 5
select * from user 
  <trim prefix="WHERE" prefixOverrides="AND |OR">
    <if test="name != null and name.length()>0"> AND name=#{name}</if>
    <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
  </trim>
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
假如说name和gender的值都不为null的话,打印的SQL为:

    
    
  • 1
  • 2
select * from user where name = 'xx' and gender = 'xx'
    
    
  • 1

where后不存在and,这是因为prefixOverrides="AND |OR"代表去掉第一个and或者是or。

2、代码为:

    
    
  • 1
  • 2
update user
  <trim prefix="set" suffixOverrides="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} , </if>
    <if test="gender != null and gender.length()>0"> AND gender=#{gender} ,  </if>
  </trim>
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
假如说name和gender的值都不为null的话,打印的SQL为:

    
    
  • 1
  • 2
update user set name='xx' , gender='xx' where id='x'
    
    
  • 1

可以参考第一个例子理解。

3、代码为:

    
    
  • 1
  • 2
<insert id="save" parameterType="NoticeEntity">
        INSERT INTO S_NOTICE 
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ID,
            <if test="title != null">TITLE,</if>
            <if test="content != null">CONTENT,</if>
            <if test="noticeStatus != null">NOTICE_STATUS,</if>
            <if test="createdBy != null">CREATED_BY,</if>
            CREATED_TS,
            <if test="lastUpdBy != null">LAST_UPD_BY,</if>
            LAST_UPD_TS,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            SYS_GUID(),
            <if test="title != null">#{title,jdbcType=VARCHAR},</if>
            <if test="content != null">#{content,jdbcType=VARCHAR},</if>
            <if test="noticeStatus != null">#{noticeStatus,jdbcType=VARCHAR},</if>
            <if test="createdBy != null">#{createdBy,jdbcType=VARCHAR},</if>
            systimestamp,
            <if test="lastUpdBy != null">#{lastUpdBy,jdbcType=VARCHAR},</if>
            systimestamp,
        </trim>
    </insert>
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

这个标签主要用于动态拼接sql语句,特别适用于不确定几个查询条件的时候使用

trim标签的作用是可以在trim标签内容前面加上prefix定义的前缀,也可以在其后加上suffix定义的后缀;
prefixOverrides和suffixOverrides可以把包含内容的首部或者尾部某些内容覆盖,也就是忽略掉。



  
  
  • 1
  • 2
  • 3
  • 4
  • 5
select * from user 
  <trim prefix="WHERE" prefixOverrides="AND |OR">
    <if test="name != null and name.length()>0"> AND name=#{name}</if>
    <if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
  </trim>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
假如说name和gender的值都不为null的话,打印的SQL为:

  
  
  • 1
  • 2
select * from user where name = 'xx' and gender = 'xx'
  
  
  • 1

where后不存在and,这是因为prefixOverrides="AND |OR"代表去掉第一个and或者是or。

2、代码为:

  
  
  • 1
  • 2
update user
  <trim prefix="set" suffixOverrides="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} , </if>
    <if test="gender != null and gender.length()>0"> AND gender=#{gender} ,  </if>
  </trim>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
假如说name和gender的值都不为null的话,打印的SQL为:

  
  
  • 1
  • 2
update user set name='xx' , gender='xx' where id='x'
  
  
  • 1

可以参考第一个例子理解。

3、代码为:

  
  
  • 1
  • 2
<insert id="save" parameterType="NoticeEntity">
        INSERT INTO S_NOTICE 
        <trim prefix="(" suffix=")" suffixOverrides=",">
            ID,
            <if test="title != null">TITLE,</if>
            <if test="content != null">CONTENT,</if>
            <if test="noticeStatus != null">NOTICE_STATUS,</if>
            <if test="createdBy != null">CREATED_BY,</if>
            CREATED_TS,
            <if test="lastUpdBy != null">LAST_UPD_BY,</if>
            LAST_UPD_TS,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            SYS_GUID(),
            <if test="title != null">#{title,jdbcType=VARCHAR},</if>
            <if test="content != null">#{content,jdbcType=VARCHAR},</if>
            <if test="noticeStatus != null">#{noticeStatus,jdbcType=VARCHAR},</if>
            <if test="createdBy != null">#{createdBy,jdbcType=VARCHAR},</if>
            systimestamp,
            <if test="lastUpdBy != null">#{lastUpdBy,jdbcType=VARCHAR},</if>
            systimestamp,
        </trim>
    </insert>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

这个标签主要用于动态拼接sql语句,特别适用于不确定几个查询条件的时候使用

猜你喜欢

转载自blog.csdn.net/wonderful_life_mrchi/article/details/80540865