mybatis动态sql中的trim标签的使用

MyBatis动态SQL中trim标签的使用

事实上trim标签有点类似于replace效果。

  trim 属性

  prefix:前缀覆盖并增加其内容

  suffix:后缀覆盖并增加其内容

  prefixOverrides:前缀判断的条件(去掉第一个指定的条件)

  suffixOverrides:后缀判断的条件(去掉最后一个指定的条件)

例子:

select * from user 

  <trim prefix="WHERE" prefixoverride="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>

假如说name和gender的值都不为null的话打印的SQL为:select * from user where    name = 'xx' and gender = 'xx'

上面两个属性的意思如下:

  prefix:前缀      

  prefixoverride:去掉第一个and或者是or

update user

  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">

    <if test="name != null and name.length()>0"> name=#{name} , </if>

    <if test="gender != null and gender.length()>0"> gender=#{gender} ,  </if>

  </trim>

假如说name和gender的值都不为null的话打印的SQL为:update user set name='xx' , gender='xx'     where id='x'

       suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)

  suffix:后缀

项目实例:

<insert id="insertSelective" parameterType="com.wanda.crs.standard.entity.OrderContact" >
    insert into CRS_ORDER_CONTACT
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="idOrderContact != null" >
        ID_ORDER_CONTACT,
      </if>
      <if test="idOrder != null" >
        ID_ORDER,
      </if>
      <if test="mobile != null" >
        MOBILE,
      </if>
      <if test="email != null" >
        EMAIL,
      </if>
      <if test="contactName != null" >
        CONTACT_NAME,
      </if>
      <if test="idCard != null" >
        ID_CARD,
      </if>
      <if test="isSms != null" >
        IS_SMS,
      </if>
      <if test="business != null" >
        BUSINESS,
      </if>
      <if test="remark != null" >
        REMARK,
      </if>
      <if test="isDel != null" >
        IS_DEL,
      </if>
      <if test="addUser != null" >
        ADD_USER,
      </if>
      <if test="tmAdd != null" >
        TM_ADD,
      </if>
      <if test="updateUser != null" >
        UPDATE_USER,
      </if>
      <if test="tmUpdate != null" >
        TM_UPDATE,
      </if>
      <if test="takeTicketCode != null" >
        TAKE_TICKET_CODE,
      </if>
      <if test="openid != null" >
        OPENID,
      </if>      
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="idOrderContact != null" >
        #{idOrderContact,jdbcType=VARCHAR},
      </if>
      <if test="idOrder != null" >
        #{idOrder,jdbcType=VARCHAR},
      </if>
      <if test="mobile != null" >
        #{mobile,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="contactName != null" >
        #{contactName,jdbcType=VARCHAR},
      </if>
      <if test="idCard != null" >
        #{idCard,jdbcType=VARCHAR},
      </if>
      <if test="isSms != null" >
        #{isSms,jdbcType=DECIMAL},
      </if>
      <if test="business != null" >
        #{business,jdbcType=VARCHAR},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="isDel != null" >
        #{isDel,jdbcType=DECIMAL},
      </if>
      <if test="addUser != null" >
        #{addUser,jdbcType=VARCHAR},
      </if>
      <if test="tmAdd != null" >
        #{tmAdd,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="tmUpdate != null" >
        #{tmUpdate,jdbcType=TIMESTAMP},
      </if>
      <if test="takeTicketCode != null" >
        #{takeTicketCode,jdbcType=VARCHAR},
      </if>
      <if test="openid != null" >
        #{openid,jdbcType=VARCHAR},
      </if>      
    </trim>
  </insert>

 

posted @ 2017-06-26 20:43  十月围城小童鞋  阅读(161)  评论(0编辑  收藏  举报