一 创建项目(23-spring-boot-ehcache:复制22-spring-boot-jpa)
二 修改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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<groupId>com.bjsxt</groupId>
<artifactId>23-spring-boot-ehcache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- springboot的web启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot的thymeleaf启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.13</version>
</dependency>
<!-- 添加junit相关jar包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</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>
</dependencies>
</project>
三 创建Ehcache的配置文件(ehcache.xml[src/main/resources/ehcache.xml])
复制如下文件内容到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>
<!-- 自定义缓存策略,name不能重复 -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
四 修改application.properties文件,添加spring.cache.ehcache.config=ehcache.xml配置
#配置数据库相关信息
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
#配置数据库连接池信息
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#jpa相关配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#配置ehcache配置
#这样写不行,或报异常spring.cache.ehcache.config=ehcache.xml
spring.cache.ehcache.config=classpath:ehcache.xml
五 修改启动类,添加@EnableCaching注解
package com.bjsxt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
六 编写业务层
package com.bjsxt.service;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.bjsxt.pojo.Users;
public interface UsersService {
List<Users> findUserAll();
Users findUserById(Integer id);
Page<Users> findUserByPage(Pageable pageable);
void saveUsers(Users user);
}
package com.bjsxt.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.bjsxt.dao.UsersRepository;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
@Service
public class UsersServiceImpl implements UsersService{
@Autowired
private UsersRepository usersRepository;
@Override
public List<Users> findUserAll() {
return usersRepository.findAll();
}
@Override
// @Cacheable:对当前查询的对象做缓存处理 value指定ehcache.xml中的哪个配置
@Cacheable(value="users")
public Users findUserById(Integer id) {
return usersRepository.findById(id).get();
}
@Override
public Page<Users> findUserByPage(Pageable pageable) {
return usersRepository.findAll(pageable);
}
@Override
public void saveUsers(Users user) {
usersRepository.save(user);
}
}
七 编写测试类
package com.bjsxt.service;
import static org.junit.Assert.fail;
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.bjsxt.App;
import com.bjsxt.pojo.Users;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=App.class)
public class UsersServiceTest {
@Autowired
private UsersService usersService;
@Test
public void testFindUserAll() {
fail("Not yet implemented");
}
/**
2019-04-12 17:58:52.047 INFO 10904 --- [ main] com.bjsxt.service.UsersServiceTest : Started UsersServiceTest in 15.43 seconds (JVM running for 18.177)
Hibernate: select users0_.id as id1_3_0_, users0_.address as address2_3_0_, users0_.age as age3_3_0_, users0_.name as name4_3_0_, users0_.role_id as role_id5_3_0_, roles1_.role_id as role_id1_1_1_, roles1_.role_name as role_nam2_1_1_, menus2_.role_id as role_id1_2_2_, menus3_.menus_id as menus_id2_2_2_, menus3_.menus_id as menus_id1_0_3_, menus3_.father_id as father_i2_0_3_, menus3_.menus_name as menus_na3_0_3_, menus3_.menus_url as menus_ur4_0_3_ from t_users users0_ left outer join t_roles roles1_ on users0_.role_id=roles1_.role_id left outer join t_roles_menus menus2_ on roles1_.role_id=menus2_.role_id left outer join t_menus menus3_ on menus2_.menus_id=menus3_.menus_id where users0_.id=?
Users [id=1, name=guozi44, age=18, address=地球]
Hibernate: select users0_.id as id1_3_0_, users0_.address as address2_3_0_, users0_.age as age3_3_0_, users0_.name as name4_3_0_, users0_.role_id as role_id5_3_0_, roles1_.role_id as role_id1_1_1_, roles1_.role_name as role_nam2_1_1_, menus2_.role_id as role_id1_2_2_, menus3_.menus_id as menus_id2_2_2_, menus3_.menus_id as menus_id1_0_3_, menus3_.father_id as father_i2_0_3_, menus3_.menus_name as menus_na3_0_3_, menus3_.menus_url as menus_ur4_0_3_ from t_users users0_ left outer join t_roles roles1_ on users0_.role_id=roles1_.role_id left outer join t_roles_menus menus2_ on roles1_.role_id=menus2_.role_id left outer join t_menus menus3_ on menus2_.menus_id=menus3_.menus_id where users0_.id=?
Users [id=1, name=guozi44, age=18, address=地球]
2019-04-12 17:58:52.573 INFO 10904 --- [ Thread-4] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
从以上结果可看出,会执行两次sql
解决:在UsersServiceImpl.findUserById方法添加@Cacheable(value="users")注解
二次改后:
Hibernate: select users0_.id as id1_3_0_, users0_.address as address2_3_0_, users0_.age as age3_3_0_, users0_.name as name4_3_0_, users0_.role_id as role_id5_3_0_, roles1_.role_id as role_id1_1_1_, roles1_.role_name as role_nam2_1_1_, menus2_.role_id as role_id1_2_2_, menus3_.menus_id as menus_id2_2_2_, menus3_.menus_id as menus_id1_0_3_, menus3_.father_id as father_i2_0_3_, menus3_.menus_name as menus_na3_0_3_, menus3_.menus_url as menus_ur4_0_3_ from t_users users0_ left outer join t_roles roles1_ on users0_.role_id=roles1_.role_id left outer join t_roles_menus menus2_ on roles1_.role_id=menus2_.role_id left outer join t_menus menus3_ on menus2_.menus_id=menus3_.menus_id where users0_.id=?
Users [id=1, name=guozi44, age=18, address=地球]
Users [id=1, name=guozi44, age=18, address=地球]
只会查一次,两次输出结果一致,但有异常,异常信息为:java.io.NotSerializableException: com.bjsxt.pojo.Users
解决:Users实现Serializable接口即可
*/
@Test
public void testFindUserById() {
// 第一次查询
Users users = usersService.findUserById(1);
System.out.println(users);
// 第二次查询
Users users2 = usersService.findUserById(1);
System.out.println(users2);
}
@Test
public void testFindUserByPage() {
fail("Not yet implemented");
}
@Test
public void testSaveUsers() {
fail("Not yet implemented");
}
}