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>

 
 
 
 
 
1
@Test
2
    public final void testQueryClazzById() {
3
        SqlSession session = sqlSessionFactory.openSession();
4
        try {
5
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
6
            Clazz clazz1 = clazzMapper.queryClazzById(1);
7
            System.out.println("clazz1 = "+clazz1);
8
            
9
            Clazz clazz2 = clazzMapper.queryClazzById(1);
10
            System.out.println("clazz2 = "+clazz2);
11
        } finally {
12
            session.close();
13
        }
 
 
如下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>

 
 
 
 
 
1
@Test
2
    public final void testQueryClazzById() {
3
        SqlSession session = sqlSessionFactory.openSession();
4
        try {
5
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
6
            Clazz clazz1 = clazzMapper.queryClazzById(1);
7
            System.out.println("clazz1 = "+clazz1);
8
        } finally {
9
            session.close();
10
        }
11
        //緩存失效的四種情況
12
        //不在同一個對象中
13
        SqlSession session2 = sqlSessionFactory.openSession();
14
        try {
15
            ClazzMapper clazzMapper2 = session2.getMapper(ClazzMapper.class);
16
            Clazz clazz2 = clazzMapper2.queryClazzById(1);
17
            System.out.println("clazz2 = "+clazz2);
18
        } finally {
19
            session2.close();
20
        }
21
    }
 
 
  看下结果:
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>

 
 
 
 
 
1
@Test
2
    public void testfirstCacheFail2() {
3
        //一级缓存必须存在于同一个SqlSession中
4
        SqlSession session = sqlSessionFactory.openSession();
5
        ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class);
6
        Clazz user1 = clazzMapper2.queryClazzById(1);
7
        System.out.println(user1);
8
        
9
        Clazz user2 = clazzMapper2.queryClazzById(2);
10
        System.out.println( user2 );
11
        session.close();
12
    }
 
 
看结果:
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>

 
 
 
 
 
1
 @Test
2
    public void testfirstCacheFail3() {
3
        //一级缓存必须存在于同一个SqlSession中
4
        SqlSession session = sqlSessionFactory.openSession();
5
        ClazzMapper clazzMapper2 = session.getMapper(ClazzMapper.class);
6
        Clazz user1 = clazzMapper2.queryClazzById(2);
7
        System.out.println(user1);
8
        
9
        Clazz clazz = new Clazz();
10
        clazz.setId(2);
11
        clazz.setName("電影放映班");
12
        clazzMapper2.updateClazz(clazz);
13
        session.commit();
14
        
15
        System.out.println(clazzMapper2.queryClazzById(2));
16
        session.close();
17
    }
 
 
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>

 
 
 
 
 
1
@Test
2
    public  void testfirstCacheFail4() {
3
        SqlSession session = sqlSessionFactory.openSession();
4
        try {
5
            ClazzMapper clazzMapper = session.getMapper(ClazzMapper.class);
6
            Clazz clazz1 = clazzMapper.queryClazzById(1);
7
            System.out.println("clazz1 = "+clazz1);
8
            //清空緩存
9
            session.clearCache();
10
            Clazz clazz2 = clazzMapper.queryClazzById(1);
11
            System.out.println("clazz2 = "+clazz2);
12
        } finally {
13
            session.close();
14
        }
15
    }
 
 
 
看下结果:
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,没有使用到缓存

Mybatis 缓存失效的几种情况的更多相关文章

  1. MySQL索引失效的几种情况

    1.索引不存储null值 更准确的说,单列索引不存储null值,复合索引不存储全为null的值.索引不能存储Null,所以对这列采用is null条件时,因为索引上根本 没Null值,不能利用到索引, ...

  2. mysql索引总结(4)-MySQL索引失效的几种情况

    mysql索引总结(1)-mysql 索引类型以及创建 mysql索引总结(2)-MySQL聚簇索引和非聚簇索引 mysql索引总结(3)-MySQL聚簇索引和非聚簇索引 mysql索引总结(4)-M ...

  3. C++迭代器失效的几种情况总结

    一.序列式容器(数组式容器) 对于序列式容器(如vector,deque),序列式容器就是数组式容器,删除当前的iterator会使后面所有元素的iterator都失效.这是因为vetor,deque ...

  4. Spring事务失效的2种情况

    使用默认的事务处理方式 因为在java的设计中,它认为不继承RuntimeException的异常是”checkException”或普通异常,如IOException,这些异常在java语法中是要求 ...

  5. Mysql索引会失效的几种情况分析(转)

    出处:http://www.jb51.net/article/50649.htm 索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这 ...

  6. oracle数据库中索引失效的几种情况

    原文1:https://blog.csdn.net/u012255097/article/details/102792683 原文2:https://www.cnblogs.com/lanseyita ...

  7. vector迭代器失效的几种情况

    在泛型编程还是STL的实际运用中,迭代器(iterator)无疑扮演者重要的角色.迭代器是一种类似于指针的对象(如可以内容提领,成员访问等),但他又不仅仅是一种普通的指针.关于迭代器失效,我们可以看下 ...

  8. sql 索引常见失效的几种情况

    1. 对于联合索引,没有遵循左前缀原则 2. 索引的字段区分度不大,可能引起索引近乎全表扫描 3. 对于join操作,索引字段的编码不一致,导致使用索引失效 4.对于hash索引,范围查询失效,has ...

  9. Mysql索引会失效的几种情况

    1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因): 2.对于多列索引,不是使用的第一部分,则不会使用索引: 3.like查询是以%开头: 4.如果列类型是字符串, ...

随机推荐

  1. spark单击 搭建

    http://files.cnblogs.com/files/yxnyd/spark.zip

  2. [转帖]ulimit、limits.conf、sysctl和proc文件系统

    ulimit.limits.conf.sysctl和proc文件系统 来源:https://blog.csdn.net/weixin_33918114/article/details/86882372 ...

  3. 123. 单词搜索(DFS)

    描述 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. 样例 给出 ...

  4. python raise

    当程序出现错误,python会自动引发异常,也可以通过raise显示地引发异常.一旦执行了raise语句,raise后面的语句将不能执行.   演示raise用法 try: s = None if s ...

  5. placeholder解决兼容各种IE浏览器的方法

    <input id="search" type="text" class="box" class="inputText&qu ...

  6. Mysql Router 的集群

    1. c:\mysql-router, c:\mysql-5.7.23, 这两个目录的bin都要加入path 2. c:\mysql-shell,在bin下,有一个 mysqlsh.exe, 双击,打 ...

  7. JS判断语句 注意多句时加大括号 回调函数LODOP兼顾写法

    由于C-LODOP获取一些动作函数的返回值,需要用回调函数,由于常见的使用方法是混合部署,也就是同一个页面,根据浏览器的不同,可能会走LODOP ,也可能会走C-LODOP,因此写法要坚固两种,而有些 ...

  8. vim指令

    编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式: a: 在当前光标所在字符的后面,转为输入模式: o: 在当前光标所在行的下方,新建一行,并转为输入模式: I:在当前光标所在行的 ...

  9. Nginx 优先选择连接最少的上游服务器

    详见陶辉87课 upstream test { server ; server ; least_conn ; zone backends 64k; }

  10. Calendar代替Date常用方法小记

    业务逻辑中遇到日期的相关操作,整理一下小做总结 日期操作离不开 java.util.Calendar 和 java.util.Date ,实体类中日期字段设计也可能用到java.sql.Date.但目 ...