zoukankan      html  css  js  c++  java
  • 【JavaEE】之MyBatis动态SQL

      动态SQL就是在SQL语句中添加一些标签,以完成某些逻辑。通常用到的动态SQL标签有<if>、<choose>、<where>、<trim>、<set>、<foreach>、<bind>、<sql>等。

    1、if

      if是简单的条件判断,通过if语句我们可以实现某些简单的条件选择,一个例子的代码如下:

    <select id="dynamicIfTest" parameterType="Blog" resultType="Blog">  
        select * from t_blog where 11 = 1  
        <if test="title != null">  
            and title = #{title}  
        </if>  
        <if test="content != null">  
            and content = #{content}  
        </if>  
        <if test="owner != null">  
            and owner = #{owner}  
        </if>  
    </select>

    2、choose

      choose标签的作用就相当于JAVA中的switch语句,只不过不是使用case与default搭配,而是使用when和otherwise搭配。一个例子的代码如下:

    <select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">  
        select * from t_blog where 11 = 1   
        <choose>  
            <when test="title != null">  
                and title = #{title}  
            </when>  
            <when test="content != null">  
                and content = #{content}  
            </when>  
            <otherwise>  
                and owner = "owner1"  
            </otherwise>  
        </choose>  
    </select>

      choose语句和JAVA中的switch语句类似,都是按顺序从上向下判断,一旦有某个when的条件满足的时候,就会跳出choose,当所有when中的条件都不满足,就会执行otherwise中的语句。

    3、trim

      trim标签的作用是可以在自己包含的内容前后加上前缀或后缀,与之对应的属性是prefix和suffix;trim标签也可以把包含内容中首、尾部的某些内容覆盖(即忽略),对应的属性是prefixOverrides和suffixOverrides。一个例子的代码如下:

    <select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">  
        select * from t_blog   
        <trim prefix="where" prefixOverrides="and|or">  
            <if test="title != null">  
                title = #{title}  
            </if>  
            <if test="content != null">  
                and content = #{content}  
            </if>  
            <if test="owner != null">  
                or owner = #{owner}  
            </if>  
        </trim>  
    </select>

      上面这段代码的意思是:在这段代码的最前面加一个前缀where,然后把可能位于最前面的and或or给覆盖(忽略)掉。正因为trim标签有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能。

    4、where

      where标签的作用是为了简化SQL语句中where的条件判断的,一个例子的代码如下:

    <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">  
        select * from t_blog   
        <where>  
            <if test="title != null">  
                and title = #{title}  
            </if>  
            <if test="content != null">  
                and content = #{content}  
            </if>  
            <if test="owner != null">  
                and owner = #{owner}  
            </if>  
        </where>  
    </select>

      where标签的作用是会在写<where>的地方自动输出一个where,即省略了SQL语句中的where关键字;像上面这段动态SQL,假如第一个判断成立,那么MyBatis为我们生成的SQL语句是:select * from t_blog where title = #{title}; ,而不是:select * from t_blog where and title = #{title},即MyBatis的动态SQL会自动把第一个多余的and去掉,如果将这里的and换成or,也会有同样的效果。

    5、set

      set标签主要用在更新操作的时候,它的功能和where元素差不多,主要是在包含的语句最前面添加一个set前缀,然后如果所包含的语句是以逗号结尾的话就将该逗号忽略,如果set包含的内容为空的话就会报错。一个例子的代码如下:

    <update id="dynamicSetTest" parameterType="Blog">  
        update t_blog  
        <set>  
            <if test="title != null">  
                title = #{title},  
            </if>  
            <if test="content != null">  
                content = #{content},  
            </if>  
            <if test="owner != null">  
                owner = #{owner}  
            </if>  
        </set>  
        where id = #{id}  
    </update>

      在上面这段代码中,如果set中的三个判断都不成立,即set中的内容为空,那么就会报错。有了set标签,我们就可以动态的更新那些修改了的字段了。

    6、foreach

      foreach标签主要用于构建in条件,它可以在SQL语句中迭代一个集合。foreach标签的属性有item、index、collection、open、separator、close,其中,item表示集合中的元素进行迭代时的别名;index指定当前迭代的位置;open指定这段SQL的前缀;separator指定每个迭代元素之间的分隔符;close指定这段SQL的后缀;collection指定集合类型:

      (1)如果传入List列表,则collection的属性值为list,示例代码如下:

    <select id="dynamicForeachTest" resultType="Blog">  
        select * from t_blog where id in  
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
            #{item}  
        </foreach>  
    </select>

      (2)如果传入数组,则collection的属性值为array,示例代码如下:

    <select id="dynamicForeach2Test" resultType="Blog">  
        select * from t_blog where id in  
        <foreach collection="array" index="index" item="item" open="(" separator="," close=")">  
            #{item}  
        </foreach>  
    </select>

      (3)如果传入Map集合,则collection的属性值是Map集合的key,示例代码如下(在这个例子中,Map中存储着一个key是ids的List):

    <select id="dynamicForeach3Test" resultType="Blog">  
        select * from t_blog where title like "%"#{title}"%" and id in  
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">  
            #{item}  
        </foreach>  
    </select>

      (4)如果是Set集合,且Set中每个元素的类型是Map.Entry时,则collection的属性值为collection,此时index属性代表Map.Entry的key,item属性代表Map.Entry的value。示例代码如下:

    <select id="dynamicForeachTest" resultType="Blog">  
        select * from t_blog where id in  
        <!-- 遍历的对象是Map.Entry时,index代表对应的key,item代表对应的value -->  
        <foreach collection="collection" index="key" item="value" open="(" separator="," close=")">  
            #{key}, #{value}  
        </foreach>  
    </select>

    7、bind

      bind标签主要用于模糊查询的字符串拼接,一个例子的代码如下:

    <select id="fuzzyQuery" resultType="Blog" parameterType="java.lang.String">
        <bind name="titleLike" value="'%'+_parameter+'%'"/>  
        select * from t_blog where title like #{titleLike}    
    </select>

    8、SQL

      有时,如果动态SQL中包含的代码过长,且有可能在不同的SQL语句中重复用到,那么就可以将这段SQL提取出来作为SQL片段(也相当于封装)。抽取出来的SQL语句使用<sql>标签包裹,在使用到某个SQL片段时,使用<include>标签引入SQL片段。一个例子的代码如下:

    <!-- 被抽取出来的SQL片段(id是这个SQL片段的唯一标识) -->
    <!-- 注意:SQL片段一般都是基于单表创建的;SQL片段中最好不要包括where语句 -->
    <sql id="blog_query">
        <if test="title!=null">
            and title = #{title}
        </if>
    </sql>
    
    <select id="findEmplyeeListDynamicSQL" parameterType="Blog" resultType="Blog">
        SELECT * FROM t_blog
        <where>
            <!-- 引用抽取出来的SQL片段。如果要引用的SQL片段在其他mapper文件中,则需要在前面添加namespace -->
            <include refid="blog_query"></include>
        </where>
    </select>
  • 相关阅读:
    计算机的几种命令行
    oracle体系结构
    数字档案馆建设指南及档案业务系统归档接口规范
    ERP系统归档
    oracle ITL(事务槽)的理解
    oracle表属性
    docker+httpd的安装
    访问GitLab的PostgreSQL数据库,查询、修改、替换等操作
    docker+rabbitmq的安装
    docker+elasticsearch的安装
  • 原文地址:https://www.cnblogs.com/itgungnir/p/6211348.html
Copyright © 2011-2022 走看看