MyBatis一级缓存介绍
MyBatis一级缓存介绍
默认情况下,MyBatis只启用了本地的会话缓存,它仅仅对一个会话中的数据进行缓存。这也就是大家常说的MyBatis一级缓存,一级缓存的作用域是SqlSession。
MyBatis一级缓存的运行过程是这样的:执行SQL语句的过程中,首次执行它时从数据库获取的所有数据会被存储在一段高速缓存中,今后执行这条语句时就会从高速缓存中读取结果,而不是再次查询数据库。MyBatis提供了默认下基于Java HashMap的缓存实现。
重点是要明白:MyBatis执行SQL语句之后,这条语句的执行结果被缓存,以后再执行这条语句的时候,会直接从缓存中拿结果,而不是再次执行SQL。但是一旦执行新增或更新或删除操作,缓存就会被清除。下面将分情况来验证一下。
第1种情况:同个session进行两次相同查询
public class Test { public static void main(String[] args) throws Exception { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Person p1 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1); Person p2 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1); session.commit(); session.close(); } }
结论:MyBatis只进行1次数据库查询。
==> Preparing: select * from t_person where id = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='Kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010}
User{id=1, name='Kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010}
第2种情况:同个session进行两次不同的查询。
public class Test { public static void main(String[] args) throws Exception { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Person p1 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1); Person p2 = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 2); session.commit(); session.close(); } }
结论:MyBatis进行两次数据库查询。
==> Preparing: select * from t_person where id = ? ==> Parameters: 1(Integer) <== Total: 1 User{id=1, name='kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010} ==> Preparing: select * from t_person where id = ? ==> Parameters: 2(Integer) <== Total: 1 User{id=2, name='Jim', age=50, birthday=Sat Dec 06 17:12:01 CST 2010}
第3种情况:不同session,进行相同查询。
public class Test { public static void main(String[] args) throws Exception { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session1 = sqlSessionFactory.openSession(); Person p1 = session1.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1); SqlSession session2 = sqlSessionFactory.openSession(); Person p2 = session2.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 2); session1.commit(); session2.commit(); session1.close(); session2.close(); } }
结论:MyBatis进行两次数据库查询。
==> Preparing: select * from t_person where id = ? ==> Parameters: 1(Integer) <== Total: 1 User{id=1, name='kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010} ==> Preparing: select * from t_person where id = ? ==> Parameters: 2(Integer) <== Total: 1 User{id=2, name='Jim', age=50, birthday=Sat Dec 06 17:12:01 CST 2010}
第4种情况:同个session,查询之后更新数据,再次查询相同的语句。
public class Test { public static void main(String[] args) throws Exception { InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Person p = null; p = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1); p.setAge(30); session.update("cn.mybatis.mydemo.mapper.PersonMapper.updatePerson", p); p = session.selectOne("cn.mybatis.mydemo.mapper.PersonMapper.selectPersonById", 1); session.commit(); session.close(); } }
结论:更新操作之后缓存会被清除:
==> Preparing: select * from t_person where id = ? ==> Parameters: 1(Integer) <== Total: 1 User{id=1, name='kit', age=23, birthday=Sun Oct 12 23:20:13 CST 2010} ==> Preparing: update t_person set age = ? where ID = ? ==> Parameters: 30(Integer), 1(Integer) <== Updates: 1 ==> Preparing: sselect * from t_person where id = ? ==> Parameters: 1(Integer) <== Total: 1 User{id=1, name='kit', age=30, birthday=Sun Oct 12 23:20:13 CST 2010}
小结
很明显,以上各种情况验证了一级缓存的概念,在同个SqlSession中,查询语句相同的sql会被缓存,但是一旦执行新增或更新或删除操作,缓存就会被清除。