一、引入依赖
<dependency>
<groupId>org.mybatis.caches</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.3</version>
<scope>test</scope>
</dependency>
二、配置文件:固定名字ehcache.xml
<?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:\ehcache"></diskStore>
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="10000000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
三、SQL映射文件,设置cache标签type
<cache type="org.mybatis.caches.ehcache.EhcacheCache">
四、创建logback.xml,名字是固定的
由于存在slf4j时,简易日志log4j将失效,需要借助slf4j的具体实现logback来打印日志,创建logback的配置文件logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration debug="true">
<!--日志输出位置-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!--日志输出格式-->
<!--时间、日志级别、线程名字、打印日志的类、日志主体内容 换行-->
<pattern>
[%d{HH:mm:ss.SSS} [%-5level] [%thread] [%logger] [%msg]%n
</pattern>
</encoder>
</appender>
<!--設置全局日志級別-->
<root level="DEBUG">
<!--指定打印日志的appender-->
<appender-ref ref="STDOUT"/>
</root>
<!--根据特殊需求,指定局部日志级别-->
<logger name="com.java.ssm.mapper" level="DEBUG"></logger>
</Configuration>
slf4j相当于提供接口,logback-classic相当于slf4j的一个实现类的jar