动态SQL
什么是动态SQL?
动态SQL就是指根据不同的条件生成不同的SQL语句。
动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
- if
- choose (when, otherwise)
- trim (where, set)
- foreach
环境搭建
CREATE TABLE `blog` (
`id` VARCHAR(50) NOT NULL COMMENT '博客id',
`title` VARCHAR(100) NOT NULL COMMENT '博客标题',
`author` varchar(30) NOT NULL COMMENT '博客作者',
`create_time` datetime NOT NULL COMMENT '创建时间',
`views` INT(30) NOT NULL comment '浏览量'
) ENGINE=INNODB DEFAULT CHARSET=utf8
创建一个基础工程
导包
编写核心配置文件和工具类
原来的工具类省略,新增工具类:IdUtil.java/** * 用于获取随机的id */ public class IdUtil { public static String getId(){ return UUID.randomUUID().toString().replaceAll("-", ""); } }
- 编写实体类
@Data public class Blog { private String id; private String title; private String author; private Date CreateTime; private int views; }
- 编写实体类对应的 Mapper 接口和 Mapper.xml文件
if
案例:根据不同的条件查询博客
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from mybatis.blog
<!-- where会根据条件自动补充,或去掉and -->
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</select>
choose (when, otherwise)
有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
<select id="queryBlogChoose" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<choose>
<when test="title != null">
title = #{title}
</when>
<when test="author != null">
and author = #{author}
</when>
<otherwise>
and views = #{views}
</otherwise>
</choose>
</where>
</select>
trim (where, set)
where 和 set 标签会智能判断你的sql 语句。
当使用where时,你查询语句的第一个条件不满足,它会自动去掉下一个满足条件中开始的and 关键字,如果没有条件满足,where就不会出现。【上面两个示例中存在这种情况】
当时用set时,会动态前置set关键字,你更新语句的最后一个条件末尾存在逗号时,它会自动给你去掉。
<update id="updateBlog" parameterType="map">
update mybatis.blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != author">
author = #{author}
</if>
</set>
where id = #{id}
</update>
where 和 set 的 trim 形式:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
<trim prefix="SET" suffixOverrides=",">
...
</trim>
prefixOverrides
属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。它的作用是移除所有指定在 prefixOverrides
属性中的内容,并且插入 prefix
属性中指定的内容。
suffixOverrides
属性与 prefixOverrides
类似。
foreach
案例:查询第1-2-3条记录
这里为了方便,把id修改为1,2,3,4。
<!--
1. select * from mybatis.blog where 1=1 and (id=1 or id=2 or id=3);
2. select * from mybatis.blog where 1=1 and id in (1,2,3);
我们现在传递的是一个map,在map中可以存放一个集合ids。
-->
<select id="queryBlogForeach" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id" open="(" close=")" separator="or">
id= #{id}
</foreach>
</where>
</select>
sql片段
有的时候,我们可以将一些相同的sql片段抽取出来方便复用,使用的就是 sql
标签.
以第一个查询为例:
<sql id="if_title_author">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
<select id="queryBlogIf" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<include refid="if_title_author"/>
</where>
</select>
在其他相同代码的地方,也可以使用 include
标签引用我们的sql片段。