mybatis注解配置缓存的方法

xiaoxiao2021-02-28  117

mapper文件

package it.com.db; import java.util.List; import java.util.Map; import org.apache.ibatis.cache.decorators.LruCache; import com.mysql.jdbc.util.LRUCache; import org.apache.ibatis.annotations.CacheNamespace; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; @CacheNamespace( size=100,eviction=LruCache.class,implementation=org.mybatis.caches.ehcache.EhcacheCache.class) public interface IuserMapper { @Select("select * from username") @Options(useCache=true) public List<Map> findAll(); }

conf文件

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings>    <setting name="cacheEnabled" value="true"></setting>      </settings>     <environments default="development">         <environment id="development">             <transactionManager type="JDBC" />             <!-- 配置数据库连接信息 -->             <dataSource type="POOLED">                 <property name="driver" value="com.mysql.jdbc.Driver" />                 <property name="url" value="jdbc:mysql://localhost:3306/luntan" />                 <property name="username" value="root" />                 <property name="password" value="mysql" />             </dataSource>         </environment>     </environments>     <mappers>        <mapper class="it.com.db.IuserMapper"/>           </mappers>      </configuration>

ehcache.xml文件 <ehcache> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> </ehcache>

log4j.properties 文件

### ÉèÖÃLoggerÊä³ö¼¶±ðºÍÊä³öÄ¿µÄµØ ### log4j.rootLogger = all, stdout,logfile ### °ÑÈÕÖ¾ÐÅÏ¢Êä³öµ½¿ØÖÆÌ¨ ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout ### °ÑÈÕÖ¾ÐÅÏ¢Êä³öµ½Îļþ£ºjbit.log ### log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=d\:\\tt.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%l %F %p %m%n

IuserMapper文件  

package it.com.db; import java.util.List; import java.util.Map; import org.apache.ibatis.cache.decorators.LruCache; import com.mysql.jdbc.util.LRUCache; import org.apache.ibatis.annotations.CacheNamespace; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; @CacheNamespace( size=100,eviction=LruCache.class,implementation=org.mybatis.caches.ehcache.EhcacheCache.class) public interface IuserMapper { @Select("select * from username") @Options(useCache=true) public List<Map> findAll(); }

转载请注明原文地址: https://www.6miu.com/read-31834.html

最新回复(0)