Mybatis对缓存提供支持,可是在没有配置的默认状况下,它只开启一级缓存,一级缓存只是相对于同一个SqlSession而言。因此在参数和SQL彻底同样的状况下,咱们使用同一个SqlSession对象调用一个Mapper方法,每每只执行一次SQL,由于使用SelSession第一次查询后,MyBatis会将其放在缓存中,之后再查询的时候,若是没有声明须要刷新,而且缓存没有超时的状况下,SqlSession都会取出当前缓存的数据,而不会再次发送SQL到数据库。html
为何要使用一级缓存,不用多说也知道个大概。可是还有几个问题咱们要注意一下。java
一、一级缓存的生命周期有多长?mysql
a、MyBatis在开启一个数据库会话时,会 建立一个新的SqlSession对象,SqlSession对象中会有一个新的Executor对象。Executor对象中持有一个新的PerpetualCache对象;当会话结束时,SqlSession对象及其内部的Executor对象还有PerpetualCache对象也一并释放掉。git
b、若是SqlSession调用了close()方法,会释放掉一级缓存PerpetualCache对象,一级缓存将不可用。算法
c、若是SqlSession调用了clearCache(),会清空PerpetualCache对象中的数据,可是该对象仍可以使用。spring
d、SqlSession中执行了任何一个update操做(update()、delete()、insert()) ,都会清空PerpetualCache对象的数据,可是该对象能够继续使用sql
二、怎么判断某两次查询是彻底相同的查询?数据库
mybatis认为,对于两次查询,若是如下条件都彻底同样,那么就认为它们是彻底相同的两次查询。apache
2.1 传入的statementId缓存
2.2 查询时要求的结果集中的结果范围
2.3. 此次查询所产生的最终要传递给JDBC java.sql.Preparedstatement的Sql语句字符串(boundSql.getSql() )
2.4 传递给java.sql.Statement要设置的参数值
MyBatis的二级缓存是Application级别的缓存,它能够提升对数据库查询的效率,以提升应用的性能。
SqlSessionFactory层面上的二级缓存默认是不开启的,二级缓存的开席须要进行配置,实现二级缓存的时候,MyBatis要求返回的POJO必须是可序列化的。 也就是要求实现Serializable接口,配置方法很简单,只须要在映射XML文件配置就能够开启缓存了<cache/>,若是咱们配置了二级缓存就意味着:
因为二级缓存的数据不必定都是存储到内存中,它的存储介质多种多样,因此须要给缓存的对象执行序列化。(若是存储在内存中的话,实测不序列化也能够的。)
package com.yihaomen.mybatis.model; import com.yihaomen.mybatis.enums.Gender; import java.io.Serializable; import java.util.List; /** * @ProjectName: springmvc-mybatis */ public class Student implements Serializable{ private static final long serialVersionUID = 735655488285535299L; private String id; private String name; private int age; private Gender gender; private List<Teacher> teachers;
setters&getters()....; toString(); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.yihaomen.mybatis.dao.StudentMapper"> <!--开启本mapper的namespace下的二级缓存--> <!-- eviction:表明的是缓存回收策略,目前MyBatis提供如下策略。 (1) LRU,最近最少使用的,一处最长时间不用的对象 (2) FIFO,先进先出,按对象进入缓存的顺序来移除他们 (3) SOFT,软引用,移除基于垃圾回收器状态和软引用规则的对象 (4) WEAK,弱引用,更积极的移除基于垃圾收集器状态和弱引用规则的对象。这里采用的是LRU, 移除最长时间不用的对形象 flushInterval:刷新间隔时间,单位为毫秒,这里配置的是100秒刷新,若是你不配置它,那么当 SQL被执行的时候才会去刷新缓存。 size:引用数目,一个正整数,表明缓存最多能够存储多少个对象,不宜设置过大。设置过大会致使内存溢出。 这里配置的是1024个对象 readOnly:只读,意味着缓存数据只能读取而不能修改,这样设置的好处是咱们能够快速读取缓存,缺点是咱们没有 办法修改缓存,他的默认值是false,不容许咱们修改 --> <cache eviction="LRU" flushInterval="100000" readOnly="true" size="1024"/> <resultMap id="studentMap" type="Student"> <id property="id" column="id" /> <result property="name" column="name" /> <result property="age" column="age" /> <result property="gender" column="gender" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler" /> </resultMap> <resultMap id="collectionMap" type="Student" extends="studentMap"> <collection property="teachers" ofType="Teacher"> <id property="id" column="teach_id" /> <result property="name" column="tname"/> <result property="gender" column="tgender" typeHandler="org.apache.ibatis.type.EnumOrdinalTypeHandler"/> <result property="subject" column="tsubject" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/> <result property="degree" column="tdegree" javaType="string" jdbcType="VARCHAR"/> </collection> </resultMap> <select id="selectStudents" resultMap="collectionMap"> SELECT s.id, s.name, s.gender, t.id teach_id, t.name tname, t.gender tgender, t.subject tsubject, t.degree tdegree FROM student s LEFT JOIN stu_teach_rel str ON s.id = str.stu_id LEFT JOIN teacher t ON t.id = str.teach_id </select> <!--能够经过设置useCache来规定这个sql是否开启缓存,ture是开启,false是关闭--> <select id="selectAllStudents" resultMap="studentMap" useCache="true"> SELECT id, name, age FROM student </select> <!--刷新二级缓存 <select id="selectAllStudents" resultMap="studentMap" flushCache="true"> SELECT id, name, age FROM student </select> --> </mapper>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--这个配置使全局的映射器(二级缓存)启用或禁用缓存--> <setting name="cacheEnabled" value="true" /> ..... </settings> .... </configuration>
package com.yihaomen.service.student; import com.yihaomen.mybatis.dao.StudentMapper; import com.yihaomen.mybatis.model.Student; import com.yihaomen.mybatis.model.Teacher; import com.yihaomen.service.BaseTest; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.util.List; /** * * @ProjectName: springmvc-mybatis */ public class TestStudent extends BaseTest { public static void selectAllStudent() { SqlSessionFactory sqlSessionFactory = getSession(); SqlSession session = sqlSessionFactory.openSession(); StudentMapper mapper = session.getMapper(StudentMapper.class); List<Student> list = mapper.selectAllStudents(); System.out.println(list); System.out.println("第二次执行"); List<Student> list2 = mapper.selectAllStudents(); System.out.println(list2); session.commit(); System.out.println("二级缓存观测点"); SqlSession session2 = sqlSessionFactory.openSession(); StudentMapper mapper2 = session2.getMapper(StudentMapper.class); List<Student> list3 = mapper2.selectAllStudents(); System.out.println(list3); System.out.println("第二次执行"); List<Student> list4 = mapper2.selectAllStudents(); System.out.println(list4); session2.commit(); } public static void main(String[] args) { selectAllStudent(); } }
结果:
[QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(98) | Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@51e0173d]
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | ==> Preparing: SELECT id, name, age FROM student
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | ==> Parameters:
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(139) | <== Total: 6
[Student{id='1', name='刘德华', age=55, gender=null, teachers=null}, Student{id='2', name='张惠妹', age=49, gender=null, teachers=null}, Student{id='3', name='谢霆锋', age=35, gender=null, teachers=null}, Student{id='4', name='王菲', age=47, gender=null, teachers=null}, Student{id='5', name='汪峰', age=48, gender=null, teachers=null}, Student{id='6', name='章子怡', age=36, gender=null, teachers=null}]
第二次执行
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.0
[Student{id='1', name='刘德华', age=55, gender=null, teachers=null}, Student{id='2', name='张惠妹', age=49, gender=null, teachers=null}, Student{id='3', name='谢霆锋', age=35, gender=null, teachers=null}, Student{id='4', name='王菲', age=47, gender=null, teachers=null}, Student{id='5', name='汪峰', age=48, gender=null, teachers=null}, Student{id='6', name='章子怡', age=36, gender=null, teachers=null}]
二级缓存观测点
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.3333333333333333
[Student{id='1', name='刘德华', age=55, gender=null, teachers=null}, Student{id='2', name='张惠妹', age=49, gender=null, teachers=null}, Student{id='3', name='谢霆锋', age=35, gender=null, teachers=null}, Student{id='4', name='王菲', age=47, gender=null, teachers=null}, Student{id='5', name='汪峰', age=48, gender=null, teachers=null}, Student{id='6', name='章子怡', age=36, gender=null, teachers=null}]
第二次执行
[QC] DEBUG [main] org.apache.ibatis.cache.decorators.LoggingCache.getObject(62) | Cache Hit Ratio [com.yihaomen.mybatis.dao.StudentMapper]: 0.5
[Student{id='1', name='刘德华', age=55, gender=null, teachers=null}, Student{id='2', name='张惠妹', age=49, gender=null, teachers=null}, Student{id='3', name='谢霆锋', age=35, gender=null, teachers=null}, Student{id='4', name='王菲', age=47, gender=null, teachers=null}, Student{id='5', name='汪峰', age=48, gender=null, teachers=null}, Student{id='6', name='章子怡', age=36, gender=null, teachers=null}]
Process finished with exit code 0
咱们能够从结果看到,sql只执行了一次,证实咱们的二级缓存生效了。
https://gitee.com/huayicompany/springmvc-mybatis
[1]杨开振 著,《深刻浅出MyBatis技术原理与实战》, 电子工业出版社,2016.09
[2]博客,http://blog.csdn.net/luanlouis/article/details/41280959
[3]博客,http://www.cnblogs.com/QQParadise/articles/5109633.html