动态SQL指的是根据不同的查询条件 , 生成不同的SQL语句。所谓的动态SQL,只是我们可以在SQL语句的层面去执行一个逻辑代码,动态SQL就是在拼接SQL语句,只需要保持SQL语句所需要的规范即可,mybatis主要通过提供几个标签来生成动态SQL,下面介绍几个常用的标签。

if标签

需求:根据作者名字和博客名字来查询博客,如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询;如果两个都为空则查询全部博客。

  1. <select id="listBlogs" parameterType="map" resultType="Blog">
  2. select * from blog where 1=1
  3. <if test="title !=null">
  4. and title=#{title}
  5. </if>
  6. <if test="author != null">
  7. and author=#{author}
  8. </if>
  9. </select>
  1. /**
  2. * 查询博客
  3. *
  4. * @param map 查询条件的map集合
  5. * @return 返回List<Blog>
  6. */
  7. List<Blog> listBlogs(Map<String, Object> map);

where标签

从上面的需要可以看到,where 1=1 这样的写法太牵强了,但如果不写1=1那么where这个词语会一直存在,这显然不行,而mybatis提供的where标签可以很好的解决这个问题。

  1. <select id="listBlogs" parameterType="map" resultType="Blog">
  2. select * from blog
  3. <where>
  4. <if test="title != null">
  5. title = #{title}
  6. </if>
  7. <if test="author != null">
  8. and author = #{author}
  9. </if>
  10. </where>
  11. </select>

这个“where”标签会知道如果它包含的标签中有返回值的话(即存在where条件),它才会插入一个‘where’。此外,如果标签返回的内容是以“AND 或OR ”开头的,则它会剔除掉(如下代码所示)。

  1. select * from blog
  2. <where>
  3. <if test="title != null">
  4. and title = #{title}
  5. </if>
  6. <if test="author != null">
  7. and author = #{author}
  8. </if>
  9. </where>

set标签

类似于where标签,set标签可以很好的动态生成含set关键词的sql语句,set标签同样也可以自动识别并去除不需要的“,”。

  1. /**
  2. * 更新一篇博客
  3. *
  4. * @param map 参数map
  5. */
  6. int updateBlog(Map<String, Object> map);
  1. <update id="updateBlog" parameterType="map">
  2. update blog
  3. <set>
  4. <if test="title != null">
  5. title=#{title},
  6. </if>
  7. <if test="author != null">
  8. author=#{author},
  9. </if>
  10. </set>
  11. where id=#{id}
  12. </update>

choose标签

如果有时候不想用到所有的查询条件,只想选择其中的一个(即查询条件有一个满足即可),使用 choose标签可以解决此类问题,类似于 Java 的 switch 语句。

  1. /**
  2. * 通过choose标签查询博客
  3. *
  4. * @param map map参数
  5. * @return 返回数据库发生改变的行数
  6. */
  7. List<Blog> getBlogChoose(Map<String, Object> map);
  1. <select id="getBlogChoose" parameterType="map" resultType="blog">
  2. select * from blog
  3. <where>
  4. <choose>
  5. <when test="title != null">title=#{title}</when>
  6. <when test="author != null">author=#{author}</when>
  7. <otherwise>views=#{views}</otherwise>
  8. </choose>
  9. </where>
  10. </select>

sql标签

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用,这个工作可以由sql标签来完成。举个例子:

  1. <select id="listBlogs" parameterType="map" resultType="Blog">
  2. select * from blog
  3. <where>
  4. <if test="title != null">
  5. and title = #{title}
  6. </if>
  7. <if test="author != null">
  8. and author = #{author}
  9. </if>
  10. </where>
  11. </select>
  12. <select id="listArticle" parameterType="map" resultType="Article">
  13. select * from article
  14. <where>
  15. <if test="title != null">
  16. and title = #{title}
  17. </if>
  18. <if test="author != null">
  19. and author = #{author}
  20. </if>
  21. </where>
  22. </select>

可以看到上述两个sql存在相同的部分,而项目中可能会存在大量的重复部分,这时候把它们抽取出来即可复用:

  1. <sql id="title-author">
  2. <if test="title != null">
  3. and title = #{title}
  4. </if>
  5. <if test="author != null">
  6. and author = #{author}
  7. </if>
  8. </sql>
  9. <select id="listBlogs" parameterType="map" resultType="Blog">
  10. select * from blog
  11. <where>
  12. <include refid="title-author"></include>
  13. </where>
  14. </select>
  15. <select id="listArticle" parameterType="map" resultType="Article">
  16. select * from article
  17. <where>
  18. <include refid="title-author"></include>
  19. </where>
  20. </select>

注意:①最好基于 单表来定义 sql 片段,提高片段的可重用性
②在 sql 片段中不要包括 where

foreach标签

先介绍foreach标签的各个属性,如下所示:
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串,即分割符
index:遍历到当前元素的索引
然后,看一个实例:

  1. <select id="getBlogForeach" parameterType="map" resultType="blog">
  2. select * from blog
  3. <where>
  4. <foreach collection="ids" item="id" open="id in (" close=")" separator=",">
  5. #{id}
  6. </foreach>
  7. </where>
  8. </select>

运行结果:
image.png
可见拼接出来的sql:select * from blog WHERE id in ( ? , ? , ? )