How to query mybits with second-level caching enabled?
To check the second-level cache enabled in MyBatis, you can use one of the following methods:
- Factory that creates SqlSessions
- retrieve the configuration
- Setting up
- retrieve the cache names
- retrieveCache(String cacheName)
// 获取SqlSessionFactory
SqlSessionFactory sqlSessionFactory = ...;
// 获取Configuration对象
Configuration configuration = sqlSessionFactory.getConfiguration();
// 获取所有缓存的名称
Set<String> cacheNames = configuration.getCacheNames();
// 遍历缓存名称并查询缓存对象
for (String cacheName : cacheNames) {
Cache cache = configuration.getCache(cacheName);
// 查询操作...
}
- Factory that creates SqlSessions
- initiate session
- Session used for interacting with a SQL database
- retrieve the configuration
- Setting up
- retrieve the cache with the specified name
// 获取SqlSessionFactory
SqlSessionFactory sqlSessionFactory = ...;
// 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取Configuration对象
Configuration configuration = sqlSession.getConfiguration();
// 获取特定名称的缓存对象
Cache cache = configuration.getCache("cacheName");
// 查询操作...
Before using the above method to query the second-level cache, make sure that MyBatis’ second-level cache has been correctly configured and enabled.