- 缓存概念
- 一级缓存
- 二级缓存
- 可能遇到的问题
缓存概念 一级缓存
public void getBlogByID(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
blogMapper mapper = sqlSession.getMapper(blogMapper.class);
Blog blogById1 = mapper.getBlogById("1");
Blog blogById2 = mapper.getBlogById("1");
System.out.println(blogById1);
System.out.println(blogById2);
System.out.println("======================");
System.out.println(blogById1==blogById2);
sqlSession.close();
}
二级缓存即使增删改的数据不是缓存中的数据,缓存也会刷新
手动刷新缓存:sqlSession.clearCache();
public void getBlogById(){
SqlSession sqlSession1 = MybatisUtils.getSqlSession();
SqlSession sqlSession2 = MybatisUtils.getSqlSession();
blogMapper mapper1 = sqlSession1.getMapper(blogMapper.class);
Blog blogById1 = mapper1.getBlogById("1");
System.out.println(blogById1);
sqlSession1.close();
blogMapper mapper2 = sqlSession2.getMapper(blogMapper.class);
Blog blogById2 = mapper2.getBlogById("1");
System.out.println(blogById2);
System.out.println("==========================");
System.out.println(blogById1==blogById2);
sqlSession2.close();
}
sql查询的顺序
如果开启缓存时直接写;没有自定义配置,可能会报实体类没有序列化的错误
将实体类序列化即可



