什么是动态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 'title',
`author` varchar(30) NOT NULL COMMENT 'author',
`create_time` datetime NOT NULL COMMENT 'create_time',
`views` int(30) NOT NULL COMMENT 'views'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
INSERT INTO `blog` (`id`, `title`, `author`, `create_time`, `views`) VALUES ('1', 'Java SE-1', '巡山的小白', '2019-10-17 18:38:02', '100');
INSERT INTO `blog` (`id`, `title`, `author`, `create_time`, `views`) VALUES ('2', 'Java SE-2', '巡山的小白', '2019-10-16 18:39:22', '200');
INSERT INTO `blog` (`id`, `title`, `author`, `create_time`, `views`) VALUES ('3', 'Java SE-3', '小白的巡山', '2019-10-18 18:39:58', '200');
INSERT INTO `blog` (`id`, `title`, `author`, `create_time`, `views`) VALUES ('4', 'Java SE-4', '小白的巡山', '2019-10-15 18:40:46', '300');
INSERT INTO `blog` (`id`, `title`, `author`, `create_time`, `views`) VALUES ('5', 'Java 从入门到放弃', '小白不背锅', '2019-10-14 18:41:42', '1');
编写一个基础工程
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Blog {
private Integer id;
private String title;
private String author;
private Date createTime;
private Integer views;
}
介绍:
根据你给的条件(可以多个条件)拿到信息,相当于 Java 中的 IF 语句,if 条件可以叠加
//根据 map 集合存查询的条件
List<Blog> queryBlogIF(Map map);
<select id="queryBlogIF" resultType="Blog" parameterType="map">
select * from mybatis.blog where 1=1
<if test="id != null">
and id = #{id}
</if>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
<if test="createTime != null">
and create_time = #{createTime}
</if>
<if test="views != null">
and views = #{views}
</if>
</select>
@Test
public void queryBlogIF(){
SqlSession sqlsession = mybatisUtils.getSqlsession();
blogMapper mapper = sqlsession.getMapper(blogMapper.class);
HashMap map = new HashMap();
map.put("author","巡山的小白");
List<Blog> blogs = mapper.queryBlogIF(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlsession.close();
}
介绍:
根据你给的条件(可以多个条件,但是只匹配 xml 配置文件中靠前的条件)拿到信息,类似于 Java 的 switch-case 语句
List<Blog> queryBlogChoose(Map map);
<select id="queryBlogChoose" resultType="Blog" parameterType="map">
select * from mybatis.blog
<where>
<choose>
<when test="id != null">
id = #{id}
</when>
<when test="title != null">
and title = #{title}
</when>
<when test="author != null">
and author = #{author}
</when>
<when test="createTime != null">
and create_time = #{createTime}
</when>
<when test="views != null">
and views = #{views}
</when>
</choose>
</where>
</select>
@Test
public void queryBlogChoose(){
SqlSession sqlsession = mybatisUtils.getSqlsession();
blogMapper mapper = sqlsession.getMapper(blogMapper.class);
HashMap map = new HashMap();
map.put("author","小白的巡山");
map.put("view",200);
List<Blog> blogs = mapper.queryBlogChoose(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlsession.close();
}
介绍:
List<Blog> queryBlogTrime(Map map);
Integer updateBlogTrime(Map map);
<select id="queryBlogTrime" resultType="Blog" parameterType="map">
select * from mybatis.blog
<where>
<if test="id != null">
id = #{id}
</if>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
<if test="createTime != null">
and create_time = #{createTime}
</if>
<if test="views != null">
and views = #{views}
</if>
</where>
</select>
<update id="updateBlogTrime" parameterType="map">
update mybatis.blog
<set>
<if test="title != null">
title = #{title},
</if>
<if test="author != null">
author = #{author},
</if>
<if test="createTime != null">
create_time = #{createTime},
</if>
<if test="views != null">
views = #{views}
</if>
</set>
where id = #{id}
</update>
@Test
public void queryBlogTrime(){
SqlSession sqlsession = mybatisUtils.getSqlsession();
blogMapper mapper = sqlsession.getMapper(blogMapper.class);
HashMap map = new HashMap();
map.put("author","小白的巡山");
map.put("views",200);
List<Blog> blogs = mapper.queryBlogTrime(map);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlsession.close();
}
@Test
public void updateBlogTrime(){
SqlSession sqlsession = mybatisUtils.getSqlsession();
blogMapper mapper = sqlsession.getMapper(blogMapper.class);
HashMap map = new HashMap();
map.put("id",4);
map.put("author","小白不背锅");
Integer result = mapper.updateBlogTrime(map);
if(result>0){
System.out.println("更新成功...");
sqlsession.commit();
}else {
System.out.println("更新失败...");
}
sqlsession.close();
}
有的时候,我们会将一些常用的 CRUD 代码抽取出来,方便下次复用!
<sql id="If-Not-Null">
<if test="id != null">
id = #{id}
</if>
<if test="title != null">
and title = #{title}
</if>
<if test="author != null">
and author = #{author}
</if>
<if test="createTime != null">
and create_time = #{createTime}
</if>
<if test="views != null">
and views = #{views}
</if>
</sql>
<select id="queryBlogTrime" resultType="Blog" parameterType="map">
select * from mybatis.blog
<where>
<include refid="If-Not-Null"></include>
</where>
</select>
动态 SQL 的另外一个常用的操作需求是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。比如:
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
<select id="queryBlogForeach" resultType="Blog" parameterType="list">
select * from blog where id in
<foreach collection="list" item="BlogId" open="(" separator="," close=")">
#{BlogId}
</foreach>
</select>
public void queryBlogForeach(){
SqlSession sqlsession = mybatisUtils.getSqlsession();
blogMapper mapper = sqlsession.getMapper(blogMapper.class);
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
List<Blog> blogs = mapper.queryBlogForeach(list);
for (Blog blog : blogs) {
System.out.println(blog);
}
sqlsession.close();
}
查询 : 需要连接数据库 ,耗费资源!
如果我们将一次查询的结果,给他暂存在一个可以直接取到的地方!----> 缓存
我们再次查询相同数据的时候,直接走缓存,就不用走数据库了,这样就可以省时省力
测试步骤:
@Test
public void queryBlogs(){
SqlSession sqlsession = mybatisUtils.getSqlsession();
blogMapper mapper = sqlsession.getMapper(blogMapper.class);
List<Blog> blogs = mapper.queryBlogs();
System.out.println(blogs);
List<Blog> blogs1 = mapper.queryBlogs();
System.out.println(blogs1);
sqlsession.close();
}
缓存失效的情况:
步骤:
<!--在当前Mapper.xml中使用二级缓存-->
<cache/>
也可以自定义参数
<!--在当前Mapper.xml中使用二级缓存-->
<cache eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
@Test
public void queryBlogs(){
SqlSession sqlsession1 = mybatisUtils.getSqlsession();
blogMapper mapper1 = sqlsession1.getMapper(blogMapper.class);
SqlSession sqlsession2 = mybatisUtils.getSqlsession();
blogMapper mapper2= sqlsession2.getMapper(blogMapper.class);
Blog blog1 = mapper1.queryBlogById(1);
System.out.println(blog1);
//关闭第一个sqlsession
sqlsession1.close();
//让第二个sqlsession会话查询同一条数据
Blog blog = mapper2.queryBlogById(1);
System.out.println(blog);
sqlsession2.close();
}
Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存
org.mybatis.caches
mybatis-ehcache
1.1.0
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="./tmpdir/Tmp_EhCache"/>
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU"/>
<cache
name="cloud_user"
eternal="false"
maxElementsInMemory="5000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LRU"/>
ehcache>