[Mybatis source code analysis] first-level cache and second-level cache source code analysis

The previous article "[Mybatis source code analysis] mapper instantiation and execution process source code analysis" mainly explained the basic principles of Mybatis and the first-level execution process. This chapter will talk about the two caches of Mybatis: first-level cache and second-level cache.

Because most of the Internet uses the xml configuration method to use the cache, so let's explain the annotation method here.

一级缓存
The first level cache is the SqlSession level. The scope of the first-level cache is SqlSession, and Mabits enables the first-level 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 the SQL is executed, there are additions, deletions, and modifications between the two queries, and the cache of the SqlSession will be cleared.

The first-level cache Mybatis uses a HashMap inside, and the key is hashcode+statementId+sql statement. Value is the java object mapped to the query result set. SqlSession will clear the SqlSession cache after performing operations such as insert, update, and delete.

  1. The life cycle of MyBatis level 1 cache is consistent with that of SqlSession. The localCache will be emptied before each update is executed.

  2. The internal design of the MyBatis level-1 cache is simple, but it has no capacity limit

Guess you like

Origin blog.csdn.net/CSDN_SAVIOR/article/details/129078396