8.1什么是动态SQL
- 动态SQL就是指根据不同的条件生成不同的SQL语句
- 例如拼接时要确保不能忘记添加必要的空格,还要注意去掉最后一个列表的逗号,利用动态SQL可以解决这个问题
- 动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
- 本质还是SQL语句 , 只是我们可以在SQL层面,去执行一个逻辑代码,使得语句不那么容易出错
- 拼接(组合)SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合就可以了
if
choose (when, otherwise)
trim (where, set)
foreach
8.2 测试
8.2.1搭建环境
- 创建表
CREATE TABLE `blog` (
`id` int NOT NULL COMMENT '博客id',
`title` varchar(45) NOT NULL COMMENT '博客标题',
`author` varchar(45) NOT NULL COMMENT '博客作者',
`create_time` datetime NOT NULL COMMENT '创建时间',
`views` int NOT NULL COMMENT '浏览量',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
- 创建项目
- 配置文件
- 编写实体类
- 编写对应接口和xml文件
- 测试
驼峰命名转换(Setting)
<setting name="mapUnderscoreToCamelCase" value="ture"/>
void addBlog(Blog blog);
public class Blog {
private int id;
private String title;
private String author;
private Date createtime;
private int views;
}
<mapper namespace="com.node.dao.BlogMapper">
<insert id="addBlog" parameterType="com.node.pojo.Blog">
insert into blog values (#{id},#{title},#{author},#{createtime},#{views})
</insert>
@Test
public void addBlog(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
Blog blog=new Blog();
blog.setId(1);
blog.setTitle("Mybatis好难!!!");
blog.setAuthor("厉害得很");
blog.setCreatetime(new Date());
blog.setViews(99999);
mapper.addBlog(blog);
blog.setId(2);
blog.setTitle("Spring好难!!!");
blog.setAuthor("厉害得很");
blog.setCreatetime(new Date());
blog.setViews(99999);
mapper.addBlog(blog);
blog.setId(3);
blog.setTitle("SpringMVC好难!!!");
blog.setAuthor("厉害得很");
blog.setCreatetime(new Date());
blog.setViews(99999);
mapper.addBlog(blog);
sqlSession.close();
}
- if
<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>
如果不写1=1,将会 where与and直接拼接起来,出错
- 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>
用于选择,只执行一个成功的,看map.put(); 的要求
- trim (where,set) 这个可以常用
select * from mybatis.blog
<where>
<if test="title != null">
title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
</where>
用于,如果第一个执行失败,直接执行第二个会出现where and 情况 这时 where会直接忽略and
- update
<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修改语句时会出现“,”,在java里会出错,set会自动忽略“,”
8.3 SQL片段
- foreach:集合进行遍历
List<Blog> forreach(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>
@Test
public void forreach(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
HashMap hashMap = new HashMap();
ArrayList<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
hashMap.put("ids",ids);
List<Blog> blogs = mapper.forreach(hashMap);
for (Blog blog:blogs){
System.out.println(blog);
}
sqlSession.close();
}