Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of MyBayis Cache in java Application Development

Shulou Source: shulou.com Published: 2022-06-03 13:49:32 10月22日 Update

This article mainly shows you the "sample analysis of MyBayis cache in java application development", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample analysis of MyBayis cache in java application development".

What is MyBatis caching

So that the cache can reduce the number of interactions between Java and the database, so as to improve the efficiency of the program. If an object with id = 1 is queried, the object will be automatically saved to the cache after the first query. In the current query, the object can be directly removed from the cache without having to access the database again.

MyBatis cache category 1, level cache: SqlSession level, which is on by default and cannot be turned off. (on by default)

When operating the database, you need to create a SqlSession object, in which there are "HashMap" to store cached data, and the cached data areas between different SqlSession do not affect each other. The scope of the cache is SqlSession-wide. When the same SQL statement is executed twice in the same SqlSession, the result will be saved to the cache after the first execution, and will be directly obtained from the cache when the query is completed. It is important to note that if SqlSession performs DML operations (insert, update, delete), MyBatis must empty the cache to ensure data accuracy.

2. Second-level cache: Mapper level, which is turned off by default and can be enabled.

When using multiple SqlSession to operate the database with the same SQL statement of Mapper, the resulting data will be stored in the cache area, which also allows the HashMap to enter into the data storage. The scope of the cache is wider than that of the cache. Multiple SqlSession can be cached at the same level, and the cache is cross-SqlSession. The cache is shared by multiple SqlSession, and its domain is the same namespace of Mapper. If different SqlSession executes the same SQL statement under the same namespace twice, and the parameters are equal, the data will be saved to the cache after the first execution, and the data can be directly extracted from the cache for the first time.

How to use the second-level cache 1.1config.xml configuration that comes with MyBatis to enable level-level cache settings > 1.2Mapper.xml configure level-level cache 1.3 entity classes to implement serialization interface @ Data@AllArgsConstructor@NoArgsConstructorpublic class Account implements Serializable {private long id; private String username; private String password; private int age } 2. Ehcache second-level cache (third-party) 2.1pom.xml add related dependencies org.mybatis mybatis-ehcache 1.0.0 net.sf.ehcache ehcache-core 2.4.32.2 add ehcache.xml 2.3config.xml configuration on level cache 2.4 Mapper.xml configure level cache above is all the contents of this article "sample Analysis of MyBayis Cache in java Application Development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Cache data object database query configuration application development example analysis application development content multiple article statement different same level scope storage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple macOS MariaDB MySQL