MyBatis(七):MyBatis缓存详解(一级缓存/二级缓存) (2)

查看控制台输出,发现两次缓存命中都是0.0,Cache Hit Ratio [com.rangers.dao.IUserDao]: 0.0

14:49:39,073 DEBUG IUserDao:62 - Cache Hit Ratio [com.rangers.dao.IUserDao]: 0.0 14:49:39,075 DEBUG JdbcTransaction:137 - Opening JDBC Connection 14:49:39,385 DEBUG PooledDataSource:406 - Created connection 543846639. 14:49:39,385 DEBUG JdbcTransaction:101 - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 14:49:39,389 DEBUG findOne:159 - ==> Preparing: select * from user where id=? 14:49:39,408 DEBUG findOne:159 - ==> Parameters: 1(Integer) 14:49:39,433 DEBUG findOne:159 - <== Total: 1 com.rangers.entity.User{id=1,, address='杭州'} 14:49:39,434 DEBUG IUserDao:62 - Cache Hit Ratio [com.rangers.dao.IUserDao]: 0.0 14:49:39,434 DEBUG JdbcTransaction:137 - Opening JDBC Connection 14:49:39,489 DEBUG PooledDataSource:406 - Created connection 2079179914. 14:49:39,489 DEBUG JdbcTransaction:101 - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7bedc48a] 14:49:39,491 DEBUG findOne:159 - ==> Preparing: select * from user where id=? 14:49:39,492 DEBUG findOne:159 - ==> Parameters: 1(Integer) 14:49:39,495 DEBUG findOne:159 - <== Total: 1 com.rangers.entity.User{id=1,, address='杭州'} 14:49:39,497 DEBUG JdbcTransaction:123 - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 14:49:39,499 DEBUG JdbcTransaction:91 - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 14:49:39,500 DEBUG PooledDataSource:363 - Returned connection 543846639 to pool. 14:49:39,500 DEBUG JdbcTransaction:123 - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7bedc48a] 14:49:39,502 DEBUG JdbcTransaction:91 - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7bedc48a] 14:49:39,502 DEBUG PooledDataSource:363 - Returned connection 2079179914 to pool.

更新提交事务,与一级缓存一样,增删改操作提交事务会清空缓存

InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); User param = new User(); param.setId(1); SqlSession sqlSession1 = sqlSessionFactory.openSession(); IUserDao userDao1 = sqlSession1.getMapper(IUserDao.class); User user1 = userDao1.findOne(param); System.out.println("sqlSession1查询结果:"+user1); sqlSession1.close(); SqlSession sqlSession2 = sqlSessionFactory.openSession(); IUserDao userDao2 = sqlSession2.getMapper(IUserDao.class); param.setName("游骑兵"); param.setAddress("西安"); Boolean flag = userDao2.updateUser(param) > 0; System.out.println("执行更新操作结果:"+flag); sqlSession2.commit(); sqlSession2.close(); SqlSession sqlSession3 = sqlSessionFactory.openSession(); IUserDao userDao3 = sqlSession3.getMapper(IUserDao.class); User user3 = userDao3.findOne(param); sqlSession3.close(); System.out.println("sqlSession3查询结果:"+user3);

查看控制台输出,经过sqlSession2提交事务后,sqlSession3的查询缓存命中率为0.0

15:00:33,901 DEBUG IUserDao:62 - Cache Hit Ratio [com.rangers.dao.IUserDao]: 0.0 15:00:33,904 DEBUG JdbcTransaction:137 - Opening JDBC Connection 15:00:34,263 DEBUG PooledDataSource:406 - Created connection 543846639. 15:00:34,263 DEBUG JdbcTransaction:101 - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,267 DEBUG findOne:159 - ==> Preparing: select * from user where id=? 15:00:34,290 DEBUG findOne:159 - ==> Parameters: 1(Integer) 15:00:34,314 DEBUG findOne:159 - <== Total: 1 sqlSession1查询结果:com.rangers.entity.User{id=1,, address='杭州'} 15:00:34,317 DEBUG JdbcTransaction:123 - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,321 DEBUG JdbcTransaction:91 - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,321 DEBUG PooledDataSource:363 - Returned connection 543846639 to pool. 15:00:34,321 DEBUG JdbcTransaction:137 - Opening JDBC Connection 15:00:34,322 DEBUG PooledDataSource:398 - Checked out connection 543846639 from pool. 15:00:34,322 DEBUG JdbcTransaction:101 - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,325 DEBUG updateUser:159 - ==> Preparing: update user set name=?,address=? where id=? 15:00:34,325 DEBUG updateUser:159 - ==> Parameters: 游骑兵(String), 西安(String), 1(Integer) 15:00:34,334 DEBUG updateUser:159 - <== Updates: 1 执行更新操作结果:true 15:00:34,335 DEBUG JdbcTransaction:70 - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,445 DEBUG JdbcTransaction:123 - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,448 DEBUG JdbcTransaction:91 - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,450 DEBUG PooledDataSource:363 - Returned connection 543846639 to pool. 15:00:34,450 DEBUG IUserDao:62 - Cache Hit Ratio [com.rangers.dao.IUserDao]: 0.0 15:00:34,450 DEBUG JdbcTransaction:137 - Opening JDBC Connection 15:00:34,451 DEBUG PooledDataSource:398 - Checked out connection 543846639 from pool. 15:00:34,451 DEBUG JdbcTransaction:101 - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,453 DEBUG findOne:159 - ==> Preparing: select * from user where id=? 15:00:34,454 DEBUG findOne:159 - ==> Parameters: 1(Integer) 15:00:34,458 DEBUG findOne:159 - <== Total: 1 15:00:34,458 DEBUG JdbcTransaction:123 - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,461 DEBUG JdbcTransaction:91 - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@206a70ef] 15:00:34,462 DEBUG PooledDataSource:363 - Returned connection 543846639 to pool. sqlSession3查询结果:com.rangers.entity.User{id=1,, address='西安'}

多表操作时,对MyBatis二级缓存的影响

问题:未同一namespace下进行增删改提交操作时,其他namespace的缓存是无法感知的

例如:两个Mapper文件,AMapper.xml和BMapper.xml,B修改了user表中的内容,A是感知不到的,那么再从A里查询如果用到了缓存,就是旧的数据。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygwxz.html