MyBatis动态SQL完整版

 MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。如果你有使用 JDBC 或其他相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。,MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。从而拼接成相应的sql语句。

  • if
在动态 SQL 中所做的最通用的事情是包含部分 where 字句的条件。比如:
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <select id="findActiveBlogWithTitleLike" parameterType="Blog"  
  2.     resultType="Blog">  
  3.     SELECT * FROM BLOG  
  4.     WHERE state = "ACTIVE"  
  5.     <if test="title != null ">  
  6.         AND title like #{title}  
  7.     </if>  
  8. </select>  


这条语句会提供一个可选的文本查找功能。如果你没有传递 title,那么所有激活的博客都会被返回。但是如果你传递了 title,那么就会查找相近的 title(对于敏锐的检索,这中情况下你的参数值需要包含任意的遮掩或通配符)的博客。假若我们想可选地搜索 title 和 author 呢?首先,要改变语句的名称让它有意义。然后简单加入另外的一个条件。
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">  
  2.     SELECT * FROM BLOG WHERE state = 'ACTIVE'  
  3.     <if test="title ! null ">  
  4.         AND title like #{title}  
  5.     </if>  
  6.     <if test="author != null and author.name != null">  
  7.         AND title like #{author.name}  
  8.     </if>  
  9. </select>  


  • choose, when, otherwise
有时我们不想应用所有的条件,相反我们想选择很多情况下的一种。 Java 中的 switch和语句相似,MyBatis 提供 choose 元素。我们使用上面的示例,但是现在我们来搜索当 title 提供时仅有 title 条件,当 author 提供时仅有 author 条件。如果二者都没提供,只返回 featured blogs(也许是由管理员策略地选择的结果列表,而不是返回大量没有意义的,随机的博客结果列表)。
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">  
  2.     SELECT * FROM BLOG WHERE state = "ACTIVE"  
  3.     <choose>  
  4.         <when test="title != null ">  
  5.             AND title like #{title}  
  6.         </when>  
  7.         <when test="author != null and author.name != null">  
  8.             AND title like #{author.name}  
  9.         </when>  
  10.         <otherwise>  
  11.             AND featured = 1  
  12.         </otherwise>  
  13.     </choose>  
  14. </select>  


  • trim, where, set
前面的例子已经方便地处理了一个臭名昭著的动态 SQL 问题。要考虑我们回到“if”示例后会发生什么,但是这次我们将“ACTIVE = 1”也设置成动态的条件。
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">  
  2.     SELECT * FROM BLOG  
  3.     WHERE  
  4.     <if test="state != null ">  
  5.         state = #{state}  
  6.     </if>  
  7.     <if test="title != null ">  
  8.         AND title like #{title}  
  9.     </if>  
  10.     <if test="author != null and author.name != null">  
  11.         AND title like #{author.name}  
  12.     </if>  
  13. </select>  


如果这些条件都没有匹配上将会发生什么?这条 SQL 结束时就会成这样:
      SELECT * FROM BLOG WHERE
这会导致查询失败。如果仅仅第二个条件匹配是什么样的?这条 SQL 结束时就会是这样:
SELECT * FROM BLOG WHERE AND title like „someTitle‟
这个查询也会失败。这个问题不能简单的用条件来解决,或许你会说可以这样写:
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">  
  2.     SELECT * FROM BLOG WHERE 1=1  
  3.     <if test="state ! null ">  
  4.         and state = #{state}  
  5.     </if>  
  6.     <if test="title ! null ">  
  7.         AND title like #{title}  
  8.     </if>  
  9.     <if test="author ! null and author.name ! =null">  
  10.         AND title like #{author.name}  
  11.     </if>  
  12. </select>  


MyBatis 有一个简单的处理,这在 90%的情况下都会有用。而在不能使用的地方,你可以自定义处理方式。加上一个简单的改变,所有事情都会顺利进行:
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <select id="findActiveBlogLike" parameterType="Blog" resultType="Blog">  
  2.     SELECT * FROM BLOG  
  3.     <where>  
  4.         <if test="state ! null ">  
  5.             state = #{state}  
  6.         </if>  
  7.         <if test="title ! null ">  
  8.             AND title like #{title}  
  9.         </if>  
  10.         <if test="author ! null and author.name ! =null">  
  11.             AND title like #{author.name}  
  12.         </if>  
  13.     </where>  
  14. </select>  


where 元素知道如果由被包含的标记返回任意内容,就仅仅插入“WHERE”。而且,如果以“AND”或“OR”开头的内容,那么就会跳过 WHERE 不插入。如果 where 元素没有做出你想要的,你可以使用 trim 元素来自定义。比如,和 where元素相等的 trim 元素是:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,例如:
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1.     <select id="test" parameterType="map" resultType="dept">  
  2.     select * from scott.Dept  
  3.     <trim  prefix="where" prefixOverrides="and | or" suffixOverrides=",">  
  4.         <if test="startId != null">  
  5.             <![CDATA[and deptno <=#{startId}]]>  
  6.         </if>  
  7.         <if test="endId != null">  
  8.             <![CDATA[or deptno >= #{endId},]]>  
  9.         </if>  
  10.     </trim>  
  11. </select>  


或者用于更新
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <update id="updateDept" parameterType="dept">  
  2.     update scott.dept  
  3.     <trim prefix="set" suffixOverrides=",">  
  4.         <choose>  
  5.             <when test="dname!=null">  
  6.                 dname=#{dname},  
  7.             </when>  
  8.             <otherwise>  
  9.                 dname='123',  
  10.             </otherwise>  
  11.         </choose>  
  12.         <if test="loc!=null">  
  13.                 loc=#{loc},  
  14.         </if>  
  15.     </trim>  
  16.     where deptno=#{deptno}  
  17. </update>  


set 元素可以被用于动态包含更新的列,而不包含不需更新的。比如:
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">  
  2.     update Author  
  3.     <set>  
  4.         <if test="username != null">username=#{username},</if>  
  5.         <if test="password != null">password=#{password},</if>  
  6.         <if test="email != null">email=#{email},</if>  
  7.         <if test="bio != null">bio=#{bio}</if>  
  8.     </set>  
  9.     where id=#{id}  
  10. </update>  


这里,set 元素会动态前置 SET 关键字,而且也会消除任意无关的逗号,那也许在应用条件之后来跟踪定义的值。如果你对和这相等的 trim 元素好奇,它看起来就是这样的:
<trim prefix="SET" suffixOverrides=",">
...
</trim>
注意这种情况下我们覆盖一个后缀,而同时也附加前缀。


  • foreach
另外一个动态 SQL 通用的必要操作是迭代一个集合,通常是构建在 IN 条件中的。比如:
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <select id="selectPostIn" resultType="domain.blog.Post">  
  2.     SELECT * FROM POST P WHERE ID in  
  3.     <foreach item="item" index="index" collection="list" open="("  
  4.         separator="," close=")">  
  5.         #{item}  
  6.     </foreach>  
  7. </select>  


foreach 元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。
注意:你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名称作为键。List 实例将会以“list”作为键,而数组实例将会以“array”作为键。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值