上一篇:19-Mybatis分页小工具https://blog.csdn.net/fsjwin/article/details/109684099
mybatis的一级缓存就是SqlSession级别的缓存
同一个SqlSession连续查询两次仅发出了一个sql,使用了一级缓存
mybaits缓存失效的四种情况
- 不同的SqlSession
- 相同的SqlSession,查询条件不同
- 相同的SqlSession,查询条件相同,但是中间做了 增删改操作
- 相同的SqlSession,但是我中间清理了缓存
本质上就是一个map。
1.正常使用一级缓存
2.一级缓存失效
2.1不同的SqlSession
- 测试
@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));
}
- 测试结果:
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从新查询:
- 测试
@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));
}
- 测试结果
在这里插入代码片`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,查询条件相同,但是中间做了 增删改操作
- 测试
@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));
}
- 测试结果
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,但是我中间清理了缓存
- 测试
@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));
}
- 测试结果
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