MyBatis-动态sql
十一、动态sql
动态SQL就是指根据不同的条件生成不同的SQL语句
动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合就可以了
动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
if
choose (when, otherwise)
trim (where, set)
foreach
1、环境搭建
- 创建数据库
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
-
新建一个空的模块
-
导入所需的依赖
-
编写mybatis-config.xml配置文件
-
编写实体类
@Data
public class Blog {
private String id;
private String title;
private String author;
private Date createTime;
private int views;
}
- 编写实体类对应Mapper接口以及Mapper.xml
- 测试运行
2、IF
- 接口
//查询博客
List<Blog> queryBlogIF(Map map);
- Mapper.xml
<select id="queryBlogIF" 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>
- 测试
//IF语句
@Test
public void queryBlogIF(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, String> map1=new HashMap<String,String>();
map.put("title","java");
List<Blog> blogList = mapper.queryBlogIF(map);
List<Blog> blogList1 = mapper.queryBlogIF(map1);
for (Blog blog : blogList) {
System.out.println(blog);
}
for (Blog blog : blogList1) {
System.out.println(blog);
}
sqlSession.close();
}
}
3、choose (when, otherwise)
<select id="queryBlogChoose" parameterType="map" resultType="Blog">
select * from blog
<where>
<choose>
<when test="title != null">
title=#{title}
</when>
<when test="author!=null">
author=#{author}
</when>
<when test="views!=null">
views=#{views}
</when>
</choose>
</where>
</select>
4、trim (where, set)
<!--prefixOverrides 属性会忽略通过管道符分隔的文本序列(注意此例中的空格是必要的)。上述例子会移除所有 prefixOverrides 属性中指定的内容,并且插入 prefix 属性中指定的内容。-->
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
<!--where-->
<select id="queryBlogIF" 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>
<!--覆盖了后缀值设置,并且自定义了前缀值。-->
<trim prefix="SET" suffixOverrides=",">
...
</trim>
<!--set-->
<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>
- where语句只会在至少有一个子元素的条件返回SQL子句的情况下才去插入“where”;而且如果开头的为“AND”,“OR”,where元素会将他们去除!
5、SQL片段
有的时候,我们可能会将一些功能的部分抽取出来,方便复用!
<!--使用SQL标签抽取公共的部分-->
<sql id="if-title-author">
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</sql>
<!--在需要使用的地方使用Include标签引用即可-->
<select id="queryBlogIF" parameterType="map" resultType="blog">
select * from mybatis.blog
<where>
<include refid="if-title-author"></include>
</where>
</select>
注意事项:
最好基于单表来定义SQL片段!
不要存在where标签
6、Foreach
动态 SQL 的另一个常见使用场景是对集合进行遍历(尤其是在构建 IN 条件语句的时候)
<select id="queryBlogForeach" parameterType="map" resultType="blog">
select * form blog
<where>
<foreach collection="ids" item="id" open="and(" close=")" separator="or">
id = #{id}
</foreach>
</where>
</select>
- oreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。