本视频观看地址:https://edu.51cto.com/sd/3ec2c
1、缓存
1.1、缓存的意义
将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题
1.2、一级缓存
MyBatis的一级缓存的作用域是session,当openSession()后,如果执行相同的sql(相同语句和参数,MyBatis不执行sql,而是从缓存中命中并返回)
Mybatis执行查询时首先去缓存区中命中,如果命中直接返回,没有命中则执行sql,从数据库中查询
注意:mybatis和spring整合后进行mapper代理开发,不支持一级缓存,mybatis和spring整合,spring按照mapper的模板去生成mapper代理对象,模板中在最后统一关闭sqlsession
1.2.1、测试一级缓存
@Test
public void testCache()throws Exception{
User user = userMapper.selectUserById(8);
System.out.println(user);
User user2 = userMapper.selectUserById(8);
System.out.println(user2);
}
日志文件
DEBUG - Opening JDBC Connection
DEBUG - Created connection 1667925979.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@636a87db]
DEBUG - ==> Preparing: select userid,user_name as userName,age,pwd,sex,birthday from tb_user where userid = ?
DEBUG - ==> Parameters: 8(Integer)
DEBUG - <== Total: 1
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@636a87db]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@636a87db]
DEBUG - Returned connection 1667925979 to pool.
发现只发出一条sql语句,第二次执行实际上是从缓存拿去这个数据
1.2.2、刷新缓存操作
sqlSession.clearCache();
@Test
public void testCache()throws Exception{
User user = userMapper.selectUserById(8);
System.out.println(user);
//清除缓存
sqlSession.clearCache();
User user2 = userMapper.selectUserById(8);
System.out.println(user2);
}
日志文件
DEBUG - Opening JDBC Connection
DEBUG - Created connection 823632238.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@3117a16e]
DEBUG - ==> Preparing: select userid,user_name as userName,age,pwd,sex,birthday from tb_user where userid = ?
DEBUG - ==> Parameters: 8(Integer)
DEBUG - <== Total: 1
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
DEBUG - ==> Preparing: select userid,user_name as userName,age,pwd,sex,birthday from tb_user where userid = ?
DEBUG - ==> Parameters: 8(Integer)
DEBUG - <== Total: 1
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@3117a16e]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@3117a16e]
DEBUG - Returned connection 823632238 to pool.
执行了更新操作
@Test
public void testCache()throws Exception{
User user = userMapper.selectUserById(8);
System.out.println(user);
user.setUserName("李白");
userMapper.insertUser(user);
sqlSession.commit();
User user2 = userMapper.selectUserById(8);
System.out.println(user2);
}
日志文件
DEBUG - Opening JDBC Connection
DEBUG - Created connection 1782252248.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6a3b02d8]
DEBUG - ==> Preparing: select userid,user_name as userName,age,pwd,sex,birthday from tb_user where userid = ?
DEBUG - ==> Parameters: 8(Integer)
DEBUG - <== Total: 1
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
DEBUG - ==> Preparing: insert into tb_user(userid,user_name,age,pwd,sex,birthday) values(seq_user.nextval,?,?,?,?,?)
DEBUG - ==> Parameters: 李白(String), 18(Integer), 123456(String), 男(String), 2018-08-14 10:19:19.0(Timestamp)
DEBUG - <== Updates: 1
DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@6a3b02d8]
DEBUG - ==> Preparing: select userid,user_name as userName,age,pwd,sex,birthday from tb_user where userid = ?
DEBUG - ==> Parameters: 8(Integer)
DEBUG - <== Total: 1
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6a3b02d8]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6a3b02d8]
DEBUG - Returned connection 1782252248 to pool.
1.3、二级缓存
mybatis 的二级缓存的作用域是一个mapper的namespace ,同一个namespace中查询sql可以从缓存中命中
每次查询先看是否开启二级缓存,如果开启从二级缓存的数据结构中取缓存数据,
二级缓存的范围是mapper级别(mapper同一个命名空间),mapper以命名空间为单位创建缓存数据结构,结构是map
如果从二级缓存没有取到,再从一级缓存中找,如果一级缓存也没有 ,从数据库中查询
1.3.1、二级缓存
mybatis二级缓存需要将查询结果映射的pojo实现 java.io.serializable接口,如果不实现则抛出异常
二级缓存可以将内存的数据写到磁盘,存在对象的序列化和反序列化,所以要实现java.io.serializable接口。
如果结果映射的pojo中还包括了pojo,都要实现java.io.serializable接口。
1.3.2、开启二级缓存
开启全局开关(mybatis-config.xml)
开启局部开关
...
1.3.3、测试二级缓存
@Test
public void testCache2()throws Exception{
User user = userMapper.selectUserById(8);
System.out.println(user);
sqlSession.close();
sqlSession = sqlSessionFactory.openSession();
userMapper = sqlSession.getMapper(UserMapper.class);
User user2 = userMapper.selectUserById(8);
System.out.println(user2);
}
日志文件
DEBUG - Checking to see if class cn.org.kingdom.mapper.UserMapper matches criteria [is assignable to Object]
DEBUG - Cache Hit Ratio [cn.org.kingdom.mapper.UserMapper]: 0.0
DEBUG - Opening JDBC Connection
DEBUG - Created connection 221388699.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@d321f9b]
DEBUG - ==> Preparing: select userid,user_name as userName,age,pwd,sex,birthday from tb_user where userid = ?
DEBUG - ==> Parameters: 8(Integer)
DEBUG - <== Total: 1
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@d321f9b]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@d321f9b]
DEBUG - Returned connection 221388699 to pool.
DEBUG - Cache Hit Ratio [cn.org.kingdom.mapper.UserMapper]: 0.5
User [userid=8, userName=hello, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
1.3.4、刷新缓存
如果sqlsession操作commit操作,对二级缓存进行刷新(全局清空)。设置statement的flushCache是否刷新缓存,默认值是true。
测试类
@Test
public void testCache2()throws Exception{
//查询
User user = userMapper.selectUserById(8);
System.out.println(user);
sqlSession.close();
//更新操作
user.setUserName("小乔");
SqlSession session2 = sqlSessionFactory.openSession();
UserMapper userMapper3 = session2.getMapper(UserMapper.class);
userMapper3.updateUser(user);
session2.commit();
//再次查询
sqlSession = sqlSessionFactory.openSession();
userMapper = sqlSession.getMapper(UserMapper.class);
User user2 = userMapper.selectUserById(8);
System.out.println(user2);
}
mapper.xml中
update tb_user set user_name=#{userName},age=#{age},pwd=#{pwd},sex=#{sex},birthday=#{birthday}
where userid=#{userid}
日志
DEBUG - Created connection 876236253.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@343a4ddd]
DEBUG - ==> Preparing: select userid,user_name as userName,age,pwd,sex,birthday from tb_user where userid = ?
DEBUG - ==> Parameters: 8(Integer)
DEBUG - <== Total: 1
User [userid=8, userName=冰封战神, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
DEBUG - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@343a4ddd]
DEBUG - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@343a4ddd]
DEBUG - Returned connection 876236253 to pool.
DEBUG - Opening JDBC Connection
DEBUG - Checked out connection 876236253 from pool.
DEBUG - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@343a4ddd]
DEBUG - ==> Preparing: update tb_user set user_name=?,age=?,pwd=?,sex=?,birthday=? where userid=?
DEBUG - ==> Parameters: 小乔(String), 18(Integer), 123456(String), 男(String), 2018-08-14 10:19:19.0(Timestamp), 8(Integer)
DEBUG - <== Updates: 1
DEBUG - Committing JDBC Connection [oracle.jdbc.driver.T4CConnection@343a4ddd]
DEBUG - Cache Hit Ratio [cn.org.kingdom.mapper.UserMapper]: 0.5
User [userid=8, userName=冰封战神, pwd=123456, age=18, sex=男, birthday=Tue Aug 14 10:19:19 CST 2018]
对于缓存使用,一般情况,我们要经常使用的数据,这些数据不会发生太大的变化,或者对用户没有什么影响
1.3.5、缓存的高级设置
这个更高级的配置创建了一个 FIFO 缓存,并每隔 60 秒刷新,存数结果对象或列表的 512 个引用,而且返回的对象被认为是只读的,因此在不同线程中的调用者之间修改它们会 导致冲突。
可用的收回策略有:
LRU
– 最近最少使用的:移除最长时间不被使用的对象。FIFO
– 先进先出:按对象进入缓存的顺序来移除它们。SOFT
– 软引用:移除基于垃圾回收器状态和软引用规则的对象。WEAK
– 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。
默认的是 LRU。
flushInterval(刷新间隔)可以被设置为任意的正整数,而且它们代表一个合理的毫秒 形式的时间段。默认情况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。
size(引用数目)可以被设置为任意正整数,要记住你缓存的对象数目和你运行环境的 可用内存资源数目。默认值是 1024。
readOnly(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓 存对象的相同实例。因此这些对象不能被修改。这提供了很重要的性能优势。可读写的缓存 会返回缓存对象的拷贝(通过序列化) 。这会慢一些,但是安全,因此默认是 false。
最后欢迎关注我的网络课堂:https://edu.51cto.com/sd/ef353