几个注意事项。
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();}
}