温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis Cache配置

发布时间:2020-08-06 23:53:15 来源:网络 阅读:676 作者:zsdnr 栏目:网络安全

MyBatis提供了一级缓存和二级缓存

配置

全局配置

配置说明默认值可选值
cacheEnabled全局缓存的开关truetrue false
localCacheScope本地缓存,SESSION表示执行的sql结果缓存数据可以在同一个sqlSession共享,
而STATEMENT,则同只有在单条语句会被缓存,
两条语句不能共享缓存数据
SESSIONSESSION STATEMENT
    <!-- 默认值 --><setting name="cacheEnabled" value="true"/><setting name="cacheEnabled" value="SESSION"/>

Mapper配置

flushCache=true表示该语句的执行结果,会清空本地缓存以及2级缓存
useCache="true"表示该语句的执行结果,会被缓存到到2级缓存
默认值:
<select flushCache="false" useCache="true">
<insert/update/delete flushCache="true">

<cache>:当前Mapper的缓存配置,二级缓存
<cache-ref>:cache只对特定的Namespace使用,即每个namespace使用一个cache实例,如果要多个namespace使用同一个cache实例,则可以使用cache-ref来引用

<cache blocking="" eviction="" flushInterval="" readOnly="" size="" type="">
    <property name="" value=""/>
</cache>
<cache-ref namespace=""/>
cache配置
属性说明默认值可选值
eviction回收内存策略LRULRU FIFO SOFT WEAK
flushInterval刷新间隔没设置大于0 (单位:ms)
size缓存对象的数量1024大于0
readOnly如果为true会返回所有调用者同一个实例,尽管提高了性能,
但是需要程序保证实例对象不被修改,如果为false,
则为读写缓存,会通过序列化返回缓存对象的一份Copy,
较慢,但是比较安全
falsetrue false
type可以指定自定义缓存,但是该类必须实现
org.apache.ibatis.cache.Cache接口

com....class
自定义缓存
<!-- 该属性会调用setCacheFile方法(setter),将属性值注入 --><cache type="com.domain.something.MyCustomCache">
    <property name="cacheFile" value="/tmp/my-custom-cache.tmp"/></cache>

二级缓存整体管理结构:

MapperA.xml

<mapper namespace="com.jabnih.demo.mapper.MapperA">
    <cache /></mapper>

MapperB.xml

<mapper namespace="com.jabnih.demo.mapper.MapperB">
    <cache-ref namespace="com.jabnih.demo.mapper.MapperA"/>
</mapper>

MapperC.xml

<mapper namespace="com.jabnih.demo.mapper.MapperC">
    <cache /></mapper>

如下:
MyBatis Cache配置


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI