Mybatis04--动态SQL

    xiaoxiao2021-03-25  37

    前言:

    在以前我们使用JDBC或者其它框架时,一件很痛苦的事情是根据不同条件拼接 SQL 语句。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。然而现在,我们利用动态 SQL 这一特性就可以彻底摆脱这种痛苦。

    通常使用动态SQL不可能是独立的一部分,MyBatis当然使用一种强大的动态SQL语言来改进这种情形,这种语言可以被用在任意映射的SQL语句中。

    动态SQL元素和使用JSTL或其它相似的基于XML的文本处理器相似,在MyBatis之前的版本中,有很多元素需要了解,MyBatis3大大地提升了它们,现在用不到原先一半的元素就能工作了,MyBatis采用功能强大的基于OGNL的表达式来消除其他元素。


    if:

    动态 SQL 通常要做的事情是有条件地包含 where 子句的一部分。比如:

    <select id="findBlog" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select>

    这条语句提供了一个可选的文本查找类型的功能。如果没有传入“title”,那么所有处于“ACTIVE”状态的BLOG都会返回;反之若传入了“title”,那么就会把模糊查找“title”内容的BLOG结果返回(就这个例子而言,细心的读者会发现其中的参数值是可以包含一些掩码或通配符的)。

    多个条件查询:

    <select id="findBlog" 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…case…类似,MyBasit提供choose元素。

    <select id="findBlog" 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>

    两个when只能满足一个,都不满足则走other。


    trim, where, set:

    第一个例子已经示例了if的用法,但是这种用法有个缺陷—-动态SQL外必须有where子句。

    什么意思,因为很多时候我们需要where后面的子句都动态生成,而不是事先有一个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> </select>

    如果这些条件没有一个能匹配上将会怎样?最终这条 SQL 会变成这样:

    SELECT * FROM BLOG WHERE

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

    SELECT * FROM BLOG WHERE AND title like ‘someTitle’

    这个查询也会失败。这个问题不能简单的用条件句式来解决。

    这里有两种方法解决这种问题:

    1)一个讨巧的办法是用where 1 = 1的方式

    <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHERE 1 = 1 <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>

    解释:因为”1 = 1”永远满足,所以相当于给where加了一层true而已,此时动态SQL生成什么where判断条件就是什么。

    2)MyBatis 有一个简单的处理,这在90%的情况下都会有用。

    <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 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。

    如果 where 元素没有按正常套路出牌,我们还是可以通过自定义 trim 元素来定制我们想要的功能。比如,和 where 元素等价的自定义 trim 元素为:

    <trim prefix="WHERE" prefixOverrides="AND |OR "> … </trim>

    即:

    ```sql <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG <trim prefix="WHERE" prefixOverrides="AND |OR "> <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> </select>

    注:prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。

    类似的用于动态更新语句的解决方案叫做 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 元素的功能是非常强大的,它允许你指定一个集合,声明可以用在元素体内的集合项和索引变量。它也允许你指定开闭匹配的字符串以及在迭代中间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。

    注:可以将任何可迭代对象(如列表、集合等)和任何的字典或者数组对象传递给foreach作为集合参数。当使用可迭代对象或者数组时,index是当前迭代的次数,item的值是本次迭代获取的元素。当使用字典(或者Map.Entry对象的集合)时,index是键,item是值。

    转载请注明原文地址: https://ju.6miu.com/read-50018.html

    最新回复(0)