一级缓存
在一个sqlSession中,对User表根据id进行两次查询,发现只打印一条查询语句
在两次查询中加入增删改会触发两次查询,因为清空了一级缓存
mybatis一级缓存默认开启,由BaseExecutor的createCacheKey方法来执行
CacheKey cacheKey = new CacheKey();
//MappedStatement 的 id
// id就是Sql语句的所在位置包名+类名+ SQL名称 cacheKey.update(ms.getId());
// offset 就是 0
cacheKey.update(rowBounds.getOffset());
// limit 就是 Integer.MAXVALUE cacheKey.update(rowBounds.getLimit());
//具体的SQL语句
cacheKey.update(boundSql.getSql());
//后面是update 了 sql中带的参数
cacheKey.update(value);
...
if (configuration.getEnvironment() != null) {
// issue #176 cacheKey.update(configuration.getEnvironment().getId()); }
在查询时先从缓存中查询是否有存储过,没有再去queryFromDatabase中查询
list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
二级缓存
二级缓存是namespace基于的,多个sqlSession可以共享二级缓存
开启方式:在sqlMapConfig.xml设置
<!--开启二级缓存-->
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
在UserMapper.xml文件中开启缓存
<!--开启二级缓存--> <cache></cache>
二级缓存本质还是map集合,开启了二级缓存后,还需要将要缓存的pojo实现Serializable接口,为了将缓存数据取出执行反序列化操作,因为二级缓存数据存储介质多种多样,不一定只存在内存中,有可能存在硬盘中,如果我们要再取这个缓存的话,就需要反序列化了。所以mybatis中的pojo都去实现Serializable接口
spring整合mybatis后导致一级缓存失效问题
spring对sqlSession的使用由sqlSessionTemplate控制,sqlSession被当作上下文存在了当前线程的threadlocal里,所以当查询结束,线程销毁,下次查询生成新的sqlSession导致一级缓存失效
public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) {
notNull(sessionFactory, NO_SQL_SESSION_FACTORY_SPECIFIED);
notNull(executorType, NO_EXECUTOR_TYPE_SPECIFIED);
SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
// 首先从SqlSessionHolder里取出session
SqlSession session = sessionHolder(executorType, holder);
if (session != null) {
return session;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Creating a new SqlSession");
}
session = sessionFactory.openSession(executorType);
registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session);
return session;
}
getSqlSession里面维护了个SqlSessionHolder,关联了事务与session,如果存在则直接取出,否则则新建个session,所以在有事务的里,每个session都是同一个,故能用上缓存了