天天看点

Mybatis 缓存失效的几种情况

下面比较下载同一个sqlSession和不在同一sqlSession下面的两种情况:

同一sqlSession:

如下sql执行了一次,第二次queryClazzById没有执行sql,直接从缓存里面获取。

DEBUG [main] - ==>  Preparing: select c.*,s.id stu_id,s.name stu_name from t_clazz c left join t_student s on c.id = s.clazz_id where c.id = ? 

DEBUG [main] - ==> Parameters: 1(Integer)

DEBUG [main] - <==      Total: 3

clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]

clazz2 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]

不在同一sqlSession:

  看下结果:

clazz1 = Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]

DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]

DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]

DEBUG [main] - Returned connection 1754638213 to pool.

DEBUG [main] - Opening JDBC Connection

DEBUG [main] - Checked out connection 1754638213 from pool.

DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]

DEBUG [main] - Returned connection 1754638213 to pool. 

分别调用两次sql,没用使用缓存。

看结果:

Clazz [id=1, name=javaEE20170228, stus=[Student [id=1, name=stu0228_张三, clazz=null], Student [id=2, name=stu0228_李四, clazz=null], Student [id=3, name=stu0228_王五, clazz=null]]]

DEBUG [main] - ==> Parameters: 2(Integer)

DEBUG [main] - <==      Total: 2

Clazz [id=2, name=javaEE20170325, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]

分别调用两次sql,没有使用到缓存。

DEBUG [main] - ==>  Preparing: update t_clazz set name = ? where id = ? 

DEBUG [main] - ==> Parameters: 電影放映班(String), 2(Integer)

DEBUG [main] - <==    Updates: 1

DEBUG [main] - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6895a785]

Clazz [id=2, name=電影放映班, stus=[Student [id=4, name=stu0325_马云, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]

查询执行两次,更新执行一次,也没使用到缓存

4、手动清空缓存数据

执行了两次sql,没有使用到缓存

下面比较下载同一个sqlSession和不在同一sqlSession下面的两种情况

<wiz_code_mirror>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

分别调用两次sql,没用使用缓存

分别调用两次sql,没有使用到缓存

看下结果: