MyBatis动态SQL语法

【注:摘自MyBatis官网 】html

一、动态SQL的元素:git

二、 if语句
 
<select id="findActiveBlogWithTitleLike" resultType="Blog">
  SELECT * FROM BLOG 
  WHERE state = ‘ACTIVE’ 
  <if test="title != null">
    AND title like #{title}
  </if>
</select>
这条语句会提供一个可选的文本查找功能。若是你没有传递title,那么全部激活的博客都会被返回。可是若是你传递了title,那么就会查找相近的title(对于敏锐的检索,这中状况下你的参数值须要包含任意的遮掩或通配符)的博客。
 
倘若咱们想可选地搜索title 和author呢?首先,要改变语句的名称让它有意义。而后 简单加入另外的一个条件。
<select id="findActiveBlogLike" resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’ 
  <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>
 
三、choose(when, otherwise)
 
有时咱们不想应用全部的条件, 相反咱们想选择不少状况下的一种。Java 中的switch和语句类似,MyBatis提供choose元素。
咱们使用上面的示例,可是如今咱们来搜索当title提供时仅有title 条件,当author提 供时仅有author条件。若是两者都没提供,只返回featured blogs。
 
<select id="findActiveBlogLike" 
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>
【注:choose语句中的when是只要知足一个条件以后就放弃继续比较了,比较方式同Java中的Switch语句相似。】
 
四、trim(where, set)
 
考虑,多个动态查询条件状况:
<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 结束时就会成这样:github

SELECT * FROM BLOG 
WHERE

这会致使查询失败。若是仅仅第二个条件匹配是什么样的?这条 SQL 结束时就会是这 样:sql

SELECT * FROM BLOG 
WHERE 
AND title like ‘someTitle’

这个查询也会失败。这个问题不能简单的用条件来解决,若是你历来没有这样写过,那 么你之后也不会这样来写。数组

MyBatis 有一个简单的处理,这在 90%的状况下都会有用。而在不能使用的地方,你可 以自定义处理方式。加上一个简单的改变,全部事情都会顺利进行:mybatis

<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>
where 元素知道若是由被包含的标记返回任意内容,就仅仅插入“WHERE” 。并且,如 果以“AND”或“OR”开头的内容,那么就会跳过WHERE不插入。
 
若是 where 元素没有作出你想要的,你可使用 trim 元素来自定义。好比,和 where 元素相等的 trim元素是:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ... 
</trim>
prefixOverrides属性采用管道文本分隔符来覆盖, 这里的空白也是重要的。它的结果就是移除在prefixOverrides属性中指定的内容,插入在with属性中的内容。
 
和动态更新语句类似的解决方案是 set。set元素能够被用于动态包含更新的列,而不包含不需更新的。好比:
<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</update>
这里,set 元素会动态前置 SET 关键字,并且也会消除任意无关的逗号,那也许在应用 条件以后来跟踪定义的值。
 
若是你对和这相等的trim元素好奇,它看起来就是这样的:
<trim prefix="SET" suffixOverrides=",">
  ...
</trim>
注意这种状况下咱们覆盖一个后缀,而同时也附加前缀。
 
五、foreach
 
另一个动态SQL通用的必要操做是迭代一个集合, 一般是构建在IN条件中的。
<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select> 
foreach 元素是很是强大的,它容许你指定一个集合,声明集合项和索引变量,它们可 以用在元素体内。它也容许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素 是很智能的,它不会偶然地附加多余的分隔符。
注意 你能够传递一个 List 实例或者数组做为参数对象传给MyBatis。当你这么作的时候,MyBatis会自动将它包装在一个Map中,用名称在做为键。List实例将会以“list” 做为键,而数组实例将会以“array”做为键。
 
六、bind
 
bind元素容许你在自定义变量(不用符合OGNL规范),而且应用到上下文中。例如:
<select id="selectBlogsLike" resultType="Blog">
  <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
  SELECT * FROM BLOG
  WHERE title LIKE #{pattern}
</select>
相关文章
相关标签/搜索