延迟加载==懒加载==按需加载:
- mybatis中,resultMap 标签的association标签(模型)和collection标签(集合)都具有延迟加载功能
配置:在全局配置文件中
-
<!--配置允许懒加载--> <settings> <setting name="lazyLoadingEnabled" value="true"/> //积极的懒加载,默认是true,设置为false时,懒加载生效 5 <setting name="aggressiveLazyLoading" value="false"/> </settings>
<!-- 懒加载 -->
<resultMap id="orderLazyloadingRslMap" type="orders">
<id column="id" property="id"/>
<result column="name" property="name"/>
<!--懒加载配置查询 association针对模型 + 执行方法 -->
<!--此处会进行懒加载,如果不用user则不会执行findUserById方法,
如果用到,则执行,并将column对应的列值作为user的属性传入findUserById方法参数 -->
<association property="user" select="com.zjc.mapper.UserMapper.findUserById" column="user_id"/>
</resultMap>
<select id="findOrderAndUserByLazyloading" resultMap="orderLazyloadingRslMap">
SELECT * FROM orders
</select>
缓存关系
一级缓存:默认开启 session级别
- 一级缓存指的是sqlsession,在sqlsession中有一个数据区域,是map结构,这个区域就是一级缓存区域
- key:sql语句、条件、statement等信息组成一个 唯一值
- value:查询出的结果对象
- 只有执行 增、 删、 改 操作才会清空缓存
二级缓存:手动配置 session工厂级别
配置:全局配置文件
-
<!--允许开启二级缓存--> <setting name="cacheEnabled" value="true"/>
-
mapper中配置缓存开启,默认是perpetualCache实现Cache接口 <cache></cache> <select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM user WHERE id = #{id} </select>
- 二级缓存指的是同一个namespace下的mapper,map结构
- 模型model要实现序列化接口 Serializable
- 执行 增、 删、 改 操作才会清空缓存
指定方法禁用二级缓存 useCache="false"
-
<select id="findUserById" parameterType="int" resultType="user" useCache="false"> SELECT * FROM user WHERE id = #{id} </select>
刷新缓存 flushCache
- 默认flushCache="true",插入,更新,删除,会清空二级缓存;
- 设置flushCache="false",插入,更新,删除,不会清空二级缓存
<insert id="save" parameterType="user" flushCache="false"> INSERT INTO user (username,sex,birthday,address) VALUE (#{username},#{sex},#{birthday},#{address}) </insert>
二级缓存应用场景:访问响应速度要求高,但实时性不高的查询
- cache标签中有一个flashInterval属性)来定时刷新二级缓存,单位为毫秒
整合 ehcache :分布式缓存框架
- 分布式缓存框架:redis 、menmcached、 ehcache
- 应用:对缓存数据集中管理,集群模式
原理:
- Cache缓存是一个接口,mybatis默认实现类是PerpetualCache。整合mybatis的二级缓存,那么用其他实现类实现Cache接口即可
步骤:
- 1.导入jar包 mybatis-ehcache-1.0.2.jar ehcache-core-2.6.5.jar
- 2.src下添加ehcache.xml配置文件
-
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> </ehcache>
- 3.指定mapper下配置
-
<!-- 配置Ehcached缓存,type不写,默认使用mybaits自带缓存:perpetualCache,此处改为ehcache缓存 --> <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
附录:ehcache配置说明
配置项 | 说明 |
maxElementsInMemory | 设置基于内存的缓存中可存放的对象最大数目 |
eternal | 设置对象是否为永久的,true表示永不过期 |
timeToIdleSeconds | 设置对象空闲最长时间,单位:秒;超过则对象过期,并从缓存中清除;值为0,表示对象可以无限期处于空闲状态 |
timeToLiveSeconds | 设置对象生存最长时间,超过则对象过期;值为0,表示对象可以无限期地存在于缓存中。该属性值必须大于或等于 timeToIdleSeconds 属性值 |
overflowToDisk | 设置基于内在的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中 |
diskPersistent | 当jvm结束时是否持久化对象 true false 默认是false |
diskExpiryThreadIntervalSeconds | 指定专门用于清除过期对象的监听线程的轮询时间 |
memoryStoreEvictionPolicy | 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出) |