In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "what is Mybaits caching mechanism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. First-level cache
Local cach
The data queried during the same session as the database will be placed in the local cache
If you need to get the same data later, take it directly from the cache
First-level caching is automatically turned on. Here are four ways to invalidate the first-level cache
1.sqlSession is different
The same 2.sqlSession, but different query conditions
3.sqlSession is the same, adding and deleting operations are performed during the query.
4. Manually cleared the first-level cache
1.1 first experience of first-level caching
@ Test
Public void test1 () throws Exception {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory ()
SqlSession openSession = sqlSessionFactory.openSession ()
EmployMapper mapper = openSession.getMapper (EmployMapper.class)
Employee id = mapper.getId (2)
System.out.println (id)
Employee id1 = mapper.getId (2)
System.out.println (id1)
OpenSession.close ()
}
Because there is a level of cache, there is no need to call sqlSession.
1.2 level 1 cache invalidation
SqlSession is different
@ Test
Public void test1 () throws Exception {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory ()
SqlSession openSession = sqlSessionFactory.openSession ()
EmployMapper mapper = openSession.getMapper (EmployMapper.class)
Employee id = mapper.getId (2)
System.out.println (id)
SqlSession openSession1 = sqlSessionFactory.openSession ()
EmployMapper mapper1 = openSession1.getMapper (EmployMapper.class)
Employee id1 = mapper1.getId (2)
System.out.println (id1)
OpenSession.close ()
}
Due to the difference of SqlSession
So that there is no cache
The same 2.sqlSession, but different query conditions
3.sqlSession is the same, adding and deleting operations are performed during the query.
4. Manually cleared the first-level cache
The openSession.clearCache () method is executed
two。 Second-level cache
Global caching
Is based on namespace-level caching, one namespace to one secondary cache
1. Query a piece of data and put it in the first-level cache
two。 If the session is closed, from level 1 to level 2, the new session information will refer to the level 2 cache.
3. Different namespace will be placed in different map and in different secondary cache.
2.1 enable second-tier cache
Turn on the secondary cache
Test.java
@ Test
Public void test2 () throws Exception {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory ()
SqlSession openSession = sqlSessionFactory.openSession ()
SqlSession openSession1 = sqlSessionFactory.openSession ()
EmployMapper mapper = openSession.getMapper (EmployMapper.class)
EmployMapper mapper1 = openSession1.getMapper (EmployMapper.class)
Employee id = mapper.getId (3)
System.out.println (id)
OpenSession.close ()
Employee id1 = mapper1.getId (3)
System.out.println (id1)
OpenSession1.close ()
}
Note that you need to close and get the second one, otherwise the secondary cache will not be turned on, because only when the session is closed will the primary cache be converted to the secondary cache.
2.2 attributes of secondary cache
CacheEnabled= "true"
Turn on the secondary cache
CacheEnabled= "false"
The secondary cache is turned off, but the primary cache is not closed
UseCache= "false"
The secondary cache cannot be used, but the primary cache can also be used
If flushCache= "true", both primary and secondary caches will be cleared.
Default is false
3. Cache schematic diagram
Cache query order:
Second-level cache
First-level cache
Database
That's all for "what is the Mybaits caching mechanism?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.