原文地址: 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语句只执行了两次,第二次调用查询的时候取的是缓存