MyBatis之动态sql
程序员文章站
2022-07-12 22:38:53
...
一、动态SQL
概念:
动态SQL就是指根据不同的条件生成不同的SQL语句;
动态SQL本质还是sql语句,只不过在SQL层面,去执行一个逻辑代码
二、创建一个基础工程
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
2.每一个数据库表独有对应的pojo类;
3.每一个实体类都有一个对应的Mapper接口;
4.每一个Mapper接口都有一个对应的Mapper.xml文件。
三、动态SQL对应的标签
1、IF
动态 SQL 通常要做的事情是有条件地包含 where 子句的一部分;
(1)BlogMapper.java
List<Blog> queryBlogIF(Map map);
(2)BlogMapper.xml
<select id="queryBlogIF" parameterType="map" resultType="Blog">
select * from mybatis.blog where
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</select>
(3)BlogMapperTest.java
public void queryBlogIF(){
SqlSession session = MyBatisUtils.getSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
Map<String, String> map = new HashMap<String, String>();
map.put("title","MyBatis so easy");
map.put("author","胖虎");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
}
(4)运行结果
2、choose (when, otherwise)
有些时候,我们不想用到所有的条件语句,而只想从中择其一二。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。
(1)BlogMapper.java
List<Blog> queryChoose(Map map);
(2)BlogMapper.xml
<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>
(3)BlogMapperTest.java
public void queryBlogChoose(){
SqlSession session = MyBatisUtils.getSession();
BlogMapper mapper = session.getMapper(BlogMapper.class);
Map<String, String> map = new HashMap<String, String>();
map.put("title","MyBatis so easy");
map.put("author","胖虎");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
}
(4)运行结果
什么条件都不加:
加了一个条件:
加了两个条件:
3、trim (where,set)
where元素只会在至少有一个子元素的条件返回SQL子句的情况下才能去插入“WHERE”子句,而且,若语句的开头为“And”和“Or”,where也会将它们去除。
BlogMapper.java
List<Blog> queryBlogIF(Map map);
(2)BlogMapper.xml
<select id="queryBlogIF" parameterType="map" resultType="Blog">
select * from mybatis.blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
</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>
(3)BlogMapperTest.java
public void queryBlogIF(){
SqlSession session = MyBatisUtils.getSession(true);
BlogMapper mapper = session.getMapper(BlogMapper.class);
Map<String, String> map = new HashMap<String, String>();
//map.put("title","MyBatis so easy");
//map.put("author","胖虎");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
session.close();
}
(4)运行结果
不加条件:
一个条件:
两个条件:
三个条件:
4、Foreach
对集合进行遍历:
BlogMapper.xml
<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>
什么都没写:
public void queryBlogIF(){
SqlSession session = MyBatisUtils.getSession(true);
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap map = new HashMap();
ArrayList<String> ids = new ArrayList<String>();
map.put("ids",ids);
List<Blog> blogs = mapper.queryBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
session.close();
}
运行结果:
写了一个id:
多个id:
public void queryBlogIF(){
SqlSession session = MyBatisUtils.getSession(true);
BlogMapper mapper = session.getMapper(BlogMapper.class);
HashMap map = new HashMap();
ArrayList<String> ids = new ArrayList<String>();
ids.add("22a523cdc0b24a69b9a184327fc1ae10");
ids.add("1a3f3db3a9cb465d80d2e1adac60bdc0");
ids.add("8cc96f8255894ab68409c8525a874560");
ids.add("7dd8ae2ab8d74559a5d072d269a776fc");
ids.add("fe7e3ce3926d47bbb6800859fb8faf82");
map.put("ids",ids);
List<Blog> blogs = mapper.queryBlogForeach(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
session.close();
}
运行结果: