20-Mybatis一级缓存

上一篇:19-Mybatis分页小工具https://blog.csdn.net/fsjwin/article/details/109684099

mybatis的一级缓存就是SqlSession级别的缓存
同一个SqlSession连续查询两次仅发出了一个sql,使用了一级缓存

mybaits缓存失效的四种情况

  1. 不同的SqlSession
  2. 相同的SqlSession,查询条件不同
  3. 相同的SqlSession,查询条件相同,但是中间做了 增删改操作
  4. 相同的SqlSession,但是我中间清理了缓存

本质上就是一个map。

1.正常使用一级缓存

在这里插入图片描述

2.一级缓存失效

2.1不同的SqlSession

  1. 测试
 @Test
    public void test24() {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
        List<StudentVo> studentList = studentDao.selectStudentResultType(1001);
        System.out.println("第二次查询");
        List<StudentVo> studentList2 = studentDao.selectStudentResultType(1001);
        studentList.forEach(stu -> System.out.println(stu));
    }
  1. 测试结果:
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1904324159.
==>  Preparing: select id,name,email,age from student where id=?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
第二次查询
StudentVo{name='张三', age=20}

2.2相同的SqlSession,查询条件不同

这个很容易理解吧,条件不同肯定要发出sql从新查询:
在这里插入图片描述

  1. 测试
 @Test
    public void test24() {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
        List<StudentVo> studentList = studentDao.selectStudentResultType(1001);
        System.out.println("第二次查询");
        List<StudentVo> studentList2 = studentDao.selectStudentResultType(1002);
        studentList.forEach(stu -> System.out.println(stu));
    }
  1. 测试结果
在这里插入代码片`PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1904324159.
==>  Preparing: select id,name,email,age from student where id=?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
第二次查询
==>  Preparing: select id,name,email,age from student where id=?
==> Parameters: 1002(Integer)
<==    Columns: id, name, email, age
<==        Row: 1002, 李四, lisi@qq.com, 28
<==      Total: 1
StudentVo{name='张三', age=20}`

2.3相同的SqlSession,查询条件相同,但是中间做了 增删改操作

在这里插入图片描述

  1. 测试
@Test
    public void test24() {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
        List<StudentVo> studentList = studentDao.selectStudentResultType(1001);

        System.out.println("插入数据");
        studentDao.insertStudent(new Student(1007, "张三", "qq.com", 10));

        System.out.println("第二次查询");
        List<StudentVo> studentList2 = studentDao.selectStudentResultType(1001);
        studentList.forEach(stu -> System.out.println(stu));
    }
  1. 测试结果
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1904324159.
==>  Preparing: select id,name,email,age from student where id=?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
插入数据
==>  Preparing: insert into student(id, name, email, age) values (?,?,?,?)
==> Parameters: 1007(Integer), 张三(String), qq.com(String), 10(Integer)
<==    Updates: 1
第二次查询
==>  Preparing: select id,name,email,age from student where id=?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
StudentVo{name='张三', age=20}

2.4相同的SqlSession,但是我中间清理了缓存

在这里插入图片描述

  1. 测试
@Test
    public void test25() {
        SqlSession sqlsession = MybatisUtil.getSqlsession();
        StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
        //可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
        List<StudentVo> studentList = studentDao.selectStudentResultType(1001);

        System.out.println("清理缓存");
        sqlsession.clearCache();

        System.out.println("第二次查询");
        List<StudentVo> studentList2 = studentDao.selectStudentResultType(1001);
        studentList.forEach(stu -> System.out.println(stu));
    }
  1. 测试结果
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1904324159.
==>  Preparing: select id,name,email,age from student where id=?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
清理缓存
第二次查询
==>  Preparing: select id,name,email,age from student where id=?
==> Parameters: 1001(Integer)
<==    Columns: id, name, email, age
<==        Row: 1001, 张三, zhangsan@qq.com, 20
<==      Total: 1
StudentVo{name='张三', age=20}

下一篇:21-Mybatis二级缓存https://blog.csdn.net/fsjwin/article/details/109685894

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值