总结:无论是采用mybatis 自身的cache 还是三方的cache , 这样的配置,就是对 所有的select 语句都全局缓存
为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方案,在此我们主要是做查询缓存,提高查询的效率.
Ehcache缓存:
加入ehcache.xml、ehcache-2.10.0.jar(net.sf) 、mybatis-ehcache-1.0.3.jar
Maper中加入:
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
说明: cache的名称: Cache Hit Ratio [cn.mapper.UserMapper] ,与namespace相同
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 默认的磁盘缓存目录 -->
<diskStore path="java.io.tmpdir"/>
<!-- EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力 -->
<!-- 默认缓存策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120" />
<!-- 用于整合Hibernate二级缓存:缓存领域对象
<cache name="cn.domain.User"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600" />
-->
</ehcache>
UserMapper.xml
<?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="cn.mapper.UserMapper" >
<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
<!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache" /> -->
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
</mapper>
<settings>
<!-- 全局的映射器启用或禁用缓存。 -->
<setting name="cacheEnabled" value="true" />
</settings>
/**使用了查询缓存,只查询了一次数据库*/
static void testEhcache(UserService userService){
testPage(userService);
testPage(userService);
}
/*使用cn.domain.User cache 缓存对象*/
static void testSpringEhcache(ApplicationContext app, UserService userService){
CacheManager cm=(CacheManager)app.getBean("ehCacheManager");
Cache cache=cm.getCache("cn.domain.User");
User user=userService.selectById("100");
Element e=new Element(user.getUserId(), user);
cache.put(e);
User u = (User)cache.get("100").getObjectValue();
System.out.println(user);
}
<cache name="cn.domain.User"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600" />
<!-- Ehcache与spring整合 -->
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
相关推荐
mybatis添加ehcache缓存支持,基于maven项目管理
ehcache是一个快速内存缓存框架,java项目里用起来很方便,本来想实现分布式缓存的,但是实践下来redis更适合分布式,所以这里面把分布缓存的配置注释掉了
spring springmvc mybatis shiro 以及 ehcache(配合shiro实现缓存验证 配合spring实现二级缓存) 测试 二级缓存 访问http://localhost:8080/vkblog/test/ehcacheuserlist.action 测试 访问限制 访问任意的action
ehcache缓存的技术包和mybatis 中间包 ehcache-core-2.6.8.jar mybatis-ehcache-1.0.3.jar ....................
mybatis开发,配置ehcache缓存,shior框架认证需要配置
NULL 博文链接:https://qiuqiu0034.iteye.com/blog/1162952
mybatis 第三方二级缓存,ehcache 配置文件的 约束文件 ehcache.xsd !!
mybatis-ehcache-1.0.3.jar mybatis开发使用缓存
mybatis持久层与ehcache缓存框架中的经典案例,还不错
mybatis缓存, mybatis的缓存EhCache,mybatis工具,mybatis工具类
缓存,其存储作用域为 Session,当 Session flush 或 close ...3. 对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear
3.Ehcache方法缓存及页面缓存。见applicationContext-cache.xml及web.xml的pageEhCacheFilter 4.MyBatis+Maven代码生成工具。见bin目录 5.作为项目或者技术研究的基础架构。必要的jar包依赖都已配置好,基本都采用较...
mybatis-ehcache.jar 各个版本,免费下载。 mybatis 第三方缓存整合适配包,各个版本,免费下载。
mybatis-ehcache.jar 各个版本,免费下载。 mybatis 第三方缓存整合适配包,各个版本,免费下载。
mybatis-ehcache.jar 各个版本,免费下载。 mybatis 第三方缓存整合适配包,各个版本,免费下载。
EHCache缓存的说明文档是到处找来的内容,都有参考链接指向原地址。有三个测试项目也在里面:一个整合了Struts2或Hibernate,一个整合了MyBatis3,这两个是我做的;另一个ehcachetest是下载了別人的。
MyBatis整合Ehcache第三方缓存。