下面比較下載下傳同一個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,沒有使用到緩存
看下結果: