MyBatis 整合Ehcach 缓存1. 导入第三方缓存jar包2. 添加Ehcache核心配置文件(ehcache.xml) 或者 在Mapper中配置缓存数据3. 需要使用Ehcache缓存的 Mapper 文件中指定Ehcache缓存。
MyBatis 整合Ehcach 缓存
整合步骤:
1. 导入第三方缓存jar包
ehcache-core-x.x.x.jar
slf4j-api-x.x.x.jar
slf4j-log4j12-x.x.x.jar
mybatis-ehcache-x.x.x.jar
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
<version>5.1.38
</version>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis
</artifactId>
<version>3.4.6
</version>
</dependency>
<dependency>
<groupId>log4j
</groupId>
<artifactId>log4j
</artifactId>
<version>1.2.17
</version>
</dependency>
<dependency>
<groupId>org.slf4j
</groupId>
<artifactId>slf4j-api
</artifactId>
<version>1.7.25
</version>
</dependency>
<dependency>
<groupId>org.slf4j
</groupId>
<artifactId>slf4j-log4j12
</artifactId>
<version>1.7.25
</version>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis-spring
</artifactId>
<version>1.3.2
</version>
</dependency>
<dependency>
<groupId>org.mybatis
</groupId>
<artifactId>mybatis-ehcache
</artifactId>
<version>1.0.0
</version>
</dependency>
2. 添加Ehcache核心配置文件(ehcache.xml) 或者 在Mapper中配置缓存数据
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="D:\MyBatisCache\EhcacheCache" />
<defaultCache
maxElementsInMemory="100000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
3. 需要使用Ehcache缓存的 Mapper 文件中指定Ehcache缓存。
<cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>
<cache-ref namespace="com.javaee.mybatis.mapper.EmployeeMapper"/>