`

mybatis3添加Ehcache缓存

阅读更多

 

总结:无论是采用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缓存支持

    在MyBatis 中添加Ehcache 缓存支持,首先需要确保项目采用Maven作为构建工具,因为我们需要在`pom.xml`文件中添加Ehcache的相关依赖。这一步骤包括查找并引入Ehcache的Maven坐标,通常为以下格式: ```xml ...

    Mybatis-ehcache 1.2.1源码(ehcache-cache-mybatis-ehcache-1.2.1.zip)

    3. **缓存操作**:Mybatis-ehcache如何进行缓存的读取、写入和更新操作。在执行SQL后,是如何将结果存入Ehcache的,以及在后续的查询中,如何检查Ehcache并返回缓存的结果。 4. **Key生成策略**:关键在于如何生成...

    Mybatis入门实例(二)——添加ehcache缓存支持

    在本篇《Mybatis入门实例(二)——添加ehcache缓存支持》中,我们将深入探讨如何在Mybatis框架中集成Ehcache作为二级缓存,以提高数据访问的效率和性能。Ehcache是一个开源的Java分布式缓存,广泛用于缓存应用程序中...

    Mybatis-ehcache 1.2.1源码(ehcache-cache-mybatis-ehcache-1.2.1.tar

    1. **配置集成**:在 Mybatis 的配置文件中,我们需要添加 Ehcache 的配置,包括指定缓存驱动、设置缓存区域等信息。 2. **Cache 接口实现**:Ehcache 实现了 Mybatis 的 Cache 接口,提供了缓存的生命周期管理和...

    springboot+mybatis+ehcache实现缓存数据

    在本文中,我们将深入探讨如何使用SpringBoot、MyBatis和Ehcache来实现缓存数据。Ehcache是一款高效且易于使用的Java内存缓存框架,对于提升应用程序性能,尤其是在处理大量数据时,能起到显著作用。虽然在分布式...

    mybatis-ehcache-1.0.3.zip

    MyBatis-Ehcache整合包是MyBatis框架与Ehcache缓存系统结合的版本,主要用于提升数据访问效率,减少数据库的负载。Ehcache是一个广泛使用的开源Java分布式缓存,它支持内存和磁盘存储,具有高性能、易用性以及可扩展...

    mybatis-ehcache-1.0.3.rar

    MyBatis-Ehcache是一个集成框架,用于将流行的缓存解决方案Ehcache与MyBatis持久层框架结合使用。这个`mybatis-ehcache-1.0.3.rar`压缩包包含了MyBatis-Ehcache的1.0.3版本,提供了一个便捷的方式来在MyBatis应用...

    mybatis-ehcache-1.0.2.jar

    MyBatis Ehcache 1.0.2 是一个专门为MyBatis框架集成Ehcache缓存功能的库。在Java开发中,缓存是提升应用性能的重要手段,它能够减少数据库的访问频率,提高数据读取速度。Ehcache是一款广泛使用的开源Java分布式...

    mybatis ehcache 1.0 ehcache.xsd 提示文件

    MyBatis,作为一个流行的Java持久层框架,提供了二级缓存功能,允许开发者选择不同的缓存实现,其中Ehcache是一个常用的选择。本篇文章将详细探讨MyBatis与Ehcache的集成以及`ehcache.xsd`和`ehcache.xml`这两个配置...

    mybatis-ehcache

    1. 添加Ehcache和mybatis-ehcache的依赖到项目中。 2. 配置MyBatis的配置文件(`mybatis-config.xml`),启用二级缓存并指定使用Ehcache。 ```xml ... &lt;id&gt;com.example.mapper.YourMapper&lt;/id&gt; &lt;type&gt;org....

    mybatis整合ehcache的jar包+配置文件.zip

    这样,MyBatis就会为这些方法启用Ehcache缓存: ```java @CacheNamespace(implementation = org.mybatis.caches.ehcache.EhcacheCache.class) public interface UserMapper { // ...查询方法... } ``` 5. **...

    spring springmvc mybatis shiro 以及 ehcache(配合shiro实现缓存验证 配合spring实现二级缓存)

    spring springmvc mybatis shiro 以及 ehcache(配合shiro实现缓存验证 配合spring实现二级缓存) 测试 二级缓存 访问http://localhost:8080/vkblog/test/ehcacheuserlist.action 测试 访问限制 访问任意的action

    springMybatis+redis三级缓存框架

    3. **缓存雪崩**:多个缓存同时过期可能导致大量请求涌向数据库,设置合理的缓存过期时间以及采用随机过期策略可以避免这种情况。 4. **缓存击穿**:热点数据的缓存失效,导致数据库压力骤增。可以使用互斥锁(如...

    mybatis-ehcache.rar

    ehcache缓存的技术包和mybatis 中间包 ehcache-core-2.6.8.jar mybatis-ehcache-1.0.3.jar ....................

    java ehcache core 2.6.8.jar 核心包和mybatis-ehcache-1.0.3.jar分享

    3. 集成MyBatis:在MyBatis的配置文件中启用二级缓存,并指定使用Ehcache。 4. 编写Mapper:在Mapper接口和XML文件中,使用MyBatis的注解或元素来启用缓存。 5. 测试:运行应用,检查是否能正确缓存查询结果,以及...

    springMVC+mybatis+ehcache整合

    SpringMVC、MyBatis和Ehcache的整合提供了一种优秀的解决方案,这三种技术分别处理了不同的层面:SpringMVC作为MVC框架负责控制层,MyBatis作为持久层框架简化了数据库操作,而Ehcache则作为缓存系统提高了数据访问...

Global site tag (gtag.js) - Google Analytics