用python开发网站开发技术,如何在公司网站上添加内容,怎么下载网页上的视频,网站怎么做展现量Mybatis提供对缓存的支持#xff0c;分为一级缓存和二级缓存#xff0c;在没有配置的情况下#xff0c;系统默认会使用一级缓存。 一级缓存#xff08;SqlSession级别#xff09; 我们都知道每个SqlSession对象之间的缓存是互不影响的#xff0c;当同一个SqlSession执行多…Mybatis提供对缓存的支持分为一级缓存和二级缓存在没有配置的情况下系统默认会使用一级缓存。 一级缓存SqlSession级别 我们都知道每个SqlSession对象之间的缓存是互不影响的当同一个SqlSession执行多次相同的SQL语句时主要针对select系统只会到底层访问数据库一次后续执行sql时会从一级缓存里面读取第一次访问的数据。当这个SqlSession对象执行close()或者显示声明清空缓存时对应的一级缓存也会清空从而提高效率。值得注意的是如果SqlSession执行DML操作即insert、update、delete并提交到数据库时也会起到清空该SqlSession对象对应的一级缓存的作用目的是为了保证缓存里面的数据是最新的。避免脏读现象。 二级缓存SqlSessionFactory级别 有些书籍说二级缓存是Mapper级别可能是依据二级缓存的配置是在xxxMapper.xml上配置的吧不过本人更认同是SqlSessionFactory级别为什么呢因为同一个Configuration里面是创建一个SqlSessionFactorySqlSessionFactory是属于线程安全的SqlSessionFactory可以创建很多个SqlSession来进行事务操作也就是说SqlSessionFactory是由很多个SqlSession对象共享的同样的二级缓存的数据也是由很多个SqlSession共享的。如何才能让系统操作二级缓存呢原理很简单不同的SqlSession对象执行相同的namespace下的sql语句当第一个SqlSession对象调用close()关闭一级缓存时第一次查询得到的数据将会被保存到二级缓存后面的SqlSession对象调用相同sql语句时就会从二级缓存中获取数据。当然使用二级缓存需要对应的返回对象即POJO实现序列化。 配置在需要使用的xxxMapper.xml里面配置cache evictionLRU flushInterval100000 size1024 readOnlytrue / eviction代表回收策略目前支持4种策略 1.LRU最近最少使用的移除最长时间不用的对象 2.FIFO先进先出 3.SOFT移除基于垃圾回收器状态和软引用规则的对象 4.WEAK更积极移除基于垃圾回收器状态和弱引用规则的对象。 flushInterval代表刷新时长单位毫秒 size代表缓存最多可以存储多少个对象注意设置过大会导致内存溢出 readOnly意味着缓存数据只能读取不能修改 上面是较为详细配置当然也可以简单一点直接写上cache /就可以了。 下面给出一个较为有意思的栗子 xml配置代码如下 ?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.learn.mapper.EmployeeMappercache /select idgetEmpList resultTypeemployeeselect * from tb_employee/select/mapper JUnit4测试代码如下 Testpublic void testCache(){SqlSession ss1 null;SqlSession ss2 null;try{ss1 SqlSessionFactoryUtil.initSqlSessionFactory().openSession();ss2 SqlSessionFactoryUtil.initSqlSessionFactory().openSession();EmployeeMapper em1 ss1.getMapper(EmployeeMapper.class);EmployeeMapper em2 ss2.getMapper(EmployeeMapper.class);ListEmployee list1 em1.getEmpList();//ss1.close();list1 em2.getEmpList();}catch(Exception e){ss1.rollback();ss2.rollback();e.printStackTrace();}finally{if(ss1 ! null){ss1.close();}if(ss2 ! null){ss2.close();}}} 代码中实例化两个SqlSession对象分别是ss1和ss2用户检验数据读取的操作。留意上面注释了ss1.close();这一行。 下面是执行上面测试代码的日志结果 Logging initialized using class org.apache.ibatis.logging.slf4j.Slf4jImpl adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Cache Hit Ratio [com.learn.mapper.EmployeeMapper]: 0.0
Opening JDBC Connection
Created connection 275310919.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection1068e947]Preparing: select * from tb_employee Parameters: Total: 6
Cache Hit Ratio [com.learn.mapper.EmployeeMapper]: 0.0
Opening JDBC Connection
Created connection 1948863195.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection74294adb]Preparing: select * from tb_employee Parameters: Total: 6
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection1068e947]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection1068e947]
Returned connection 275310919 to pool.
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection74294adb]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection74294adb]
Returned connection 1948863195 to pool. 可以看到即使配置了cache /二级缓存第一个SqlSession对象ss1没有手动执行close()方法时ss1对应的一级缓存数据仍然没有保存到二级缓存里面即二级缓存里面没有数据当第二个SqlSession对象ss2执行相同的sql语句时会找一级缓存有没有数据因为第一次执行当然没有数据了然后找二级缓存刚才说过了ss1的一级缓存数据并没有保存到二级缓存里面所以二级缓存也没有数据因此ss2就会再次操作数据库。可以看到log日志会有两条select语句。 此时如果将close();的注释去掉再执行一下测试代码日志结果如下 Logging initialized using class org.apache.ibatis.logging.slf4j.Slf4jImpl adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Cache Hit Ratio [com.learn.mapper.EmployeeMapper]: 0.0
Opening JDBC Connection
Created connection 275310919.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection1068e947]Preparing: select * from tb_employee Parameters: Total: 6
Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection1068e947]
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection1068e947]
Returned connection 275310919 to pool.
Cache Hit Ratio [com.learn.mapper.EmployeeMapper]: 0.5 显而易见此时只访问一次数据库。转载于:https://www.cnblogs.com/SysoCjs/p/9574318.html