您的位置:首页 > 数据库

MyBatis动态SQL中trim标签的使用

2017-03-21 16:48 841 查看
My Batis 官方文档 对 动态SQL中使用trim标签的场景及效果介绍比较少。

 

 

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

 

trim 属性

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

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

                prefixOverrides:前缀判断的条件

                suffixOverrides:后缀判断的条件

 

 

比如:

 

 

Java代码  


select b.* from sys_menu b where 1 = 1  
  
<trim suffix="WHERE" suffixOverrides="AND | OR">  
    <if test="id != null and id !='' ">  
        AND b.id =#{id}   
    </if>  
    <if test="name != null">  
        AND b.menu_name like #{name}  
    </if>  
</trim>     

 

 

最终sql打印为:

select b.* from sys_menu b where 1 = 1 AND b.menu_name like '' WHERE

 

从结果可以发现:

 

Java代码  


<trim suffix="WHERE" suffixOverrides="AND | OR">  

 

suffix是针对符合suffixOverrides的SQL语句追加后缀suffix值。

 

 

 

总而言之:

      

      

Java代码  


<trim suffix="WHERE" suffixOverrides="AND | OR">  

 

      And  sqlxxx

 

 最终结果是:

     And  sqlxxx WHERE

 

 

 

我不是很确认这种用法的具体场景,但是,就目前mybatis的动态sql语句来看的话,很多标签都足够用了。

 <insert id="insertTrack" parameterType="com.wanda.gmp.admin.dao.model.clientReview.TrackingForm" useGeneratedKeys="true" keyProperty="id">

    insert into tracking_form

    <trim prefix="(" suffix=")" suffixOverrides="," >

      <if test="id != null" >

        id,

      </if>

      <if test="adminUserId != null" >

        admin_user_id,

      </if>

      <if test="adminUserName != null and adminUserName !=''" >

        admin_user_name,

      </if>

      <if test="userId != null" >

        user_id,

      </if>

      <if test="userAuthId != null" >

        user_auth_id,

      </if>

      <if test="username != null and username !=''" >

        username,

      </if>

      <if test="requirementNum != null" >

        requirement_num,

      </if>

      <if test="otherUserId != null" >

        other_user_id,

      </if>

      <if test="otherUserAuthId != null" >

        other_user_auth_id,

      </if>

      <if test="otherusername != null and otherusername !=''" >

        otherusername,

      </if>

      <if test="otherRequirementNum != null" >

        other_requirement_num,

      </if>

      <if test="propertyType != null" >

        property_type,

      </if>

      <if test="referenceId != null" >

        reference_id,

      </if>

      <if test="referenceType != null" >

        reference_type,

      </if>

      <if test="referenceName != null and referenceName !=''" >

        reference_name,

      </if>

      <if test="content != null and content !=''" >

        content,

      </if>

      <if test="status != null" >

        status,

      </if>

      <if test="isActive != null" >

        is_active,

      </if>

      <if test="dayNum != null" >

        day_num,

      </if>

      <if test="beginTime != null" >

        begin_time,

      </if>

      <if test="endTime != null" >

        end_time,

      </if>

      <if test="updatedTime != null" >

        updated_time,

      </if>

      <if test="createdTime != null" >

        created_time,

      </if>

      <if test="receiveTime != null" >

        receive_time,

      </if>

      <if test="createdBy != null and createdBy !=''" >

        created_by,

      </if>

      <if test="updatedBy != null and updatedBy !=''" >

        updated_by,

      </if>

      <if test="version != null" >

        version,

      </if>

    </trim>

    <trim prefix="values (" suffix=")" suffixOverrides="," >

      <if test="id != null" >

        #{id,jdbcType=BIGINT},

      </if>

      <if test="adminUserId != null" >

        #{adminUserId,jdbcType=BIGINT},

      </if>

       <if test="adminUserName != null and adminUserName !=''" >

        #{adminUserName,jdbcType=VARCHAR},

      </if>

      <if test="userId != null" >

        #{userId,jdbcType=BIGINT},

      </if>

      <if test="userAuthId != null" >

        #{userAuthId,jdbcType=BIGINT},

      </if>

      <if test="username != null and username !=''" >

        #{username,jdbcType=VARCHAR},

      </if>

      <if test="requirementNum != null" >

        #{requirementNum,jdbcType=BIGINT},

      </if>

      <if test="otherUserId != null" >

        #{otherUserId,jdbcType=BIGINT},

      </if>

      <if test="otherUserAuthId != null" >

        #{otherUserAuthId,jdbcType=BIGINT},

      </if>

      <if test="otherusername != null and otherusername !=''" >

        #{otherusername,jdbcType=VARCHAR},

      </if>

      <if test="otherRequirementNum != null" >

        #{otherRequirementNum,jdbcType=BIGINT},

      </if>

      <if test="propertyType != null" >

        #{propertyType,jdbcType=TINYINT},

      </if>

      <if test="referenceId != null" >

        #{referenceId,jdbcType=BIGINT},

      </if>

      <if test="referenceType != null" >

        #{referenceType,jdbcType=TINYINT},

      </if>

       <if test="referenceName != null and referenceName !=''" >

        #{referenceName,jdbcType=VARCHAR},

      </if>

      <if test="content != null and content !=''" >

        #{content,jdbcType=VARCHAR},

      </if>

      <if test="status != null" >

        #{status,jdbcType=TINYINT},

      </if>

      <if test="isActive != null" >

        #{isActive,jdbcType=TINYINT},

      </if>

      <if test="dayNum != null" >

        #{dayNum,jdbcType=BIGINT},

      </if>

      <if test="beginTime != null" >

        #{beginTime,jdbcType=TIMESTAMP},

      </if>

      <if test="endTime != null" >

        #{endTime,jdbcType=TIMESTAMP},

      </if>

      <if test="updatedTime != null" >

        #{updatedTime,jdbcType=TIMESTAMP},

      </if>

      <if test="createdTime != null" >

        #{createdTime,jdbcType=TIMESTAMP},

      </if>

      <if test="receiveTime != null" >

        #{receiveTime,jdbcType=TIMESTAMP},

      </if>

      <if test="createdBy != null and createdBy !=''" >

        #{createdBy,jdbcType=VARCHAR},

      </if>

      <if test="updatedBy != null and updatedBy !=''" >

        #{updatedBy,jdbcType=VARCHAR},

      </if>

      <if test="version != null" >

        #{version,jdbcType=INTEGER},

      </if>

    </trim>

  </insert>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: