24.MyBatis缓存

 

Hibernate和MyBatis缓存对比

框架 缓存 作用范围 默认开启
Hibernate 一级缓存 Session
二级缓存 SessionFactory
MyBatis 一级缓存 SqlSession
二级缓存 SqlSessionFactory

MyBatis缓存

MyBatis一级缓存

在SqlSession范围内,当查询相同的对象时,会使用缓存中的数据,而不会再次查询。

只要SqlSession不关闭,使用SqlSession去查询同一个对象,就只会执行一条sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.course.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.course.dao.DepartmentDao;
import com.course.util.MyBatisUtil;
import com.course.vo.Department;

public class DepartmentTest {

private SqlSession sqlSession = MyBatisUtil.openSqlSession();
private DepartmentDao departmentDao = sqlSession.getMapper(DepartmentDao.class);

@Test
public void getDepartmentById() {
Integer departmentId = 1;
Department department1 = departmentDao.getDepartmentById(departmentId);
Department department2 = departmentDao.getDepartmentById(departmentId);

System.out.println(department1);
System.out.println(department2);

}
}
1
2
3
4
5
DEBUG [main] - ==>  Preparing: select * from department where department_id = ? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
Department [department_id=1, department_name=Retail Department, location=5 floor, employees=null]
Department [department_id=1, department_name=Retail Department, location=5 floor, employees=null]

只执行了一次查询

MyBatis二级缓存

作用范围为SqlSessionFactory,需要手动启用。

Mapper.xml中添加<cache></cache>标签,该Mapper.xml中的所有sql都会开启二级缓存,不仅<select>,包括<insert><update><delete>都会开启二级缓存。

如果想要让某个sql不开启二级缓存,添加属性useCache="false",默认值为true

Mapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.course.dao.abc.UserDao">

<cache></cache>

<insert id="addUserXml" parameterType="com.course.vo.User">
insert into user(name, age, address, birthday)
values(#{name}, #{age}, #{address}, #{birthday})
</insert>

<update id="updateUser" parameterType="com.course.vo.User">
update user set name=#{name}, age=#{age}, address=#{address}, birthday=#{birthday}
where id=#{id}
</update>

<delete id="deleteUser" parameterType="int">
delete from user
where id=#{val}
</delete>

<select id="getUserById" parameterType="int" resultType="com.course.vo.User">
select * from user where id=#{val}
</select>

<select id="getAllUser" useCache="false" resultType="com.course.vo.User">
select * from user
</select>
</mapper>

mybatis.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<!-- MyBatis的整体配置 -->
<configuration>

<settings>
<!-- 开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>

</configuration>

MyBatis和Hibernate的比较

Hibernate: 自动生成表,生成关系,对于单表的CRUD,不用写sql和hql

mybatis: 对于多表连接查询等等更加方便,因为sql写起来简单

Mybatis半自动化模式操作数据,hibenate完全面向对象操作数据

Mybatis运行的性能高于hibernate的性能

Mybatis开发速度低于hibernate的开发速度

Mybatis应用在互联网项目的开发,Hibernate用在传统的MIS(Management Information System,管理信息系统)系统开发