1. pom.xml
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
</dependency>
2. MyBatis 각 mapper 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.tachyon.api.repository.NewsRepository">
<!--
MyBatis ehcache 설정
- http://mybatis.org/ehcache-cache/
-->
<cache type="org.mybatis.caches.ehcache.EhcacheCache">
<!-- <cache type="org.mybatis.caches.ehcache.EhBlockingCache"> --><!-- blocking cache를 사용할 경우 -->
<property name="timeToIdleSeconds" value="3600"/><!--1 hour-->
<property name="timeToLiveSeconds" value="3600"/><!--1 hour-->
<property name="maxEntriesLocalHeap" value="1000"/>
<property name="maxEntriesLocalDisk" value="10000000"/>
<property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>
<!-- 기본 설정을 따를 경우 이렇게 간단히 선언해도 됨 -->
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->
<!--
각 statement 에 cache 적용 방법
- 일단 xml에 cache가 선언되면 해당 xml 내의 모든 select statement에 cache가 적용된다.
- 캐시를 적용하고 싶지 않은 statement 에 대해서는 엘리먼트 속성으로 useCache="false" 를 추가적으로 기입해 준다.
(e.g. <select id="xxx" useCache="false")
-->
<select id="findUserList" resultType="LHMap">
select *
from jhi_user
</select>
</mapper>
* 참고
- MyBatis EHCache 를 필요에 따라 statement 별로 제거하기
- 캐시를 사용하지 않는 것을 기본값으로 하는 설정도 있는 것 같은데 찾아보자.