The difference between Mybatis first level cache and second level cache

the difference

1. Level 1 cache The level 1 cache of Mybatis refers to SQLSession, the scope of level 1 cache is SQlSession, and Mabits enables level 1 cache by default. In the same SqlSession, when executing the same SQL query; the first time it will query the database and write it in the cache, and the second time it will be directly fetched from the cache. When an addition, deletion, or modification occurs between two queries during SQL execution, the SQLSession cache will be cleared. Each query will first go to the cache to find it. If it cannot be found, it will go to the database to query, and then write the result to the cache. The internal cache of Mybatis uses a HashMap, and the key is hashcode+statementId+sql statement. Value is the java object mapped to the query result set. After SqlSession executes insert, update, delete and other operations to commit, the SQLSession cache will be cleared.

2. Second-level cache The second-level cache is at the mapper level, and Mybatis does not enable the second-level cache by default. The first time the SQL under the mapper is called to query the user's information, the queried information will be stored in the secondary cache area corresponding to the mapper. In the second call to the mapper mapping file under the namespace, the same sql is used to query user information, and the result will be fetched from the corresponding secondary cache.

insert image description here

Guess you like

Origin blog.csdn.net/xiaowanzi_zj/article/details/127490689