🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 概念 存在于内存中的临时数据.减少数据库交互,提高执行难效率. ## 一级缓存概念 一级缓存默认是开启的.它指的是MyBatis中SqlSession对象的缓存.当我们执行查询之后,查询的结果会同时存入SqlSession到为我们提供的一块区域中.该区域的结构是一个Map,当我们再次查询同样的数据,MyBatis会先去SqlSession查询是否有,有的话直接拿出来使用.当SqlSession消失时MyBatis的一级缓存也就消失了. ## 实例 ~~~ IUserDao mapper = session.getMapper(IUserDao.class); User user1 = mapper.findById(41); System.out.println(user1); User user2 = mapper.findById(41); System.out.println(user2); ~~~ ``` 2019-04-04 21:00:26,953 0 [ main] DEBUG ache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter. 2019-04-04 21:00:27,168 215 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection 2019-04-04 21:00:27,420 467 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1b26f7b2] 2019-04-04 21:00:27,429 476 [ main] DEBUG com.like.dao.IUserDao.findById - ==> Preparing: select * from user where id = ? //只查询了一次 2019-04-04 21:00:27,473 520 [ main] DEBUG com.like.dao.IUserDao.findById - ==> Parameters: 41(Integer) 2019-04-04 21:00:27,502 549 [ main] DEBUG com.like.dao.IUserDao.findById - <== Total: 1 User{id=41, username='老王', birthday=Tue Feb 27 17:47:08 CST 2018, sex='男', address='北京', accounts=null} User{id=41, username='老王', birthday=Tue Feb 27 17:47:08 CST 2018, sex='男', address='北京', accounts=null} 2019-04-04 21:00:27,506 553 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1b26f7b2] 2019-04-04 21:00:27,507 554 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1b26f7b2] ``` ## 关闭session ~~~ SqlSession session1 = build.openSession(); IUserDao mapper1 = session1.getMapper(IUserDao.class); User user1 = mapper1.findById(41); System.out.println(user1); session1.close(); SqlSession session2 = build.openSession(); IUserDao mapper2 = session2.getMapper(IUserDao.class); User user2 = mapper2.findById(41); System.out.println(user2); ~~~ 可以看到查询了两次: ``` 2019-04-04 21:02:08,396 0 [ main] DEBUG ache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter. 2019-04-04 21:02:08,633 237 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection 2019-04-04 21:02:08,900 504 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@eafc191] 2019-04-04 21:02:08,909 513 [ main] DEBUG com.like.dao.IUserDao.findById - ==> Preparing: select * from user where id = ? 2019-04-04 21:02:08,972 576 [ main] DEBUG com.like.dao.IUserDao.findById - ==> Parameters: 41(Integer) 2019-04-04 21:02:08,999 603 [ main] DEBUG com.like.dao.IUserDao.findById - <== Total: 1 User{id=41, username='老王', birthday=Tue Feb 27 17:47:08 CST 2018, sex='男', address='北京', accounts=null} 2019-04-04 21:02:09,003 607 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@eafc191] 2019-04-04 21:02:09,004 608 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@eafc191] 2019-04-04 21:02:09,006 610 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection 2019-04-04 21:02:09,019 623 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@78ac1102] 2019-04-04 21:02:09,020 624 [ main] DEBUG com.like.dao.IUserDao.findById - ==> Preparing: select * from user where id = ? 2019-04-04 21:02:09,021 625 [ main] DEBUG com.like.dao.IUserDao.findById - ==> Parameters: 41(Integer) 2019-04-04 21:02:09,022 626 [ main] DEBUG com.like.dao.IUserDao.findById - <== Total: 1 User{id=41, username='老王', birthday=Tue Feb 27 17:47:08 CST 2018, sex='男', address='北京', accounts=null} ``` ## 手动清空缓存 ~~~ session.clearCache(); ~~~ ## 触发清空一级缓存的情况 当调用session的修改,添加,删除,commit(),close()等方法时,就会清空一级缓存.