How to implement first-level cache and second-level cache in Mybatis
Mybatis how to achieve a level of cache and secondary cache, in response to this problem, this article describes the corresponding analysis and solution in detail, hoping to help more small partners who want to solve this problem find a simpler and easier way.
Level 1 cache
The first level cache is a SqlSession level cache. When operating the database, you need to construct sqlSession object, in which there is a data structure (HashMap) for storing cached data. The cache data area (HashMap) between different sqlSessions is independent of each other.
The same query statement (with the same parameters) under the same SqlSession returns results from the cache; update statements (update, insert, delete) flush the cache.
II. Level 2 cache
The second level cache is based on the namespace of the mapper file, that is to say, multiple sqlSessions can share the second level cache area in a mapper, and if the namespaces of two Mappers are the same, even if it is two Mappers, then the data obtained by executing sql queries in the two Mappers will also exist in the same second level cache area.
Second level cache is based on namespace. If a user table has two Mappers with different namespaces querying and changing it, they are invisible to each other and easy to read expired data.
About Mybatis how to achieve the first level cache and the second level cache questions to share here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.