Mybatis cache

1. Level 1 cache
MyBatis enables the level 1 cache by default, and the level 1 cache is cached at the SqlSession level. That is, the same SqlSession, calling the same Mapper and the same parameter of the same method multiple times, will only perform a database query, and then cache the data in the buffer, and then directly retrieve the data from the cache, and will not directly check database.
There is a first-level cache by default, and the first-level cache is only for the case of using the same SqlSession.
The scope of the first-level cache has two types: SESSION and STATEMENT. The default is SESSION. If we do not need to use the first-level cache, then we can specify the scope of the first-level cache as STATEMENT, so that each time a Mapper statement is executed, a level cache clearing. If you need to change the scope of the first-level cache, please specify it through localCacheScope in the Mybatis configuration file.

  <setting name="localCacheScope" value="SESSION"/>

The following example

public static void main(String[] args) {
        // 自定义的单例SqlSessionFactory模式
        SqlSessionFactory factory = SqlSessionFactoryUtil.openSqlSession();

        // 获得SqlSession对象
        SqlSession sqlSession = factory.openSession();
        // 获得dao实体
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 进行两次相同的查询操作
        userMapper.selectByPrimaryKey(1);
        userMapper.selectByPrimaryKey(1);
        // 注意,当我们使用二级缓存时候,sqlSession需要使用commit时候才会生效
        sqlSession.commit();

        System.out.println("\n\n=============================================================");
        // 获得一个新的SqlSession 对象
        SqlSession sqlSession1 = factory.openSession();
        // 进行相同的查询操作
        sqlSession1.getMapper(UserMapper.class).selectByPrimaryKey(1);
        // 注意,当我们使用二级缓存时候,sqlSession需要使用commit时候才会生效
        sqlSession1.commit();
    }

2. L2 cache

The first-level cache is at the sqlSession level, and the second-level cache is at the mapper level. The
second-level cache is enabled by default. If you want to cancel, you can specify cacheEnabled as false through the sub-element under the element in the Mybatis configuration file.

  <setting name="cacheEnabled" value="false" />

You can manually configure some properties when the second level cache is enabled

<cache eviction="LRU" flushInterval="100000" size="1024" readOnly="true"/>

The meaning of each attribute is as follows:

eviction: cache recycling strategy LRU
: the principle of least use, remove objects that have not been used for the longest time The object WEAK: weak reference, more aggressive removal removes objects based on the garbage collector status and weak reference rules flushInterval: refresh interval, in milliseconds, 100 milliseconds configured here. If it is not configured, the cache area will be refreshed passively only when the database modification operation is performed. size: the number of references, which represents the maximum number of objects that the cache can store readOnly: whether it is read-only or not, if it is true, all the same SQL statements return It is the same object (helps to improve performance, but it may not be safe to operate the same data concurrently). If it is set to false, the same SQL will be accessed later by a clone copy of the cache. The willingness to access the second-level cache can be set under the specific method of Mapper:






useCache configuration
If a statement needs the latest data every time, it means that the data needs to be queried from the database every time, you can set this property to false, such as:

<select id="selectAll" resultMap="BaseResultMap" useCache="false">

Refresh the cache (that is, clear the cache)
The second-level cache will refresh the cache by default after insert, update, and delete operations. You can manually configure the cache not to update the cache, as follows:

<update id="updateById" parameterType="User" flushCache="false" />

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768484&siteId=291194637