MyBatis 中的缓存就是说 MyBatis 在执行一次SQL查询或者SQL更新之后,这条SQL语句并不会消失,而是被MyBatis 缓存起来,当再次执行相同SQL语句的时候,就会直接从缓存中进行提取,而不是再次执行SQL命令。
缓存机制分为:
一级缓存:一级缓存是mybatsi已经为我们自动开启,不用我们手动操作,而且我们是关闭不了 的!!但是我们可以手动清除缓存。(SqlSession级别.提交事务,缓存清空)
一级缓存失效的情况: ①不是在同一个sqlsession下
@Test
// mybatis 的缓存 一级缓存失效 不是在同一个sqlsession下
public void test02() {
SqlSession sqlSession1 = factory.openSession();
SqlSession sqlSession2 = factory.openSession();
StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
StudentMapper mapper2 = sqlSession2.getMapper(StudentMapper.class);
List<Student> students = mapper1.selectAll();
students.forEach(student -> System.out.println("student = " + student));
System.out.println("----------------------------------------------");
students = mapper2.selectAll();
students.forEach(student -> System.out.println("student = " + student));
}
②在两次相同的查询之间进行1、手动清空缓存。2、手动提交事务
@Test
// mybatis 的缓存 一级缓存失效 在两次相同的查询之间 1.手动清空缓存 2.手动提交事务
public void test03() {
SqlSession sqlSession = factory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectAll();
students.forEach(student -> System.out.println("student = " + student));
// 手动清空缓存
//sqlSession.clearCache();
//手动提交事务 一级缓存清空
sqlSession.commit();
System.out.println("---------------------------------");
students = mapper.selectAll();
students.forEach(student -> System.out.println("student = " + student));
}
③在两次相同的查询之间(同一个sqlsession) 做增删改操作
ps:设置自动提交依然会使用缓存,并不会使缓存失效
@Test
// mybatis 的缓存 一级缓存失效 在两次相同的查询之间(同一个sqlsession) 做增删改操作
//ps:设置自动提交依然会使用缓存,并不会使缓存失效
public void test04() {
SqlSession sqlSession = factory.openSession(true);
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectAll();
students.forEach(student -> System.out.println("student = " + student));
//根据id进行删除学生信息
Integer integer = mapper.deleteById("HFYY66666");
System.out.println(integer > 0 ? "true" : "false");
System.out.println("-----------------------------------------");
students = mapper.selectAll();
students.forEach(student -> System.out.println("student = " + student));
}
二级缓存:二级缓存需要我们手动开启。(全局级别 SqlSessionFactory)
开启二级缓存需要两个步骤,
第一步在mybatis的全局配置文件中配置Setting属性,设置名为cacheEnabled的属性值为true即可
<settings>
<!--
(1):开启二级缓存,这个全局的配置二级缓存
默认是开启的,但是还是需要写上,防止版本的更新
-->
<setting name="cacheEnabled" value="true"/>
</settings>
第二步:在具体需要二级缓存的mapeer映射文件中开启二级缓存,值需要在相应的映射文件中添加一个cache标签即可
2):在相应的映射文件中开启二级缓存
<!-- 开启二级缓存 -->
<cache></cache>
3)查询数据封装的实体类要实现序列化的接口
4)二级缓存需要在一级缓存关闭或者提交后生效
二级缓存失效的条件:
1.在两次查询之间进行了任意的增删改操作,一级二级缓存同时失效
@Test
// mybatis 的缓存 二级缓存
public void test05() {
SqlSession sqlSession1 = factory.openSession();
SqlSession sqlSession2 = factory.openSession();
StudentMapper mapper1 = sqlSession1.getMapper(StudentMapper.class);
StudentMapper mapper2 = sqlSession2.getMapper(StudentMapper.class);
List<Student> students = mapper1.selectAll();
students.forEach(student -> System.out.println("student = " + student));
// 手动提交事务 将数的一级缓存 写入二级缓存
sqlSession1.commit();
//将一级缓存关闭也可以
// sqlSession1.close();
System.out.println("--------------------------------------");
//开启二级缓存封装对象必须要将其进行序列化操作第二次要将实体类进行序列化 不然会报错
students = mapper2.selectAll();
students.forEach(student -> System.out.println("student = " + student));
}
二、mybatis分页插件
1、现在pom.xml中导入分页查询依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.1</version>
</dependency>
2、在mybatis配置文件中 配置mybatis插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 配置mysql方言 -->
<property name="helperDialect" value="mysql" />
<!-- 设置为true时,如果pageSize=0就会查询出全部的结果 -->
<property name="pageSizeZero" value="true" />
<!-- 3.3.0版本可用,分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="true" />
</plugin>
</plugins>
3、案例
public class TestStudentPage {
SqlSessionFactory factory;
@Before
public void init() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
}
@Test
public void test01() {
SqlSession sqlSession = factory.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//使用分页插件 实现分页查询 page limit 1 ,10
PageHelper.startPage(1, 20);
List<Student> students = mapper.selectAll();
students.forEach(student -> System.out.println("student = " + student));
//将查询的结果封装到 pageInfo的对象中 可以获取更多的分页信息 总记录数
PageInfo<Student> pageInfo = new PageInfo<>(students);
long total = pageInfo.getTotal();
System.out.println("pageInfo = " + pageInfo);
}
}