SSM integration ehcache

  ehcache, mybatis, spring

1. Add dependencies. In addition to the packages required by SSM, the packages to be added are mybatis-ehcache.jar, ehcache-spring-annotations.jar; Maven coordinates are as follows:

<!--Mybatis-ehcache-->
<dependency>
    <groupId>org.mybatis.caches</groupId>
    <artifactId>mybatis-ehcache</artifactId>
    <version>1.0.3</version>
</dependency>

<!--ehcache-spring-annotations-->
<dependency>
    <groupId>com.googlecode.ehcache-spring-annotations</groupId>
    <artifactId>ehcache-spring-annotations</artifactId>
    <version>1.2.0</version>
</dependency>

2.echache profile, as follows:

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxElementsInMemory="3000"
            eternal="false"
            timeToIdleSeconds="3600"
            timeToLiveSeconds="3600"
            overflowToDisk="true"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="100"
            memoryStoreEvictionPolicy="LRU"/>
</ehcache>

applicationContext-echache.xml


<?xml version="1.0" encoding="UTF-8"?>
<!-- /** * * 缓存配置 *  * */ -->
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<ehcache:annotation-driven cache-manager="ehCacheManager" />

<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache/ehcache.xml" />
    <property name="shared" value="true"/>
</bean>
</beans>

3. load echache configuration file into spring-mvc.xml


<!--加载ehcache缓存-->
<import resource="classpath:ehcache/applicationContext-ehcache.xml"/>

4. configure in mybatis mapper.xml, such as SysUserMapper.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="com.springmvc.mapper.system.SysUserMapper">
    <!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 -->
    <cache type="org.mybatis.caches.ehcache.EhcacheCache" />
    <!--<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>-->
</mapper>