表结构:
create table grade(
gid number primary key,gname varchar2(20),
sex number(1) //0男1女
);
create table student(
sid number primary key,
sname varchar2(20),
gid number references grade(gid)
);
mybatis:默认将缓存写在应用程序本地 ,读写缓存 占用程序的内存和cpu
选择分布式缓存方案
一级缓存:为sqlSession级别的缓存 默认开启 (内嵌缓存)
相同的sqlsession对象
查询相同条件的结果时存在一级缓存只会查询一次,
sqlSession关闭后缓存失效 调用cleanCache后 缓存被清除,执行过增删改后缓存会被清除第一次查询时, 查询数据库 获取数据后通过sqlSession设置到一级缓存中
第二次查询时 通过sqlSession一级缓存判断是否存在相同主键的数据值
如果存在 直接返回引用 否则查询数据库二级缓存:为sqlSessionFactory级别的缓存 默认开启
同一个sessionFactory下产生的不同session
查询条件相同的数据,数据可以共享
配置文件:
<settings><setting name="cacheEnabled" value="true"/> //false关闭缓存
</settings>
映射文件:
mybatis默认缓存类 FifoCache :
内存空间不足时 需要一种机制保证内存不溢出
FIFO(默认):
需要将旧的的数据清除(最先插入的数据最先清除)
LRU:最近使用次数最少的,优先删除
LFU:最近一段时间内使用次数最少,优先删除
eviction="FIFO"//回收策略为先进先出
flushInterval="60000" //自动刷新时间60s
size=“512” //最多缓存512个引用对象readOnly="true" //只读
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" >
</cache>接口:
public interface StudentMapper {
public List<Student> queryStudentBySid(Integer sid);
}
映射文件:
<?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="cn.et.lesson5.xml.StudentMapper">
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" >
</cache>
<select id="queryStudentBySid" resultType="map" useCache="true">
select * from student where sid=#{0}
</select>
</mapper>
测试类:
private SqlSession getSession() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson5/xml/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
// session操作的是指向sql语句的一个唯一标识符
SqlSession openSession = sqlSessionFactory.openSession();
return openSession;
}
private SqlSessionFactory getSessionFactory() throws IOException {
// mybatis核心配置文件路径
String resource = "cn/et/lesson5/xml/mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// 工厂类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
return sqlSessionFactory;
}
/*
* 一级缓存:同一个session对象针对同一份数据的查询 产生的缓存 第一次查询时, 查询数据库 获取数据后通过session设置到一级缓存中
* 第二次查询时 通过session一级缓存判断是否存在相同主键的数据值 如果存在 直接返回引用 否则查询数据库
*/
@Test
public void selectStudent() throws IOException {
SqlSession openSession = getSession();
StudentMapper mapper = openSession.getMapper(StudentMapper.class);
List<Student> stu = mapper.queryStudentBySid(1);
List<Student> stu1 = mapper.queryStudentBySid(2);
System.out.println(stu == stu1);
}
// 二级缓存:同一个sessionFactory下产生的不同session可以共享数据
@Test
public void selectStudent1() throws IOException {
SqlSessionFactory factory = getSessionFactory();
SqlSession openSession = factory.openSession();
SqlSession openSession1 = factory.openSession();
StudentMapper mapper = openSession.getMapper(StudentMapper.class);
mapper.queryStudentBySid(1);
openSession.close();
StudentMapper mapper1 = openSession1.getMapper(StudentMapper.class);
mapper1.queryStudentBySid(1);
}
}