MyBatis学习(六)

本视频观看地址:https://edu.51cto.com/sd/3ec2c java

一、缓存

1.一、缓存的意义

将用户常常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提升查询效率,解决了高并发系统的性能问题spring

1.二、一级缓存

MyBatis的一级缓存的做用域是session,当openSession()后,若是执行相同的sql(相同语句和参数,MyBatis不执行sql,而是从缓存中命中并返回)
Mybatis执行查询时首先去缓存区中命中,若是命中直接返回,没有命中则执行sql,从数据库中查询
注意:mybatis和spring整合后进行mapper代理开发,不支持一级缓存,mybatis和spring整合,spring按照mapper的模板去生成mapper代理对象,模板中在最后统一关闭sqlsessionsql

1.2.一、测试一级缓存

@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.二、刷新缓存操做

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.

执行了更新操做session

@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.三、二级缓存

mybatis 的二级缓存的做用域是一个mapper的namespace ,同一个namespace中查询sql能够从缓存中命中 mybatis

每次查询先看是否开启二级缓存,若是开启从二级缓存的数据结构中取缓存数据,

二级缓存的范围是mapper级别(mapper同一个命名空间),mapper以命名空间为单位建立缓存数据结构,结构是map<key、value>。

若是从二级缓存没有取到,再从一级缓存中找,若是一级缓存也没有 ,从数据库中查询

1.3.一、二级缓存

mybatis二级缓存须要将查询结果映射的pojo实现 java.io.serializable接口,若是不实现则抛出异常

二级缓存能够将内存的数据写到磁盘,存在对象的序列化和反序列化,因此要实现java.io.serializable接口。

若是结果映射的pojo中还包括了pojo,都要实现java.io.serializable接口。

1.3.二、开启二级缓存

开启全局开关(mybatis-config.xml)

<settings>
        <setting name="mapUnderscoreToCamelCase" value="false"/>
        <!-- 二级缓存的全局开关 -->
        <setting name="cacheEnabled" value="true"/>
</settings>

开启局部开关

<mapper namespace="cn.org.kingdom.mapper.UserMapper">
    <cache/>
    ...
</mapper>

1.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.四、刷新缓存

若是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 id="updateUser" flushCache="false">
    update tb_user set user_name=#{userName},age=#{age},pwd=#{pwd},sex=#{sex},birthday=#{birthday}
    where userid=#{userid}
</update>

日志

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.五、缓存的高级设置

<cache
  eviction="FIFO"
  flushInterval="60000"
  size="512"
  readOnly="true"/>

这个更高级的配置建立了一个 FIFO 缓存,并每隔 60 秒刷新,存数结果对象或列表的 512 个引用,并且返回的对象被认为是只读的,所以在不一样线程中的调用者之间修改它们会 致使冲突。

可用的收回策略有:

  • LRU – 最近最少使用的:移除最长时间不被使用的对象。
  • FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
  • SOFT – 软引用:移除基于垃圾回收器状态和软引用规则的对象。
  • WEAK – 弱引用:更积极地移除基于垃圾收集器状态和弱引用规则的对象。

默认的是 LRU。

flushInterval(刷新间隔)能够被设置为任意的正整数,并且它们表明一个合理的毫秒 形式的时间段。默认状况是不设置,也就是没有刷新间隔,缓存仅仅调用语句时刷新。

size(引用数目)能够被设置为任意正整数,要记住你缓存的对象数目和你运行环境的 可用内存资源数目。默认值是 1024。

readOnly(只读)属性能够被设置为 true 或 false。只读的缓存会给全部调用者返回缓 存对象的相同实例。所以这些对象不能被修改。这提供了很重要的性能优点。可读写的缓存 会返回缓存对象的拷贝(经过序列化) 。这会慢一些,可是安全,所以默认是 false。

最后欢迎关注个人网络课堂:https://edu.51cto.com/sd/ef353

相关文章
相关标签/搜索