• mybatis的动态sql详解


    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑。

    MyBatis中用于实现动态SQL的元素主要有:

     

    • if
    • choose(when,otherwise)
    • trim
    • where
    • set
    • foreach
       if就是简单的条件判断,利用if语句我们可以实现某些简单的条件选择。先来看如下一个例子:
    复制代码
     1 <select id="dynamicIfTest" parameterType="Blog" resultType="Blog">  
     2     select * from t_blog where 11 = 1  
     3     <if test="title != null and title !='' ">  
     4         and title = #{title}  
     5     </if>  
     6     <if test="content != null and title !='' ">  
     7         and content = #{content}  
     8     </if>  
     9     <if test="owner != null and title !='' ">
    10 and owner = #{owner}
    11 </if>
    12 </select>
    复制代码
          这条语句的意思非常简单,如果你提供了title参数,那么就要满足title=#{title},同样如果你提供了Content和Owner的时候,它们也需要满足相应的条件,之后就是返回满足这些条件的所有Blog,这是非常有用的一个功能,以往我们使用其他类型框架或者直接使用JDBC的时候, 如果我们要达到同样的选择效果的时候,我们就需要拼SQL语句,这是极其麻烦的,比起来,上述的动态SQL就要简单多了。
     
         choose元素的作用就相当于JAVA中的switch语句,基本上跟JSTL中的choose的作用和用法是一样的,通常都是与when和otherwise搭配的。看如下一个例子:
    复制代码
     1 <select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">  
     2     select * from t_blog where 1 = 1   
     3     <choose>  
     4         <when test="title != null">  
     5             and title = #{title}  
     6         </when>  
     7         <when test="content != null">  
     8             and content = #{content}  
     9         </when>  
    10         <otherwise>  
    11             and owner = "owner"  
    12         </otherwise>  
    13     </choose>  
    14 </select>  
    复制代码
         when元素表示当when中的条件满足的时候就输出其中的内容,跟JAVA中的switch效果差不多的是按照条件的顺序,当when中有条件满足的时候,就会跳出choose,即所有的when和otherwise条件中,只有一个会输出,当所有的when条件都不满足的时候就输出otherwise中的内容。所以上述语句的意思非常简单, 当title!=null的时候就输出and titlte = #{title},不再往下判断条件,当title为空且content!=null的时候就输出and content = #{content},当所有条件都不满足的时候就输出otherwise中的内容。
     
       where语句的作用主要是简化SQL语句中where中的条件判断的,先看一个例子,再解释一下where的好处。
    复制代码
     1 <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">  
     2     select * from t_blog   
     3     <where>  
     4         <if test="title != null and title !='' ">  
     5             and title = #{title}  
     6         </if>  
     7         <if test="content != null and content !='' ">  
     8             and content = #{content}  
     9         </if>  
    10         <if test="owner != null and owner !='' ">  
    11             and owner = #{owner}  
    12         </if>  
    13     </where>  
    14 </select>  
    复制代码
           where元素的作用是会在写入where元素的地方输出一个where,另外一个好处是你不需要考虑where元素里面的条件输出是什么样子的,MyBatis会智能的帮你处理,如果所有的条件都不满足那么MyBatis就会查出所有的记录,如果输出后是and 开头的,MyBatis会把第一个and忽略,当然如果是or开头的,MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上。像上述例子中,如果title=null, 而content != null,那么输出的整个语句会是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},因为MyBatis会智能的把首个and 或 or 给忽略。
     
         trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能,示例代码如下:
     
    复制代码
     1 <select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">  
     2     select * from t_blog   
     3     <trim prefix="where" prefixOverrides="and |or">  
     4         <if test="title != null and title!='' ">  
     5             title = #{title}  
     6         </if>  
     7         <if test="content != null and content!='' ">  
     8             and content = #{content}  
     9         </if>  
    10         <if test="owner != null and owner!='' ">  
    11             or owner = #{owner}  
    12         </if>  
    13     </trim>  
    14 </select>  
    复制代码

          set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的字段。下面是一段示例代码:

    复制代码
     1 <update id="dynamicSetTest" parameterType="Blog">  
     2     update t_blog  
     3     <set>  
     4         <if test="title != null and title!='' ">  
     5             title = #{title},  
     6         </if>  
     7         <if test="content != null and content!='' ">  
     8             content = #{content},  
     9         </if>  
    10         <if test="owner != null and owner!='' ">  
    11             owner = #{owner}  
    12         </if>  
    13     </set>  
    14     where id = #{id}  
    15 </update>  
    复制代码也可以这么写
     1  <update id="dynamicSetTest" parameterType="Blog">  
     2     update t_blog  
     3      <trim prefix="set" prefixOverrides=",">  
     4         <if test="title != null and title!='' ">  
     5             title = #{title}
     6         </if>  
     7         <if test="content != null and content!='' ">  
     8             content = #{content}  
     9         </if>  
    10         <if test="owner != null and owner!='' ">  
    11             owner = #{owner}  
    12         </if>  
    13     </trim>
    14     where id = #{id}  
    15 </update>  
     
     
     
     
     
     
     
       上述示例代码中,如果set中一个条件都不满足,即set中包含的内容为空的时候就会报错。
  • 相关阅读:
    java 时间操作
    springboot-helloworld 学习02
    springboot-helloworld 学习01
    java知识图谱
    python day100-[day41-45]-5 web-django RESTful架构和DRF入门
    python day100-[day41-45]-4 web-django 前后端分离
    hive mysql count distinct 多列
    hive 导出数据到文件
    使用npm i 编译vue项目出现无法拉取clone github.com中的源文件
    windows环境下elasticsearch安装教程 (版本为7.12.0)
  • 原文地址:https://www.cnblogs.com/person008/p/6439433.html
Copyright © 2020-2023  润新知