In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What is Mybatis caching?
The use of cache can reduce the number of interactions between Java Application and the database, thus improving the efficiency of the program. For example, querying the user object of id=1 will automatically save the object to the cache after the first query. The next time you query the object, you can get it directly from the cache without having to send a SQL query to the database.
Mybatis cache classification
First-level cache: SqlSession level, which is enabled by default and cannot be disabled.
The first-level cache of mybatis is SqlSession-level cache. When you operate the database, you need to construct a SqlSession object. There is a HashMap in the object to store the cached data, and the cached data regions (HashMap) between different SqlSession do not affect each other.
The scope of the first-level cache is SqlSession. When the same sql statement is executed twice in the same SqlSession, the data queried in the database will be written to the cache (memory) after the first execution, and the data will be obtained from the cache in the second query, instead of going to the underlying layer to query the database, thus improving the query efficiency. It should be noted that if SqlSession performs DML operations (insert, update, delete) and commit () operation, mybatis will clear the first-level cache in SqlSession to ensure that the latest information is stored in the cached data and avoid dirty reading.
When a SqlSession ends, the first-level cache in the SqlSession no longer exists. The first-level cache is enabled by Mybatis by default, and no configuration is required.
Second-level cache: Mapper level, which is off by default and can be enabled.
The second-level cache is a Mapper-level cache. When using the second-level cache, multiple SqlSession use the sql statement of the same Mapper to operate the database, and the resulting data will exist in the second-level cache area, which also uses HashMap for data storage. Compared with the first-level cache SqlSession, the scope of the second-level cache is larger. Multiple SqlSession can share the second-level cache, and the secondary cache is cross-SqlSession.
The secondary cache is shared by multiple SqlSession, its scope is the same namespace of mapper, different SqlSession executes the sql statement under the same namespace twice, and the parameters passed to sql are the same, that is, the same sql statement is finally executed, the data queried in the database will be written to the cache (memory) after the first execution, and the data will be obtained from the cache in the second query instead of going to the underlying database query. So as to improve the query efficiency.
Mybatis disables secondary cache by default, and you can enable it in the global parameters of setting.
Let's learn how to use MyBatis caching through code.
First, let's demonstrate first-level caching, taking querying Student objects as an example.
/ * *
* @ ClassName Student
* @ Description
* @ Author lzq
* @ Date 13:53 on 2019-7-26
* @ Version 1.0
* * /
Public class Student {
Private int SID
Private String Sname
Private String Ssex
Private int Age
Public int getSID () {
Return SID
}
Public void setSID (int SID) {
This.SID = SID
}
Public String getSname () {
Return Sname
}
Public void setSname (String sname) {
Sname = sname
}
Public String getSsex () {
Return Ssex
}
Public void setSsex (String ssex) {
Ssex = ssex
}
Public int getAge () {
Return Age
}
Public void setAge (int age) {
Age = age
}
@ Override
Public String toString () {
Return "[id" + SID+ "name" + Sname+ "gender" + Ssex+ "Age" + Age+ "]"
}
}
StudentMapper interface:
Import org.apache.ibatis.annotations.*
Public interface StudentMapper {
Public Student getStudentById (int id)
}
Mybatis-config.xml:
PUBLIC "- / / mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
StudentMapper.xml:
PUBLIC "- / / mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
Select * from student where SID = # {id}
Test the code:
**
* @ ClassName Test
* @ Description
* @ Author lzq
* @ Date 13:53 on 2019-7-26
* @ Version 1.0
* * /
Public class Test {
Public static void main (String [] args) throws IOException {
String resource = "mybatis-config.xml"
/ / read the configuration file
InputStream asStream = Resources.getResourceAsStream (resource)
/ / create a sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (asStream)
/ / create a sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession ()
/ / generate StudentMapper objects through dynamic proxy
StudentMapper mapper = sqlSession.getMapper (StudentMapper.class)
/ / query tuples with id 1
Student student = mapper.getStudentById (1)
System.out.println (student)
Student student1 = mapper.getStudentById (1)
System.out.println (student1)
}
}
You can see the result, execute the SQL statement once, and query out two objects, the first object is queried through SQL and saved to the cache, and the second object is fetched directly from the cache.
We said that the first-level cache is SqlSession-level, so once SqlSession is turned off, the cache no longer exists, modify the code, and test again.
Test the code:
**
* @ ClassName Test
* @ Description
* @ Author lzq
* @ Date 13:53 on 2019-7-26
* @ Version 1.0
* * /
Public class Test {
Public static void main (String [] args) throws IOException {
String resource = "mybatis-config.xml"
/ / read the configuration file
InputStream asStream = Resources.getResourceAsStream (resource)
/ / create a sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder () .build (asStream)
/ / create a sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession ()
/ / generate StudentMapper objects through dynamic proxy
StudentMapper mapper = sqlSession.getMapper (StudentMapper.class)
/ / query tuples with id 2
Student student = mapper.getStudentById (1)
System.out.println (student)
SqlSession.close (); / / close the original sqlSession
SqlSession = sqlSessionFactory.openSession (); / / create a new
Mapper = sqlSession.getMapper (StudentMapper.class)
Student student1 = mapper.getStudentById (1)
System.out.println (student1)
}
}
As you can see, the SQL was executed twice.
When SqlSession is turned off and the first-level cache expires, you can enable second-level cache to achieve the requirement of improving efficiency.
MyBatis can use its own secondary cache or a third-party ehcache secondary cache.
Which is a good http://www.wxbhffk.com/ for Wuxi passenger flow with its own second-level cache?
Mybatis-config.xml configuration enables secondary caching
PUBLIC "- / / mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
Enable secondary caching in the StudentMapper.xml configuration:
PUBLIC "- / / mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
Select * from student where SID = # {id}
The Student class implements the Serializable interface:
Import java.io.Serializable
/ * *
* @ ClassName Student
* @ Description
* @ Author lzq
* @ Date 13:53 on 2019-7-26
* @ Version 1.0
* * /
Public class Student implements Serializable {
Private int SID
Private String Sname
Private String Ssex
Private int Age
Public int getSID () {
Return SID
}
Public void setSID (int SID) {
This.SID = SID
}
Public String getSname () {
Return Sname
}
Public void setSname (String sname) {
Sname = sname
}
Public String getSsex () {
Return Ssex
}
Public void setSsex (String ssex) {
Ssex = ssex
}
Public int getAge () {
Return Age
}
Public void setAge (int age) {
Age = age
}
@ Override
Public String toString () {
Return "[id" + SID+ "name" + Sname+ "gender" + Ssex+ "Age" + Age+ "]"
}
}
The test code is still the one used last time:
As you can see, the SQL is executed once and two objects are queried. The hit rate of cache is 0.5.
Zhengzhou Infertility Hospital: http://www.zzchyy110.com/
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.