mybatis缓存介绍

原文地址: http://www.cnblogs.com/selene/p/4638648.html

一级缓存示例:

 1 public static void main(String[] args){
 2         String resource = "mybatis-config.xml";
 3         try {
 4             Reader reader = Resources.getResourceAsReader(resource);
 5             SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);
 6             SqlSession sqlSession  = sqlSessionFactory.openSession();
 7             AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
 8 
 9             Account account = accountDao.selectById(1);
10             Account account1 = accountDao.selectById(1);
11             sqlSession.commit();
12             Account account2 = accountDao.selectById(1);
13         } catch (IOException e) {
14             e.printStackTrace();
15         }
16     }

mysql执行日志:

可以看到,方法调用了三次,但SQL语句只执行了两次,第二次调用查询的时候取的是缓存

posted @ 2016-01-08 19:09  桦沐  阅读(225)  评论(0编辑  收藏  举报