MyBatis 教程

MyBatis 动态 SQL 之 trim 元素

MyBatis 中的 <trim> 标签可能会让你有点迷惑,知道怎么去使用。

<trim> 标签的主要功能是可以在自己包含的内容前面添加指定前缀,内容后面添加指定的后缀;或者替换掉内容前后指定的内容。

我们先看看该标签 DTD 定义:

<!ELEMENT trim (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
<!ATTLIST trim
prefix CDATA #IMPLIED
prefixOverrides CDATA #IMPLIED
suffix CDATA #IMPLIED
suffixOverrides CDATA #IMPLIED
>

<trim> 标签定义了四个属性,作用分别如下:

  • prefix:如果指定了该属性,将会将该属性的内容添加到 <trim> 元素内容的前面;

  • prefixOverrides:如果指定了该属性,将会把 <trim> 元素内容前面的等于该属性值的内容替换为空字符;

  • suffix:如果制定了该属性,将会将该属性的内容添加到 <trim> 元素内容的后面;

  • suffixOverrides:如果指定了该属性,将会把 <trim> 元素内容后面的等于该属性值的内容替换为空字符;

示例代码

(1)下面示例将会在 <trim> 内容前添加 where 字符串,如下:

<select id="getUserList" resultMap="RESULT_MAP">
   select `user_id`, `name`, `age`, `sex` from `user`
   <trim prefix="where">
      <if test="userId > 0">
         `user_id`=#{userId}
      </if>
   </trim>
</select>

输出 SQL 语句如下:

select `user_id`, `name`, `age`, `sex` from `user` where `user_id`=?

(2)下面示例在 <trim> 标签内容前面添加 where 字符串,且将 <trim> 标签内容前面的 and 字符串替换成空字符串。代码如下:

<select id="getUserList2" resultMap="RESULT_MAP">
   select `user_id`, `name`, `age`, `sex` from `user`
   <trim prefix="where" prefixOverrides="and">
      <choose>
         <when test="username != null and username != ''">
            and `name` like #{username}
         </when>
         <when test="age != null">
            and `age` >= #{age}
         </when>
         <otherwise>
            and `user_id` is not null
         </otherwise>
      </choose>
   </trim>
</select>

输出 SQL 语句如下:

select `user_id`, `name`, `age`, `sex` from `user` where `name` like ? 
-- 或
select `user_id`, `name`, `age`, `sex` from `user` where `age` >= ? 
-- 或
select `user_id`, `name`, `age`, `sex` from `user` where `user_id` is not null

(3)下面示例在 <trim> 标签内容前面添加 where 字符串,且将 <trim> 标签内容尾部的 and 字符串替换成空字符串。代码如下:

<select id="getUserList3" resultMap="RESULT_MAP">
   select `user_id`, `name`, `age`, `sex` from `user`
   <trim prefix="where" suffixOverrides="and">
      <choose>
         <when test="username != null and username != ''">
            `name` like #{username} and
         </when>
         <when test="age != null">
            `age` >= #{age} and
         </when>
         <otherwise>
            `user_id` is not null and
         </otherwise>
      </choose>
   </trim>
</select>

输出 SQL 语句如下:

select `user_id`, `name`, `age`, `sex` from `user` where `name` like ? 
-- 或
select `user_id`, `name`, `age`, `sex` from `user` where `age` >= ? 
-- 或
select `user_id`, `name`, `age`, `sex` from `user` where `user_id` is not null

(4)下面示例在 <trim> 标签内容前面添加 where 字符串,且将 <trim> 标签内容尾部的 and 字符串替换成空字符串,还在 <trim> 标签内容尾部添加“group by `user_id` desc”分组 SQL。代码如下:

<select id="getUserList4" resultMap="RESULT_MAP">
   select `user_id`, `name`, `age`, `sex` from `user`
   <trim prefix="where" suffixOverrides="and" suffix=" group by `user_id` desc">
      <choose>
         <when test="username != null and username != ''">
            `name` like #{username} and
         </when>
         <when test="age != null">
            `age` >= #{age} and
         </when>
         <otherwise>
            `user_id` is not null and
         </otherwise>
      </choose>
   </trim>
</select>

输出 SQL 语句如下:

select `user_id`, `name`, `age`, `sex` from `user` where `age` >= ? group by `user_id` desc
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号