ssm框架之动态sql

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

1、if

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

 
  1. <select id="dynamicIfTest" parameterType="Blog" resultType="Blog">

  2. select * from t_blog where 11 = 1

  3. <if test="title != null">

  4. and title = #{title}

  5. </if>

  6. <if test="content != null">

  7. and content = #{content}

  8. </if>

  9. <if test="owner != null">

  10. and owner = #{owner}

  11. </if>

  12. </select>

2、choose

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

 
  1. <select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">

  2. select * from t_blog where 11 = 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 = "owner1"

  12. </otherwise>

  13. </choose>

  14. </select>

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

3、trim

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

 
  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">

  5. title = #{title}

  6. </if>

  7. <if test="content != null">

  8. and content = #{content}

  9. </if>

  10. <if test="owner != null">

  11. or owner = #{owner}

  12. </if>

  13. </trim>

  14. </select>

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

4、where

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

 
  1. <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">

  2. select * from t_blog

  3. <where>

  4. <if test="title != null">

  5. and title = #{title}

  6. </if>

  7. <if test="content != null">

  8. and content = #{content}

  9. </if>

  10. <if test="owner != null">

  11. and owner = #{owner}

  12. </if>

  13. </where>

  14. </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包含的内容为空的话就会报错。一个例子的代码如下:

 
  1. <update id="dynamicSetTest" parameterType="Blog">

  2. update t_blog

  3. <set>

  4. <if test="title != null">

  5. title = #{title},

  6. </if>

  7. <if test="content != null">

  8. content = #{content},

  9. </if>

  10. <if test="owner != null">

  11. owner = #{owner}

  12. </if>

  13. </set>

  14. where id = #{id}

  15. </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,示例代码如下:

 
  1. <select id="dynamicForeachTest" resultType="Blog">

  2. select * from t_blog where id in

  3. <foreach collection="list" index="index" item="item" open="(" separator="," close=")">

  4. #{item}

  5. </foreach>

  6. </select>

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

 
  1. <select id="dynamicForeach2Test" resultType="Blog">

  2. select * from t_blog where id in

  3. <foreach collection="array" index="index" item="item" open="(" separator="," close=")">

  4. #{item}

  5. </foreach>

  6. </select>

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

 
  1. <select id="dynamicForeach3Test" resultType="Blog">

  2. select * from t_blog where title like "%"#{title}"%" and id in

  3. <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">

  4. #{item}

  5. </foreach>

  6. </select>

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

 
  1. <select id="dynamicForeachTest" resultType="Blog">

  2. select * from t_blog where id in

  3. <!-- 遍历的对象是Map.Entry时,index代表对应的key,item代表对应的value -->

  4. <foreach collection="collection" index="key" item="value" open="(" separator="," close=")">

  5. #{key}, #{value}

  6. </foreach>

  7. </select>

7、bind

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

 
  1. <select id="fuzzyQuery" resultType="Blog" parameterType="java.lang.String">

  2. <bind name="titleLike" value="'%'+_parameter+'%'"/>

  3. select * from t_blog where title like #{titleLike}

  4. </select>

8、SQL

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

 

[html] view plain copy

  1. <code class="language-html"><!-- 被抽取出来的SQL片段(id是这个SQL片段的唯一标识) -->  
  2. <!-- 注意:SQL片段一般都是基于单表创建的;SQL片段中最好不要包括where语句 -->  
  3. <sql id="blog_query">  
  4.     <if test="title!=null">  
  5.         and title = #{title}  
  6.     </if>  
  7. </sql>  
  8.   
  9. <select id="findEmplyeeListDynamicSQL" parameterType="Blog" resultType="Blog">  
  10.     SELECT * FROM t_blog  
  11.     <where>  
  12.         <!-- 引用抽取出来的SQL片段。如果要引用的SQL片段在其他mapper文件中,则需要在前面添加namespace -->  
  13.         <include refid="blog_query"></include>  
  14.     </where>  
  15. </select></code>  

猜你喜欢

转载自blog.csdn.net/weixin_41290037/article/details/81285887
今日推荐