mybatis一级缓存
https://mp.weixin.qq.com/s?__biz=MzI4Njc5NjM1NQ==&mid=2247486487&idx=1&sn=1c4ab7f32e841b550a25a5b9c08ea772&chksm=ebd6333bdca1ba2d0784572d8e90989f01b05bba4eb21797bc40569cd0799bc16a8455bc742b&mpshare=1&scene=1&srcid=100322fX10AwCkzs7rijvWFv&key=9ec638334fce6a4830af5e5e3d2064f39b2501b8c176d7e1a682dcd7911d50c24996245a15b530988ee3bba588acbe6b585f77dfa8afeadfe8bab88c4148689a1cc99a0f2cf61c087ec2b2d459fe6ccb&ascene=0&uin=MTA2NzUxMDAyNQ%3D%3D&devicetype=iMac+MacBookAir6%2C2+OSX+OSX+10.10.5+build(14F2511)&version=11020012&lang=zh_CN&pass_ticket=pPNKGlObNJE8D1Cy5YHUbptxjVEzzwJnsSwaAJ67uUGXcPrFGJzurwfZ6aX8xWBU
同个session进行两次相同查询:
@Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
User user2 = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user2);
} finally {
sqlSession.close();
}
}
MyBatis只进行1次数据库查询:
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2014}
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2014}
同个session进行两次不同的查询:
@Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
User user2 = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 2);
log.debug(user2);
} finally {
sqlSession.close();
}
}
MyBatis进行两次数据库查询:
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2014}
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 2(Integer)
<== Total: 1
User{id=2, name='FFF', age=50, birthday=Sat Dec 06 17:12:01 CST 2014}
不同session,进行相同查询:
@Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession2 = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
User user2 = (User)sqlSession2.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user2);
} finally {
sqlSession.close();
sqlSession2.close();
}
}
MyBatis进行了两次数据库查询:
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2014}
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2014}
同个session,查询之后更新数据,再次查询相同的语句:
@Test
public void test() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user);
user.setAge(100);
sqlSession.update("org.format.mybatis.cache.UserMapper.update", user);
User user2 = (User)sqlSession.selectOne("org.format.mybatis.cache.UserMapper.getById", 1);
log.debug(user2);
sqlSession.commit();
} finally {
sqlSession.close();
}
}
更新操作之后缓存会被清除:
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2014}
==> Preparing: update USERS SET NAME = ? , AGE = ? , BIRTHDAY = ? where ID = ?
==> Parameters: format(String), 23(Integer), 2014-10-12 23:20:13.0(Timestamp), 1(Integer)
<== Updates: 1
==> Preparing: select * from USERS WHERE ID = ?
==> Parameters: 1(Integer)
<== Total: 1
User{id=1, name='format', age=23, birthday=Sun Oct 12 23:20:13 CST 2014}
很明显,结果验证了一级缓存的概念,在同个SqlSession中,查询语句相同的sql会被缓存,但是一旦执行新增或更新或删除操作,缓存就会被清除