这篇帖子主要讲一级缓存,它的作用范围和源码分析
(本来想把一二级缓存合在一起,发现太长了)
public class Department {
public Department(String id) {
this.id = id;
}
private String id;
/**
* 部门名称
*/
private String name;
/**
* 部门电话
*/
private String tel;
/**
* 部门成员
*/
private Set users;
}
public class User {
private String id;
private String name;
private Integer age;
private LocalDateTime birthday;
private Department department;
}
public static void main(String[] args) throws IOException {
InputStream xml = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 开启二级缓存需要在同一个SqlSessionFactory下,二级缓存存在于 SqlSessionFactory 生命周期,如此才能命中二级缓存
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(xml);
SqlSession sqlSession = sqlSessionFactory.openSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println("----------department第一次查询 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------department一级缓存生效,控制台看不见SQL ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
}
可以发现控制台在第二次查询的时候,一级缓存生效,没有出现SQL
我们清空下一级缓存再试试
xml文件中flushCache标签 会清除所有namespace 的一级缓存 和 当前namespace 的二级缓存均会清除 默认是false
public static void main(String[] args) throws IOException {
InputStream xml = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 开启二级缓存需要在同一个SqlSessionFactory下,二级缓存存在于 SqlSessionFactory 生命周期,如此才能命中二级缓存
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(xml);
SqlSession sqlSession = sqlSessionFactory.openSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
System.out.println("----------department第一次查询 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------department一级缓存生效,控制台看不见SQL ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------清除一级缓存 ↓------------");
departmentMapper.cleanCathe();
System.out.println("----------清除后department再一次查询,SQL再次出现 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
}
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession1 = sqlSessionFactory.openSession();
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
DepartmentMapper departmentMapper1 = sqlSession1.getMapper(DepartmentMapper.class);
System.out.println("----------department第一次查询 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------sqlSession1下department执行相同的SQL,控制台出现SQL ↓------------");
departmentMapper1.findById("18ec781fbefd727923b0d35740b177ab");
Mybatis顶层的缓存是接口Cache,查看它的实现类
发现大部分实现类的包都是decorators(装饰器),只有PerpetualCache是Impl,所以我们确定的说,它就是我们要找的缓存实现类,点进去看看,发现只是组合了HashMap…
public class PerpetualCache implements Cache {
private final String id;
// 看这里
private final Map
public class DefaultSqlSession implements SqlSession {
private final Configuration configuration;
private final Executor executor;
private final boolean autoCommit;
private boolean dirty;
private List> cursorList;
...
}
复制代码
发现并没有哇!DefaultSqlSession还有两个东西,Configuration是全局的配置,这里边儿应该是没有,那我们只能再去Executor里看看了
发现它是个接口,实现类有一个CachingExecutor!立马点进去!
public class CachingExecutor implements Executor {
private final Executor delegate;
private final TransactionalCacheManager tcm = new TransactionalCacheManager();
...
}
复制代码
发现还是没有???
但是Executor还有一个BaseExecutor,最后一家了,再在没有关了Idea睡觉了
public abstract class BaseExecutor implements Executor {
private static final Log log = LogFactory.getLog(BaseExecutor.class);
protected Transaction transaction;
protected Executor wrapper;
protected ConcurrentLinkedQueue deferredLoads;
// o??!! 不就在这呢嘛,小老弟
protected PerpetualCache localCache;
protected PerpetualCache localOutputParameterCache;
protected Configuration configuration;
...
}
public List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
if (closed) {
throw new ExecutorException("Executor was closed.");
}
// 是否需要清除一级缓存
if (queryStack == 0 && ms.isFlushCacheRequired()) {
clearLocalCache();
}
List list;
try {
queryStack++;
// 查询一级缓存中是否存在数据
list = resultHandler == null ? (List) localCache.getObject(key) : null;
if (list != null) {
// 有数据直接取一级缓存
handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
} else {
// 没有数据则去数据库中查
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
} finally {
queryStack--;
}
if (queryStack == 0) {
for (DeferredLoad deferredLoad : deferredLoads) {
deferredLoad.load();
}
deferredLoads.clear();
// 全局localCacheScope设置为statement,则清空一级缓存
if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
clearLocalCache();
}
}
return list;
}
System.out.println("----------department第一次查询 ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println("----------department一级缓存生效,控制台看不见SQL ↓------------");
departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
复制代码
哎,很对,第一次果然去数据库里查了
哎,更对了,第二次果然取得缓存
好嘛,真简单呀
System.out.println("----------department第一次查询 ↓------------");
Department department = departmentMapper.findById("18ec781fbefd727923b0d35740b177ab");
System.out.println(department);
department.setName("方圆把名字改了");
System.out.println("----------department一级缓存生效,控制台看不见SQL ↓------------");
System.out.println(departmentMapper.findById("18ec781fbefd727923b0d35740b177ab"));
第一次查询结果name为null,之后我们修改它的name,第二次查询取缓存的结果是更改name结果之后的
这是因为存放的数据其实是对象的引用,导致第二次从一级缓存中查询到的数据,就是我们刚刚改过的数据
一级缓存到这里就要跟大家说再见了,做个总结吧