mybatis cache and secondary cache using

In mybatis, the concept of cache and secondary cache:

Cache : cache cache Mybatis means SQLSession, a cache scope is SQLSession, Mabits cache is enabled by default. At the same SqlSession, do the same SQL query; for the first time will go to query the database, and write in the cache, the second will be taken directly from the cache. When the operation to perform CRUD SQL query when two occurred in the middle, then SQLSession cache will be cleared.

Reason for this design is to avoid dirty reads: Suppose A query of a certain commodity inventory is 10, and 10 stocks data stored in the cache, then the customer bought 10, delete the data, but in when queries this product, not from the database query, but the query from the cache, an error occurs.

Secondary cache: secondary cache is mapper level, Mybatis is not turned on by default secondary cache. The first call under the mapper SQL query information to the user's query to the information will be stored on behalf of the secondary cache area corresponding to the mapper.

 

Look at the concept of what most annoying is not it? With a look at the code chant:

 1 package top.bigking;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 import top.bigking.dao.DeptMapper;
 8 import top.bigking.dao.UserMapper;
 9 import top.bigking.pojo.Dept;
10 import top.bigking.pojo.User;
11 
12 import java.io.InputStream;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 
17 public class mybatisStudy_start {
18     public static void main(String[] args) throws Exception{
19         InputStream in = Resources.getResourceAsStream("mybatisStudy-config.xml");
20         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
21         SqlSession sqlSession = sqlSessionFactory.openSession(true);//true 表示 自动提交
22         DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
23         List<Dept> deptList = deptMapper.query();
24         for(Dept dept : deptList){
25             System.out.println(dept);
26         }
27         System.out.println("-----------------------------------");
28         //insert(deptMapper);
29         //update(deptMapper);
30         findById(deptMapper);
31         
32         findById(deptMapper);
33         //delete(deptMapper);
34         //findByIdDname(deptMapper);
35 
36 //        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
37 //        List<User> userList = userMapper.query();
38 //        for(User user : userList)
39 //            System.out.println(user);
40         sqlSession.close();
41 
42     }
43     private static void insert(DeptMapper deptMapper){
44         Dept dept = new Dept();
45         dept.setDeptNo(11);
46         dept.setDname("ABKing");
47         dept.setLoc("111");
48         int count = deptMapper.insert(dept);
49         System.out.println(count);
50     }
51     public static void update(DeptMapper deptMapper){
52         Dept dept = new Dept();
53         dept.setDeptNo(11);
54         dept.setDname("ABKing");
55         dept.setLoc("上海");
56         int count = deptMapper.update(dept);
57         System.out.println(count);
58     }
59     public static void findById(DeptMapper deptMapper){
60         Dept dept = deptMapper.findById(11);
61         System.out.println(dept);
62     }
63     public static void delete(DeptMapper deptMapper){
64         int count = deptMapper.delete(11);
65         System.out.println(count);
66     }
67     public static void findByIdDname(DeptMapper deptMapper){
68         Map map = new HashMap();
69         map.put("deptNo", "11");
70         map.put("dname", "ABKing");
71         Dept dept = deptMapper.findByIdDname(map);
72         System.out.println(dept);
73     }
74 }

See 30 lines and 32 lines, called twice to see findById (deptMapper), run, view the log console (requires log4j package)

DEBUG [main] - ==>  Preparing: select deptNo, dname, loc from Dept where deptNo=? 

Only print out a SQL statement

And if we add sqlSession.clearCache in line 31 ();

 1 package top.bigking;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 import top.bigking.dao.DeptMapper;
 8 import top.bigking.dao.UserMapper;
 9 import top.bigking.pojo.Dept;
10 import top.bigking.pojo.User;
11 
12 import java.io.InputStream;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 
17 public class mybatisStudy_start {
18     public static void main(String[] args) throws Exception{
19         InputStream in = Resources.getResourceAsStream("mybatisStudy-config.xml");
20         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
21         SqlSession sqlSession = sqlSessionFactory.openSession(true);//true 表示 自动提交
22         DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
23         List<Dept> deptList = deptMapper.query();
24         for(Dept dept : deptList){
25             System.out.println(dept);
26         }
27         System.out.println("-----------------------------------");
28         //insert(deptMapper);
29         //update(deptMapper);
30         findById(deptMapper);
31         sqlSession.clearCache();
32         findById(deptMapper);
33         //delete(deptMapper);
34         //findByIdDname(deptMapper);
35 
36 //        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
37 //        List<User> userList = userMapper.query();
38 //        for(User user : userList)
39 //            System.out.println(user);
40         sqlSession.close();
41 
42     }
43     private static void insert(DeptMapper deptMapper){
44         Dept dept = new Dept();
45         dept.setDeptNo(11);
46         dept.setDname("ABKing");
47         dept.setLoc("111");
48         int count = deptMapper.insert(dept);
49         System.out.println(count);
50     }
51     public static void update(DeptMapper deptMapper){
52         Dept dept = new Dept();
53         dept.setDeptNo(11);
54         dept.setDname("ABKing");
55         dept.setLoc("上海");
56         int count = deptMapper.update(dept);
57         System.out.println(count);
58     }
59     public static void findById(DeptMapper deptMapper){
60         Dept dept = deptMapper.findById(11);
61         System.out.println(dept);
62     }
63     public static void delete(DeptMapper deptMapper){
64         int count = deptMapper.delete(11);
65         System.out.println(count);
66     }
67     public static void findByIdDname(DeptMapper deptMapper){
68         Map map = new HashMap();
69         map.put("deptNo", "11");
70         map.put("dname", "ABKing");
71         Dept dept = deptMapper.findByIdDname(map);
72         System.out.println(dept);
73     }
74 }

