1 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloudtech</groupId>
<artifactId>01-springboot-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- junit jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- mybatis启动器 -->
<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>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- 增加thymeleaf坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring Boot缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache坐标 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!--通用Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
<!--pageHelper分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
</project>
2 创建ehcache文件
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<!--defaultCache:echcache的默认缓存策略 -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 自定义缓存策略 -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
3.application.yml
server:
port: 8082
spring:
datasource:
# 数据源基本配置
url: jdbc:mysql://120.78.150.135:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
cache: ##配置 ehcache缓存
ehcache:
config: ehcache.xml
#mybatis相关配置
mybatis:
#当mybatis的xml文件和mapper接口不在相同包下时
#需要用mapperLocations属性指定xml文件的路径。
#*是个通配符,代表所有的文件,**代表所有目录下
mapper-locations: classpath*:/com/cloudtech/mapper/*.xml
#指定bean所在包
#在mapper.xml中可以使用别名而不使用类的全路径名
type-aliases-package: com.cloudtech.entity
#通用mapper配置
mapper:
identity: MYSQL # 取主键的方式
before: false # 主键递增
not-empty: true # 按主键插入或更新时,是否判断字符串 != ''
style: camelhumpandlowercase # 实体类与表中字段的映射方式:驼峰转带下划线的小写格式
wrap-keyword: '{0}' # 自动配置关键字,配置后不需要使用 @Column 指定别名
safe-delete: true # 删除时必须设置查询条件
safe-update: true # 更新时必须设置查询条件
use-java-type: true # 是否映射Java基本数据类型
mappers: tk.mybatis.mapper.common.Mapper
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
4.修改启动类
package com.cloudtech;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import tk.mybatis.spring.annotation.MapperScan;
/**
*
* @ClassName: App2
* @Description:
* @author wude
* @date 2018年12月11日
*
*/
@SpringBootApplication
@ServletComponentScan
@MapperScan(basePackages = "com.cloudtech.dao")
@EnableCaching //启动缓存
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
}
5.代码
package com.cloudtech.dao;
import com.cloudtech.entity.Role;
import tk.mybatis.mapper.common.Mapper;
public interface RoleMapper extends Mapper<Role>{
}
RoleService.java
package com.cloudtech.service;
import com.cloudtech.entity.Role;
public interface RoleService {
/**
* 根据id查询角色信息
* @param id
* @return
*/
public Role findRoleById(Integer id);
}
RoleServiceImpl.java
package com.cloudtech.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.cloudtech.dao.RoleMapper;
import com.cloudtech.entity.Role;
import com.cloudtech.service.RoleService;
@Service
public class RoleServiceImpl implements RoleService{
@Autowired
private RoleMapper roleMapper;
@Cacheable(value="users")
@Override
public Role findRoleById(Integer id) {
return roleMapper.selectByPrimaryKey(id);
}
}
注意:@Cacheable(value="users") users是ehcache.xml中自定义的name的名字
单元测试类
package com.cloudtech.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cloudtech.App2;
import com.cloudtech.dao.RoleMapper;
import com.cloudtech.entity.Role;
import com.cloudtech.service.RoleService;
/**
* spring 测试类
* @RunWith:启动器
* SpringJUnit4ClassRunner 让junit和spring环境进行整合
* @SpringBootTest 1.当前类为spring的测试类 2加载spring boot启动类,启动spring boots
*
* @ClassName: RoleTest
* @Description:
* @author wude
* @date 2018年12月12日
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={App2.class})
public class RoleTest {
@Autowired
RoleService roleService;
@Test
public void testRoleById(){
System.out.println("第一次查询:"+roleService.findRoleById(22));
System.out.println("第二次查询:"+roleService.findRoleById(22));
}
}
效果图
打印一次说明缓存配置成功
注意:Role对象需要序列化