Please indicate the source for reprinting:
As mentioned earlier: Spring+SpringMVC+MyBatis in-depth learning and construction (7)-MyBatis delayed loading
Mybatis provides query caching to reduce database pressure and improve database performance.
Mybatis provides first-level cache and second-level cache.
The first-level cache is a SqlSession-level cache. When operating the database, you need to construct a sqlSession object. There is a data structure (HashMap) in the object for storing cache data. The cache data areas (HashMap) between different sqlSession do not affect each other.
The second-level cache is a mapper-level cache. Multiple sqlSession operates the same Mapper's sql statement. Multiple sqlSession can share the second-level cache. The second-level cache is across sqlSession.
Why use caching?
If there is data in the cache, there is no need to obtain it from the database, which greatly improves system performance.
@Testpublic void testCache1() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); UserMapper userMapper=sqlSession.getMapper(UserMapper.class);//下边查询使用一个SqlSession//第一次发起请求,查询id为1的用户User user1=userMapper.findUserById(1); System.out.println(user1); //如果sqlSession去执行commit操作(执行插入、更新、删除),清空sqlSession中的一级缓存,//这样做的目的是为了让缓存中存储的是最新的信息,避免脏读。//更新user1的信息user1.setUsername("测试用户22"); userMapper.updateUser(user1);//执行commit操作去清空缓存 sqlSession.commit(); //第二次发起请求,查询id为1的用户User user2=userMapper.findUserById(1); System.out.println(user2); sqlSession.close(); }2.3 Level 1 Cache Application The official development is to integrate mybatis and spring, and the transaction is controlled in the service. A service method includes many Mapper method calls. service{ //When execution starts, start the transaction and create the SqlSession object //The first time the mapper method is called findUserById(1) //Call the mapper method findUserById(1) for the second time to get data from the first-level cache //The method ends and sqlSession is closed}If two service calls are executed to query the same user information, the first-level cache will not be used. Because the session method ends, sqlSession will be closed and the first-level cache will be cleared. 3. Second-level cache3.1 Second-level cache principle First enable the second-level cache of mybatis. sqlSession1 queries the user information with user ID 1. After querying the user information, it regrets that the query data is stored in the secondary cache. If sqlSession3 executes SQL under the same mapper and executes commit submission, the data in the second-level cache area under the mapper will be cleared. sqlSession2 queries the user information with user ID 1, checks whether data exists in the cache, and if so, directly retrieves the data from the cache. The difference between the second-level cache and the first-level cache: the scope of the second-level cache is larger, and multiple sqlSession can share a UserMapper's second-level cache area. UserMapper has a second-level cache area (divided by namespace), and other mappers also have their own second-level cache areas (divided by namespace). Each namespace mapper has a second-level cache area. If the namespaces of two mappers are the same, the data obtained by the two mappers executing SQL queries will be stored in the same second-level cache area. 3.2 Turn on the second-level cache Mybatis’s second-level cache is mapper scope level. In addition to setting the main switch of the second-level cache in SqlMapConfig.xml, it must also be turned on in the specific mapper.xml. Level 2 cache. Add to the core configuration file SqlMapConfig.xml:
<setting name="cacheEnabled" value="true"/>
Description | Allowed values | Default value | |
Configure global on/off settings for all caches under this configuration file. | true false | true |
The above is the detailed content of What is query cache? Introduction to the usage of MyBatis query cache. For more information, please follow other related articles on the PHP Chinese website!