动态SQL指的是根据不同的查询条件 , 生成不同的SQL语句。所谓的动态SQL,只是我们可以在SQL语句的层面去执行一个逻辑代码,动态SQL就是在拼接SQL语句,只需要保持SQL语句所需要的规范即可,mybatis主要通过提供几个标签来生成动态SQL,下面介绍几个常用的标签。
if标签
需求:根据作者名字和博客名字来查询博客,如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询;如果两个都为空则查询全部博客。
<select id="listBlogs" parameterType="map" resultType="Blog">
select * from blog where 1=1
<if test="title !=null">
and title=#{title}
</if>
<if test="author != null">
and author=#{author}
</if>
</select>
/**
* 查询博客
*
* @param map 查询条件的map集合
* @return 返回List<Blog>
*/
List<Blog> listBlogs(Map<String, Object> map);
where标签
从上面的需要可以看到,where 1=1 这样的写法太牵强了,但如果不写1=1那么where这个词语会一直存在,这显然不行,而mybatis提供的where标签可以很好的解决这个问题。
<select id="listBlogs" parameterType="map" resultType="Blog">
select * from blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
这个“where”标签会知道如果它包含的标签中有返回值的话(即存在where条件),它才会插入一个‘where’。此外,如果标签返回的内容是以“AND 或OR ”开头的,则它会剔除掉(如下代码所示)。
select * from blog
<where>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
set标签
类似于where标签,set标签可以很好的动态生成含set关键词的sql语句,set标签同样也可以自动识别并去除不需要的“,”。
/**
* 更新一篇博客
*
* @param map 参数map
*/
int updateBlog(Map<String, Object> map);
<update id="updateBlog" parameterType="map">
update blog
<set>
<if test="title != null">
title=#{title},
</if>
<if test="author != null">
author=#{author},
</if>
</set>
where id=#{id}
</update>
choose标签
如果有时候不想用到所有的查询条件,只想选择其中的一个(即查询条件有一个满足即可),使用 choose标签可以解决此类问题,类似于 Java 的 switch 语句。
/**
* 通过choose标签查询博客
*
* @param map map参数
* @return 返回数据库发生改变的行数
*/
List<Blog> getBlogChoose(Map<String, Object> map);
<select id="getBlogChoose" parameterType="map" resultType="blog">
select * from blog
<where>
<choose>
<when test="title != null">title=#{title}</when>
<when test="author != null">author=#{author}</when>
<otherwise>views=#{views}</otherwise>
</choose>
</where>
</select>
sql标签
有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用,这个工作可以由sql标签来完成。举个例子:
<select id="listBlogs" parameterType="map" resultType="Blog">
select * from blog
<where>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
<select id="listArticle" parameterType="map" resultType="Article">
select * from article
<where>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
可以看到上述两个sql存在相同的部分,而项目中可能会存在大量的重复部分,这时候把它们抽取出来即可复用:
<sql id="title-author">
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
<select id="listBlogs" parameterType="map" resultType="Blog">
select * from blog
<where>
<include refid="title-author"></include>
</where>
</select>
<select id="listArticle" parameterType="map" resultType="Article">
select * from article
<where>
<include refid="title-author"></include>
</where>
</select>
注意:①最好基于 单表来定义 sql 片段,提高片段的可重用性
②在 sql 片段中不要包括 where
foreach标签
先介绍foreach标签的各个属性,如下所示:
collection:指定输入对象中的集合属性
item:每次遍历生成的对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串,即分割符
index:遍历到当前元素的索引
然后,看一个实例:
<select id="getBlogForeach" parameterType="map" resultType="blog">
select * from blog
<where>
<foreach collection="ids" item="id" open="id in (" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
运行结果:
可见拼接出来的sql:select * from blog WHERE id in ( ? , ? , ? )