1 不在同一個sqlSession對象中
下面比較下載同一個sqlSession和不在同一sqlSession下面的兩種情況:
同一sqlSession:
@Test public final void testQueryClazzById() { SqlSession session = sqlSessionFactory.openSession(); try { ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class); Clazz clazz1 = clazzMapper.queryClazzById(1); System.out.println("clazz1 = "+clazz1); Clazz clazz2 = clazzMapper.queryClazzById(1); System.out.println("clazz2 = "+clazz2); } finally { session.close(); }
如下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:
@Test public final void testQueryClazzById() { SqlSession session = sqlSessionFactory.openSession(); try { ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class); Clazz clazz1 = clazzMapper.queryClazzById(1); System.out.println("clazz1 = "+clazz1); } finally { session.close(); } //緩存失效的四種情況 //不在同一個對象中 SqlSession session2 = sqlSessionFactory.openSession(); try { ClazzMapper clazzMapper2 = session2.getMapper(ClazzMapper.class); Clazz clazz2 = clazzMapper2.queryClazzById(1); System.out.println("clazz2 = "+clazz2); } finally { session2.close(); } }
看下結果:
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]]]
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] - ==> 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
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]]]
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.
分別調用兩次sql,沒用使用緩存。
2 執行語句的參數不同。緩存中也不存在數據
@Test public void testfirstCacheFail2() { //一級緩存必須存在於同一個SqlSession中 SqlSession session = sqlSessionFactory.openSession(); ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class); Clazz user1 = clazzMapper2.queryClazzById(1); System.out.println(user1); Clazz user2 = clazzMapper2.queryClazzById(2); System.out.println( user2 ); session.close(); }
看結果:
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
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] - ==> 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: 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,沒有使用到緩存。
3、執行增,刪,改,語句,會清空掉緩存
@Test public void testfirstCacheFail3() { //一級緩存必須存在於同一個SqlSession中 SqlSession session = sqlSessionFactory.openSession(); ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class); Clazz user1 = clazzMapper2.queryClazzById(2); System.out.println(user1); Clazz clazz = new Clazz(); clazz.setId(2); clazz.setName("電影放映班"); clazzMapper2.updateClazz(clazz); session.commit(); System.out.println(clazzMapper2.queryClazzById(2)); session.close(); }
看結果:
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: 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]]]
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]
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: 2(Integer)
DEBUG [main] - <== Total: 2
Clazz [id=2, name=電影放映班, stus=[Student [id=4, name=stu0325_馬雲, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
查詢執行兩次,更新執行一次,也沒使用到緩存
4、手動清空緩存數據
@Test public void testfirstCacheFail4() { SqlSession session = sqlSessionFactory.openSession(); try { ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class); Clazz clazz1 = clazzMapper.queryClazzById(1); System.out.println("clazz1 = "+clazz1); //清空緩存 session.clearCache(); Clazz clazz2 = clazzMapper.queryClazzById(1); System.out.println("clazz2 = "+clazz2); } finally { session.close(); } }
看結果:
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]]]
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
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]]]
執行了兩次sql,沒有使用到緩存
1 不在同一個sqlSession對象中
下面比較下載同一個sqlSession和不在同一sqlSession下面的兩種情況
<wiz_code_mirror>
如下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]]]
<wiz_code_mirror>
看下結果:
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]]]
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] - ==> 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
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]]]
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.
分別調用兩次sql,沒用使用緩存
2 執行語句的參數不同。緩存中也不存在數據
<wiz_code_mirror>
看結果:
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
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] - ==> 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: 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,沒有使用到緩存
3、執行增,刪,改,語句,會清空掉緩存
<wiz_code_mirror>
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: 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]]]
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]
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: 2(Integer)
DEBUG [main] - <== Total: 2
Clazz [id=2, name=電影放映班, stus=[Student [id=4, name=stu0325_馬雲, clazz=null], Student [id=5, name=stu0325_任正非, clazz=null]]]
查詢執行兩次,更新執行一次,也沒使用到緩存
4
、手動清空緩存數據
<wiz_code_mirror>
看下結果:
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]]]
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
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]]]
執行了兩次sql,沒有使用到緩存