建设一个营销网站有哪些步骤传奇世界网页版星装
其实缓存字面的意思就是将一些内容缓存下来,等下次使用的时候可以直接调用,通过数据库得到数据,有时候会使用相同的数据,所以mybatis自然也支持缓存。
而mybatis按照缓存的效果可以分两大类:一级缓存和二级缓存。
一级缓存
其实一级缓存是SqlSession级别,通过SqlSession查询的数据会被缓存,下次查询相同的数据,就会从缓存中直接获取,不会从数据库中重写访问。
还是老规矩进行代码演示:
List<Student>  getStudent();
 
   <select id="getStudent" resultType="Student">SELECT sid , sname , sage , ssex , gid FROM testmybatis.student</select>
 
然后调用的时候,不关闭SqlSession,然后连续调用getStudent() :
   System.out.println("  --------------------  第一次     " + studentMapper.getStudent());System.out.println("  --------------------  第二次     " + studentMapper.getStudent()); 

可以看出sql语句,只是调用了一次,但是还是得到了数据。
但是一级缓存也是会失效的,其中有四种情况:
-  
不同的SqlSession对应不同一级缓存,比如例子一直说用的同一个sqlsession,也没有关闭sqlsession重写创建了一个。
 -  
同一个SqlSession,但是两次查询的条件不同,甚至是操作相同,但是方法名不同,也不会调用缓存,可以看下演示:
List<Student> getStudent();List<Student> getStudent1();<select id="getStudent" resultType="Student">SELECT sid , sname , sage , ssex , gid FROM testmybatis.student</select><select id="getStudent1" resultType="Student">SELECT sid , sname , sage , ssex , gid FROM testmybatis.student</select>然后调用:
System.out.println(" -------------------- 第一次 " + studentMapper.getStudent());System.out.println(" -------------------- 第二次 " + studentMapper.getStudent());System.out.println(" -------------------- 另一个第一次 " + studentMapper.getStudent1());System.out.println(" -------------------- 第三次 " + studentMapper.getStudent()); 

可以看出哪怕同一个sqlsession其一样的sql语句,通过两个不同方法也不会调用缓存,因为sql语句执行了两次。当然其也不会影响已缓存是数据。
-  
同一个sqlsession同一个查询,在两者依次执行期间,执行了任何依次增改操作。
来实体体验一把:
List<Student> getStudent();void deleteStudent();<select id="getStudent" resultType="Student">SELECT sid , sname , sage , ssex , gid FROM testmybatis.student</select><delete id="deleteStudent"><!--这里删除一个不存在的数据 -->delete from testmybatis.student where sid='100'</delete>System.out.println(" -------------------- 第一次 " + studentMapper.getStudent());studentMapper.deleteStudent();System.out.println(" -------------------- 第二次 " + studentMapper.getStudent()); 

可以看出其删除无论是否存在
-  
同一个SqlSession两次执行同一个查询中介,执行了手动清楚缓存。
System.out.println(" -------------------- 第一次 " + studentMapper.getStudent()); // 调用clearCache() 手动清楚了缓存sqlSession.clearCache();System.out.println(" -------------------- 第二次 " + studentMapper.getStudent()); 

二级缓存
二级缓存是SqlSessionFactory级别的,同一个SqlSessionFactory创建的SqlSession查询的结果会被缓存,此后执行相同的查询语句,数据就会从缓存中得到。
启用的条件:
- 在核心配置文件中,加入配置:
<setting name="cacheEnabled"value="true" /> - 在映射文件中加入
<cache /> - 查询的数据转换的实体类类型,必须实现序列化接口
Serializable。 - SqlSession在关闭或者提交以后二级缓存才会有效。
 
还是老规矩进行演示:
前提在核心配置文件mybatis-config.xml中配置
   <settings><setting name="cacheEnabled" value="true" /></settings>
 
然后再映射文件中添加:
 <cache></cache>
 
实体类需要实现序列化接口:
public class Student  implements Serializable
 
现在开始演示:
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
//        这个通过代理模式,传入什么类返回什么类StudentMapper  studentMapper = sqlSession.getMapper(StudentMapper.class);System.err.println("  --------------------  第一次         sqlSession  -- getStudent     " + studentMapper.getStudent());// 不关闭的话,sqlSession.close();SqlSession sqlSession1 = sqlSessionFactory.openSession();StudentMapper  studentMapper1 = sqlSession1.getMapper(StudentMapper.class);System.err.println("  --------------------  第一次    sqlSession1 --  方法名为 getStudent  " + studentMapper1.getStudent());System.err.println("  --------------------  第一次     sqlSession1 -- 方法名为 getStudent1   " + studentMapper1.getStudent1());studentMapper1.deleteStudent();System.err.println("  --------------------  第二次    sqlSession1 -- 执行deleteStudent后调用 getStudent  " + studentMapper1.getStudent());sqlSession1.close();
 

可以看出了只要删除或者修改了数据库,无论是一级缓存还是二级缓存都会同时失效。
这个又有一个疑问了,那就是一级缓存和二级缓存的查询顺序是什么?
先查询二级缓存,没有再查询二级缓存,如果还没有再查询数据库。
为什么会这样,因为二级缓存覆盖的更广,一级缓存中没有但是在二级缓存中,减少去数据库中取数的频次。
补充--------二级缓存的配置
其实再映射文件中可以配置一些属性:
看一下官网举出的例子:
<cacheeviction="FIFO"flushInterval="60000"size="512"readOnly="true"/>
 
eviction 属性是缓存回收策略,默认是LRU。
这个更高级的配置创建了一个 FIFO 缓存,每隔 60 秒刷新,最多可以存储结果对象或列表的 512 个引用,而且返回的对象被认为是只读的,因此对它们进行修改可能会在不同线程中的调用者产生冲突。
可用的清除策略有:
LRU– 最近最少使用:移除最长时间不被使用的对象。FIFO– 先进先出:按对象进入缓存的顺序来移除它们。SOFT– 软引用:基于垃圾回收器状态和软引用规则移除对象。WEAK– 弱引用:更积极地基于垃圾收集器状态和弱引用规则移除对象。
默认的清除策略是 LRU。
flushInterval(刷新间隔)属性可以被设置为任意的正整数,设置的值应该是一个以毫秒为单位的合理时间量。 默认情况是不设置,也就是没有刷新间隔,缓存仅仅会在调用语句时刷新。
size(引用数目)属性可以被设置为任意正整数,要注意欲缓存对象的大小和运行环境中可用的内存资源。默认值是 1024。
readOnly(只读)属性可以被设置为 true 或 false。只读的缓存会给所有调用者返回缓存对象的相同实例。 因此这些对象不能被修改。这就提供了可观的性能提升。而可读写的缓存会(通过序列化)返回缓存对象的拷贝。 速度上会慢一些,但是更安全,因此默认值是 false。
当然还有一个属性没有写,那就是type:其在使用第三方的缓存的时候使用,因为有些第三方的缓存的功能会更强大,所以有时候会使用第三方缓存,这个就下一篇具体聊。
