mybatis一、二级缓存,以及使用ehcache替代mysql自身的二级缓存

几个注意事项。

1、mybatis自身带一、二级缓存。

一级缓存存在与session中,使用同一个session的查询语句,会自动将结果存入一级缓存。默认一级缓存是打开的。相关闭一级缓存,在mybatis-config.xml中设置

<setting name="localCacheScope" value="STATEMENT" />

2、mybatis自身带的二级缓存,总开关在mybatis-config.xml中,默认是打开的。若需要关闭,则需要设置:

<setting name="cacheEnabled" value="false" />

各个查询的二级缓存是否起作用,需要到xxxMapper.xml中打开。

在xxxMapper.xml中的namespace下,添加

<cache /> 即可。

需要使用ehcache代替mybatis自身的二级缓存,只需要将上述的<cache />替换为

<!--以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志-->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache" />
<cache type="org.mybatis.caches.ehcache.EhcacheCache" />

3、以上二级缓存需要使用的时候,需要将实体类设置为:

public class User implements Serializable {
    private Integer id;
    private String userName;
    private String password;
    public Integer getId() {return id;}
    public void setId(Integer id) {this.id = id;}
    public String getUserName() {return userName;}
    public void setUserName(String userName) {this.userName = userName == null ? null : userName.trim();}
    public String getPassword() {return password;}
    public void setPassword(String password) {this.password = password == null ? null : password.trim();}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值