动态SQL语句
Mybatis 的动态 SQL 语句
Mybatis 的映射文件中,前面我们的 SQL 都是比较简单的,
有些时候业务逻辑复杂时,我们的 SQL 是动态变 化的,
此时在前面的学习中我们的 SQL 就不能满足要求了
什么是动态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
编写实体类
@Data
public class Blog {
private int id;
private String title;
private String author;
private Date createTime;
private int views;
}
动态SQL语句之if标签
我们根据实体类的不同取值,使用不同的 SQL 语句来进行查询。
比如在 id 如果不为空时可以根据 id 查询,
如果 username 不同空时还要加入用户名作为条件。
这种情况在我们的多条件组合查询中经常会碰到。
<select id="queryBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
注意:<if>标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法。
另外要注意 where 1=1 的作用~
动态SQL语句之foreach标签
传入多个 id 查询用户信息,用下边两个 sql 实现:
SELECT * FROM USER WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
SELECT * FROM USER WHERE username LIKE '%张%' AND id IN (10,89,16)
这样我们在进行范围查询时,就要将一个集合中的值,作为参数动态添加进来。 这样我们将如何进行参数的传递?
在 QueryVo 中加入一个 List 集合用于封装参数
public class QueryVo implements Serializable {
private List<Integer> ids;
}
<!--
select * from mybatis.blog where 1=1 and (id=1 or id = 2 or id=3)
我们现在传递一个万能的map , 这map中可以存在一个集合!
-->
<select id="queryBlogForeach" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<foreach collection="ids" item="id" open="and (" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
choose (when, otherwise)
<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)
<select id="queryBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog where 1=1
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
<update id="updateBlog" parameterType="map">
update mybatis.blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author}
</if>
</set>
where id = #{id}
</update>
所谓的动态SQL,本质还是SQL语句 , 只是我们可以在SQL层面,去执行一个逻辑代码
抽取重复的SQL代码片段
1.我们在映射文件中编写增删改查的配置时,写的一些SQL语句有一些片段是重复的 比如 select * from user
那么我们可以把这些重复的sql代码抽取处理,就可以 复用
- 我们在映射文件中可以使用一个sql标签来抽象重复的sql代码
<sql id="mysql">
select * from user <!--注意:后面不要写分号-->
</sql>
3.在其他地方,有使用到select * from user 这条语句的地方,就可以通过id来引用,引用要使用一个标签
<select id="findAll" resultType="user">
<!--查询所有 就可以引用抽取的sql代码片段-->
<include refid="mysql"></include>
</select>
在比如下面也可以引用sql代码片段
<select id="selectUserByNameAndSex" resultType="user" parameterType="user">
<include refid="mysql"></include>
<where>
<if test="username!='' and username!=null">
and username=#{username}
</if>
<if test="sex!='' and sex!=null">
and sex=#{sex}
</if>
</where>
</select>