1, 项目实在 springboot 集成 mybatis 的基础上的:
https://www.cnblogs.com/pickKnow/p/11189729.html https://www.cnblogs.com/pickKnow/p/11189729.html
2,pom 如下, 有的不需要加, 只需要加下 ehcache 相关就行
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.0.RELEASE</version>
- </parent>
- <dependencies>
- <!-- SpringBoot 对 lombok 支持 -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
- <!-- SpringBoot web 核心组件 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-Web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- </dependency>
- <!-- SpringBoot 外部 tomcat 支持 -->
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- </dependency>
- <!-- springboot-log4j -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-log4j</artifactId>
- <version>1.3.8.RELEASE</version>
- </dependency>
- <!-- springboot-aop 技术 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId>
- </dependency>
- <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- </dependency>
- <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- <version>1.2.47</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- </dependency>
- <dependency>
- <groupId>taglibs</groupId>
- <artifactId>standard</artifactId>
- <version>1.1.2</version>
- </dependency>
- <!-- 开启 cache 缓存 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-cache</artifactId>
- </dependency>
- <!-- ehcache 缓存 -->
- <dependency>
- <groupId.NET.sf.ehcache</groupId>
- <artifactId>ehcache</artifactId>
- <version>2.9.1</version><!--$NO-MVN-MAN-VER$ -->
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.1.1</version>
- </dependency>
- <!-- mysql 依赖 -->
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- </dependency>
- </dependencies>
3, 需要缓存的方法上加上注解
@Cacheable
在类上给缓存加上名字
- @CacheConfig(cacheNames = "userCache")
- package com.aiyuesheng.mapper;
- import org.springframework.cache.annotation.CacheConfig;
- import org.springframework.cache.annotation.Cacheable;
- import com.aiyuesheng.entity.User;
- @CacheConfig(cacheNames = "userCache")
- public interface UserMapperdb {
- void insertUser(User user);
- @Cacheable
- User selectUserById(int id);
- }
4, 启动类上加上读取缓存
- @EnableCaching
- @MapperScan("com.aiyuesheng.mapper")
- @SpringBootApplication
- public class App {
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- }
5, 设置了缓存的名字 userCache, 那么这个缓存的参数在哪里?
在配置文件: 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">
- <!-- 指定一个文件目录, 当 EhCache 把数据写到硬盘上时, 将把数据写到这个文件目录下 -->
- <diskStore path="java.io.tmpdir" />
- <!-- userCache 缓存 -->
- <cache name="userCache" maxElementsInMemory="1000" eternal="false"
- timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
- diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
- diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU">
- </cache>
- </ehcache>
6, 在 application.YAML 定义去读取这个文件
- # 单数据源
- spring:
- datasource:
- url: jdbc:MySQL://127.0.0.1:3306/testdb
- username: root
- password: 123456
- driver-class-name: com.MySQL.jdbc.Driver
- mybatis:
- mapper-locations: classpath:com/aiyuesheng/mapping/*Mapper.xml
- type-aliases-package: com.aiyuesheng.entity
- # 缓存配置读取
- cache:
- type: ehcache
- ehcache:
config: classpath:ehcache.xml
7, 配置 OK , 缓存里面有数据, 他就会读取缓存里面的值.
8, 配置信息解读
<diskStore path="java.io.tmpdir" /> 指定数据 (.data and .index) 存储位置, 可指定磁盘中的文件夹位置期
<cache name="userCache" Cache 的名称, 必须是唯一的(ehcache 会把这个 cache 放到 HashMap 里).
maxElementsInMemory="1000" 在内存中缓存的 element 的最大数目
eternal="false" 设定缓存的 elements 是否永远不过期. 如果为 true, 则缓存的数据始终有效, 如果为 false 那么还要根据 timeToIdleSeconds,timeToLiveSeconds 判断.
timeToIdleSeconds="120" timeToLiveSeconds="120"
overflowToDisk="true" 如果内存中数据超过内存限制, 是否要缓存到磁盘上.
diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" 否在磁盘上持久化. 指重启 jvm 后, 数据是否有效. 默认为 false.
diskExpiryThreadIntervalSeconds="120" 对象检测线程运行时间间隔. 标识对象状态的线程多长时间运行一次.
memoryStoreEvictionPolicy="LRU"> 缓存策略. 默认值 LRU, 可选 FIFO,LFU.
来源: http://www.bubuko.com/infodetail-3141904.html