I. Overview
MyBatis provides query caching, which can be used to reduce data pressure and improve system performance if there is data in the cache that is not retrieved from the database.
The first-level cache is a cache at the sqlsession level. The Sqlsession object needs to be constructed when manipulating the database, and there is a data structure (HASHMAP) in the object that stores the cached data. The cache data regions (HASHMAP) between different sqlsession are not affected by each other.
The second level cache is mapper level cache, multiple sqlsession to operate the same mapper SQL statement, multiple sqlsession can share a level two cache, level two cache is across sqlsession.
二、一级 Cache
For the first time, the user ID 1 is queried to find out if there is an ID of 1 user information in the cache, and if not, the user information is queried from the database.
Get the user information and store the user information in the first level cache.
If sqlsession goes to commit (INSERT, UPDATE, delete), empties the first-level cache in the sqlsession so that the cache is stored with the most up-to-date information and avoids dirty reads.
The second launch of the query user ID 1 user information, first to find out if the cache has ID 1 user information, the cache has, directly from the cache to obtain user information.
Mybatis first-level caching is supported by default.
1. Test A
Results:
Executes only one query statement.
2. Test 2
Results:
Sqlsession to perform INSERT, UPDATE, delete, the first level cache in Sqlsession is emptied.
MyBatis Basic Learning (v)-Caching