At this point of view the console log

DEBUG [main] - ==>  Preparing: select deptNo, dname, loc from Dept where deptNo=? 
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <==      Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - ==>  Preparing: select deptNo, dname, loc from Dept where deptNo=? 

We can see two print out the SQL statement, which is sufficient to demonstrate that the use of sqlSession.clearCache (); After that, the cache is cleared

 

 Next to prove secondary cache:

It should be clear, the secondary cache is mapper level cache, using the same Mapper.class SqlSession Mapper classes and class share the same secondary cache

code show as below:

 1 package top.bigking;
 2 
 3 import org.apache.ibatis.io.Resources;
 4 import org.apache.ibatis.session.SqlSession;
 5 import org.apache.ibatis.session.SqlSessionFactory;
 6 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 7 import top.bigking.dao.DeptMapper;
 8 import top.bigking.dao.UserMapper;
 9 import top.bigking.pojo.Dept;
10 import top.bigking.pojo.User;
11 
12 import java.io.InputStream;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 
17 public class mybatisStudy_start {
18     public static void main(String[] args) throws Exception{
19         InputStream in = Resources.getResourceAsStream("mybatisStudy-config.xml");
20         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
21         SqlSession sqlSession = sqlSessionFactory.openSession(true);//true 表示 自动提交
22         SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
23         DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
24         DeptMapper deptMapper1 = sqlSession1.getMapper(DeptMapper.class);
25         List<Dept> deptList = deptMapper.query();
26         for(Dept dept : deptList){
27             System.out.println(dept);
28         }
29         System.out.println("-----------------------------------");
30         //insert(deptMapper);
31         //update(deptMapper);
32         findById(deptMapper);
33         sqlSession.close();
34         //sqlSession.clearCache();
35         findById(deptMapper1);
36         sqlSession1.close();
37         //delete(deptMapper);
38         //findByIdDname(deptMapper);
39 
40 //        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
41 //        List<User> userList = userMapper.query();
42 //        for(User user : userList)
43 //            System.out.println(user);
44         //sqlSession.close();
45 
46     }
47     private static void insert(DeptMapper deptMapper){
48         Dept dept = new Dept();
49         dept.setDeptNo(11);
50         dept.setDname("ABKing");
51         dept.setLoc("111");
52         int count = deptMapper.insert(dept);
53         System.out.println(count);
54     }
55     public static void update(DeptMapper deptMapper){
56         Dept dept = new Dept();
57         dept.setDeptNo(11);
58         dept.setDname("ABKing");
59         dept.setLoc("上海");
60         int count = deptMapper.update(dept);
61         System.out.println(count);
62     }
63     public static void findById(DeptMapper deptMapper){
64         Dept dept = deptMapper.findById(11);
65         System.out.println(dept);
66     }
67     public static void delete(DeptMapper deptMapper){
68         int count = deptMapper.delete(11);
69         System.out.println(count);
70     }
71     public static void findByIdDname(DeptMapper deptMapper){
72         Map map = new HashMap();
73         map.put("deptNo", "11");
74         map.put("dname", "ABKing");
75         Dept dept = deptMapper.findByIdDname(map);
76         System.out.println(dept);
77     }
78 }

In the DeptMapper.xml

    <select id="findById" resultType="Dept" useCache="true">
        select deptNo, dname, loc from Dept where deptNo=#{deptNo}
    </select>

May be used by the secondary cache useCache

In mybatisStudy-config.xml, under properties tab, add

   <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>

This is a global setting, it will indicate on the secondary cache

 

View log console output:

DEBUG [main] - ==>  Preparing: select deptNo, dname, loc from Dept where deptNo=? 
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <==      Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a9273a8]

Can be seen, the use of the secondary cache, we log in, there was only a statement SQLQL

And if we DeptMapper.xml the code to read:

  <select id="findById" resultType="Dept" useCache="false">
        select deptNo, dname, loc from Dept where deptNo=#{deptNo}
    </select>

It represents the secondary cache is not used

Run, view the console:

DEBUG [main] - ==>  Preparing: select deptNo, dname, loc from Dept where deptNo=? 
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <==      Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a9273a8]
DEBUG [main] - Returned connection 2056418216 to pool.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Checked out connection 2056418216 from pool.
DEBUG [main] - ==>  Preparing: select deptNo, dname, loc from Dept where deptNo=? 
DEBUG [main] - ==> Parameters: 11(Integer)
DEBUG [main] - <==      Total: 1
Dept{deptNo=11, dname='ABKing', loc='111'}
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7a9273a8]

We can see two SQL statements

 

But our code, and the function is not used to empty the cache of sqlSession.clearCache ();

Whereby demonstrated here is the secondary cache switch 

 

reference:

https://m.w3cschool.cn/kzsow/kzsow-qmod2gri.html

https://www.cnblogs.com/happyflyingpig/p/7739749.html

https://www.cnblogs.com/yuluoxingkong/p/8205858.html

https://www.cnblogs.com/charlypage/p/9747145.html

 

-------------------------------------------------- --------------------------
Hello everyone, I'm ABKing

Jinlin How can it abounds, a case of situation will of the dragon!
I welcome the exchange of technical problems

Guess you like

Origin www.cnblogs.com/ABKing/p/11996168.html