mybatis -- trim和choose

1,350 阅读3分钟

一、trim标签

mybatis的trim标签一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作。

以下是trim标签中涉及到的属性:

属性描述
prefix给sql语句拼接的前缀
suffix给sql语句拼接的后缀
prefixOverrides去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND"
suffixOverrides去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定

1、trim标签使用

trim 有四个属性 prefix,suffix 表示在trim标签包裹的部分的前面或者后面 添加内容( 注意:是没有prefixOverrides,suffixOverrides的情况下) 如果有prefixOverrides,suffixOverrides 表示覆盖Overrides中的内容。 如果只有prefixOverrides,suffixOverrides 表示删除。 例如:

<update id="testTrim" parameterType="com.mybatis.pojo.User">
    update user
    <trim prefix="set" suffixOverrides=",">
        <if test="cash!=null and cash!=''">cash= #{cash},</if>
        <if test="address!=null and address!=''">address= #{address},</if>
    </trim>
    <where>id = #{id}</where>
</update>

只有prefix=“set”,表示在trim包裹的部分的前面添加 set。
只有suffixOverrides=“,”,表示删除最后一个逗号。

上例也可以写成

<update id="testTrim" parameterType="com.mybatis.pojo.User">
    update user
    set
    <trim suffixOverrides="," suffix="where id = #{id}">
        <if test="cash!=null and cash!=''">cash= #{cash},</if>
        <if test="address!=null and address!=''">address= #{address},</if>
    </trim>
</update>

由于set写在了外面,trim中就不再需要prefix属性了,所以删除。
where标签从外面拿进trim里面,这样其实可以认为是将最后一个逗号”,”替换成了where id = #{id}。所以suffix和suffixOverrides一起使用。

2、使用trim标签去除多余的and关键字

有这样的一个例子:

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  WHERE 
  <if test="state != null">
    state = #{state}
  </if> 
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>

如果这些条件没有一个能匹配上会发生什么?最终这条 SQL 会变成这样:

SELECT * FROM BLOG
WHERE

这会导致查询失败。如果仅仅第二个条件匹配又会怎样?这条 SQL 最终会是这样:

SELECT * FROM BLOG
WHERE 
AND title like ‘someTitle’

你可以使用where标签来解决这个问题,where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  <where> 
    <if test="state != null">
         state = #{state}
    </if> 
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
</select>

trim标签也可以完成相同的功能,写法如下:

<trim prefix="WHERE" prefixOverrides="AND">
	<if test="state != null">
	  state = #{state}
	</if> 
	<if test="title != null">
	  AND title like #{title}
	</if>
	<if test="author != null and author.name != null">
	  AND author_name like #{author.name}
	</if>
</trim>

3、使用trim标签去除多余的逗号

最重要的属性是

suffixOverrides=","

二、choose (when, otherwise)标签

有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个,choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

例如下面例子,同样把所有可以限制的条件都写上,方面使用。choose会从上到下选择一个when标签的test为true的sql执行。

<!--  choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->  
<select id="selectControlTableColumnByTableColumnIdAndIsUpdateOrIsDelete"  parameterType="com.uama.mdm.model.mdata.MdControlTableColumn" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_list"></include>
        FROM md_control_table_column u
        <where>
            <choose>
                <when test="isUpdate !=null ">
                    AND u.is_update = #{isUpdate, jdbcType=INTEGER}
                </when>
                <when test="isDelete != null">
                    AND u.is_delete = #{isDelete, jdbcType=INTEGER}
                </when>
                <otherwise>
                </otherwise>
            </choose>
            <if test="tableColumnId != null">
               AND table_column_id = #{tableColumnId}
            </if>
        </where>
    </select>

这里的用来封装SQL语句,如普通<sql id=“base" id ,name,age>, 来调用该字段,test用来判断