1.为什么使用缓存?
首次访问时,查询数据库,并将数据存储到内存中;再次访问时直接访问缓存,减少IO、硬盘读写次数、提高效率
2.Mybatis中的一级缓存和二级缓存
(1)一级缓存
它指的是mybatis中的SqlSession对象的缓存。当我们执行完查询之后,查询的结果会同时存在在SqlSession为我们提供的一块区域中。当我们再次查询同样的数据,mybatis会先去SqlSession中查询是否有,有的话直接拿出来使用。当SqlSession对象消失时,Mybatis的一级缓存也就消失了。
(2)二级缓存
它指的是Mybatis中SqlSessionFactory对象的缓存,由同一个SqlSessioFactory对象创建的SqlSession共享其缓存。
1.mapper
public User findUserById(Integer id);
2.测试一级缓存
public class MyBatisTest {
private InputStream inputStream;
private SqlSession sqlSession;
private SqlSessionFactory sqlSessionFactory;
@Before
public void before() throws IOException {
String rescource = "mybatis-config.xml";
//加载配置文件
inputStream =Resources.getResourceAsStream(rescource);
//创建sqlSessionFactory
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//获得数据的会话实例
sqlSession=sqlSessionFactory.openSession();
}
@Test
public void testFirstCache(){
SqlSession sqlSession1 = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession1.getMapper(UserMapper.class);
User userParam1 = userMapper.findUserById(41);//执行查询
System.out.println("================第一次查询=============");
System.out.println("第一次查询"+userParam1);
User userParam2= userMapper.findUserById(41);//不执行查询
System.out.println("================第二次查询=============");
System.out.println("第二次查询"+userParam2);
SqlSession sqlSession2= sqlSessionFactory.openSession();
UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
User userParam3= userMapper2.findUserById(41);//执行查询
System.out.println("================第三次查询=============");
System.out.println("第三次查询"+userParam3);
}
@After
public void close() throws IOException {
/**
* 关闭资源
*/
// sqlSession.close();
// inputStream.close();
}
}
3.结果
一级缓存是SqlSession范围的缓存,当调用SqlSession的commit(),close()等方法时,就会清空一级缓存。
第一次发起查询用户id为 1 的用户信息,先去找缓存中是否有id为 1 的用户信息,如果没有,从数据库查询用户信息。 得到用户信息,将用户信息存储到一级缓存中。
如果sqlSession去执行 commit操作(执行插入、更新、删除),清空 SqlSession 中的一级缓存,这样做的目的为了让缓存中存储的是最新的信息,避免脏读。
第二次发起查询用户id为1的用户信息,先去找缓存中是否有id为1的用户信息,缓存中有,直接从缓存中获取用户信息。
清空缓存前
public class MyBatisTest {
private InputStream inputStream;
private SqlSession sqlSession;
private SqlSessionFactory sqlSessionFactory;
@Before
public void before() throws IOException {
String rescource = "mybatis-config.xml";
//加载配置文件
inputStream =Resources.getResourceAsStream(rescource);
//创建sqlSessionFactory
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//获得数据的会话实例
sqlSession=sqlSessionFactory.openSession();
}
@Test
public void testFirstCache(){
SqlSession sqlSession1 = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession1.getMapper(UserMapper.class);
User userParam1 = userMapper.findUserById(41);//执行查询
System.out.println("================第一次查询=============");
System.out.println("第一次查询"+userParam1);
User userParam2= userMapper.findUserById(41);//不执行查询
System.out.println("================第二次查询=============");
System.out.println("第二次查询"+userParam2);
// sqlSession1.commit();
// SqlSession sqlSession2= sqlSessionFactory.openSession();
// UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
User userParam3= userMapper.findUserById(41);
System.out.println("================第三次查询=============");
System.out.println("第三次查询"+userParam3);
}
@After
public void close() throws IOException {
/**
* 关闭资源
*/
// sqlSession.close();
// inputStream.close();
}
}
清空缓存后
public class MyBatisTest {
private InputStream inputStream;
private SqlSession sqlSession;
private SqlSessionFactory sqlSessionFactory;
@Before
public void before() throws IOException {
String rescource = "mybatis-config.xml";
//加载配置文件
inputStream =Resources.getResourceAsStream(rescource);
//创建sqlSessionFactory
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//获得数据的会话实例
sqlSession=sqlSessionFactory.openSession();
}
@Test
public void testFirstCache(){
SqlSession sqlSession1 = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession1.getMapper(UserMapper.class);
User userParam1 = userMapper.findUserById(41);//执行查询
System.out.println("================第一次查询=============");
System.out.println("第一次查询"+userParam1);
User userParam2= userMapper.findUserById(41);//不执行查询
System.out.println("================第二次查询=============");
System.out.println("第二次查询"+userParam2);
sqlSession1.commit();
// SqlSession sqlSession2= sqlSessionFactory.openSession();
// UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
User userParam3= userMapper.findUserById(41);
System.out.println("================第三次查询=============");
System.out.println("第三次查询"+userParam3);
}
@After
public void close() throws IOException {
/**
* 关闭资源
*/
// sqlSession.close();
// inputStream.close();
}
}
注意:当我们在使用二级缓存时,所缓存的类一定要实现java.io.Serializable接口,这种就可以使用序列化方式来保存对象。
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
//get/set省略...
(1)在SqlMapConfig.xml 文件开启二级缓存
(2)配置相关的Mapper映射文件
(3)mapper
public User findUserById2(Integer id);
public class MyBatisTest {
private InputStream inputStream;
private SqlSession sqlSession;
private SqlSessionFactory sqlSessionFactory;
@Before
public void before() throws IOException {
String rescource = "mybatis-config.xml";
//加载配置文件
inputStream =Resources.getResourceAsStream(rescource);
//创建sqlSessionFactory
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//获得数据的会话实例
sqlSession=sqlSessionFactory.openSession();
}
@Test
public void testSecondUserById(){
SqlSession sqlSession1 = sqlSessionFactory.openSession();
UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
User user1 = userMapper1.findUserById2(41);//执行查询
System.out.println("第一次查询:" + user1);
sqlSession1.commit();//二级缓存在sqlSession.commit()或者sqlSession.close()之后生效
SqlSession sqlSession2 = sqlSessionFactory.openSession();
UserMapper userMapper2= sqlSession2.getMapper(UserMapper.class);
User user2 =userMapper2.findUserById2(41);//不执行查询
System.out.println("第二次查询:" + user2);
}
@After
public void close() throws IOException {
/**
* 关闭资源
*/
// sqlSession.close();
// inputStream.close();
}
}
二级缓存是mapper映射级别的缓存,多个SqlSession去操作同一个Mapper映射的sql语句,多个SqlSession可以共用二级缓存,二级缓存是跨SqlSession的。
二级缓存结构图:
@Test
public void testSecondUserById(){
SqlSession sqlSession1 = sqlSessionFactory.openSession();
UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
User user1 = userMapper1.findUserById2(41);//执行查询
System.out.println("第一次查询:" + user1);
sqlSession1.commit();//二级缓存在sqlSession.commit()或者sqlSession.close()之后生效
SqlSession sqlSession2 = sqlSessionFactory.openSession();
UserMapper userMapper2= sqlSession2.getMapper(UserMapper.class);
User user2 =userMapper2.findUserById2(41);//不执行查询
System.out.println("第二次查询:" + user2);
sqlSession2.commit();
SqlSession sqlSession3 = sqlSessionFactory.openSession();
UserMapper userMapper3= sqlSession3.getMapper(UserMapper.class);
User user3 =userMapper3.findUserById2(41);//不执行查询
System.out.println("第三次查询:" + user3);
sqlSession3.commit();
sqlSession3.close();
}
结果