Home  >  Article  >  Java  >  What is the difference between mybatis first-level cache and second-level cache?

What is the difference between mybatis first-level cache and second-level cache?

coldplay.xixi
coldplay.xixiOriginal
2021-01-22 10:37:3548057browse

The difference between the first-level cache and the second-level cache of mybatis: 1. The first-level cache of Mybatis refers to SQLSession. The scope of the first-level cache is SQlSession. Mabits enables the first-level cache by default; 2. Mybatis The second level cache is not enabled by default.

What is the difference between mybatis first-level cache and second-level cache?

The operating environment of this article: Windows 7 system, MyBatis version 3.5.6, Dell G3 computer.

The difference between the first-level cache and the second-level cache of mybatis:

1) The first-level cache of Mybatis refers to SQLSession, and the scope of the first-level cache is SQlSession, Mabits enable 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 fetched directly from the cache. When SQL is executed and additions, deletions, and modifications occur between the two queries, the SQLSession cache will be cleared.

Each query will first search in the cache. If it cannot be found, it will query the database and then write the results to the cache. Mybatis's internal cache uses a HashMap, and the key is the hashcode statementId sql statement. Value is the java object mapped to the query result set. After SqlSession performs insert, update, delete and other operations commit, the SQLSession cache will be cleared.

2) Second-level cache The second-level cache is at the mapper level. 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 second-level 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 results will be retrieved from the corresponding second-level cache.

What is the difference between mybatis first-level cache and second-level cache?

Related recommendations: java introductory tutorial

The above is the detailed content of What is the difference between mybatis first-level cache and second-level cache?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn