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:

  1. Factory that creates SqlSessions
  2. retrieve the configuration
  3. Setting up
  4. retrieve the cache names
  5. 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);
    // 查询操作...
}
  1. Factory that creates SqlSessions
  2. initiate session
  3. Session used for interacting with a SQL database
  4. retrieve the configuration
  5. Setting up
  6. 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.

bannerAds