cache内容
缓存的意义:将方法的运行结果进行缓存,以后再要相同的数据,直接从缓存中获取,不用调用方法;
cacheManager管理多个Cache组件,对缓存的真正操作CRUD操作在Cache组件中,每一个缓存组件有自己唯一一个名字;
几个属性:
-
cacheNames/value:指定缓存的名字;
-
key:缓存数据使用的key:可以用它来指定。默认是使用方法参数的值 1-方法的返回值
编写SpEL: #id:参数id的值 #a0 #p0 #root.args[0] -
keyGenerator: key的生成器;可以自己指定key的生成器的组件id
-
cacheManager:指定缓存管理器;或者cacheResolver指定缓存解析器
-
condition:指定符合条件的情况下缓存;condition = “#id>1"
-
unless:否定缓存;当unless指定的条件为true,方法的返回值就不会缓存,可以获取到结果进行判断; unless = “#result = null”
@Cacheable(cacheNames = {"emp"},key="root.id")
public Employee getEmpById(Integer id){
System.out.println("查询"+id+"号员工");
Employee employee = employeeMapper.getEmpById(id);
return employee;
}
@CachePut:
// @CachePut:既调用方法,又更新缓存数据;同步更新缓存
// 修改了数据库的某个数据,同时更新缓存
// 运行:
// 1.先调用目标方法
// 2.将目标方法的结果缓存起来
@CachePut(key= "#result.id")
public Employee updateEmp(Employee employee){
System.out.println("updateEmp"+employee);
employeeMapper.updateEmpById(employee);
return employee;
}
@CacheEvict
// @CacheEvict:缓存清除
// key:指定要清除的数据
// allEntries = true:指定清除这个缓存中的所有数据
// beforeInvocation=true 代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除
@CacheEvict
public void deleteEmp(Integer id){
System.out.println("deleteEmp:"+id);
int i = 101/0;
employeeMapper.deleteEmpById(id);
}
搭建基本环境
- 导入数据文件 创建出department和employee表
- 创建Javabean封装数据
- 整合MyBatis操作数据库 配置数据源信息
使用步骤
- 导入模块
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
- 在主函数中开启缓存模式 @EnableCaching
@EnableCaching
@SpringBootApplication
public class SpringbootCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCacheApplication.class, args);
}
}
- 使用缓存注解
@CacheConfig(cacheNames = "emp")
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
@Cacheable(cacheNames = {"emp"})
public Employee getEmpById(Integer id){
System.out.println("查询"+id+"号员工");
Employee employee = employeeMapper.getEmpById(id);
return employee;
}
}
具体代码
1.创建数据库表
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`departmentName` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`gender` int(2) DEFAULT NULL,
`d_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.创建bean文件
public class Department {
private Integer id;
private String departmentName;
public Department() {
super();
// TODO Auto-generated constructor stub
}
public Department(Integer id, String departmentName) {
super();
this.id = id;
this.departmentName = departmentName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName + "]";
}
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender; //性别 1男 0女
private Integer dId;
public Employee() {
super();
}
public Employee(Integer id, String lastName, String email, Integer gender, Integer dId) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.dId = dId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Integer getdId() {
return dId;
}
public void setdId(Integer dId) {
this.dId = dId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", dId="
+ dId + "]";
}
}
3.配置文件
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/springcache?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#开启驼峰命名法
mybatis.configuration.map-underscore-to-camel-case=true
#打印sql语句日志
logging.level.com.lxj.cache.mappers=debug
#控制台打印配置信息
debug=true
4.创建mapper文件
package com.example.springbootcache.mappers;
import com.example.springbootcache.bean.Employee;
import org.apache.ibatis.annotations.*;
@Mapper
public interface EmployeeMapper {
@Select("SELECT * FROM employee WHERE id = #{id}")
public Employee getEmpById(Integer id);
@Update("UPDATE employee SET lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} WHERE id=#{id}")
public void updateEmpById(Employee employee);
@Delete("DELETE * FROM employee WHERE id=#{id}")
public void deleteEmpById(Integer id);
@Insert("INSERT INTO employee(lastName,email,gender,d_id) VALUES(#{lastName},#{email},#{gender},#{dId})")
public void InsertEmp(Employee employee);
@Select("SELECT * FROM employee WHERE lastName = #{lastName}")
public Employee getEmpByLastName(String lastName);
}
5.创建service文件
@CacheConfig(cacheNames = "emp")
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
@Cacheable(cacheNames = {"emp"})
public Employee getEmpById(Integer id){
System.out.println("查询"+id+"号员工");
Employee employee = employeeMapper.getEmpById(id);
return employee;
}
// @CachePut:既调用方法,又更新缓存数据;同步更新缓存
// 修改了数据库的某个数据,同时更新缓存
// 运行:
// 1.先调用目标方法
// 2.将目标方法的结果缓存起来
@CachePut(key= "#result.id")
public Employee updateEmp(Employee employee){
System.out.println("updateEmp"+employee);
employeeMapper.updateEmpById(employee);
return employee;
}
// @CacheEvict:缓存清除
// key:指定要清除的数据
// allEntries = true:指定清除这个缓存中的所有数据
// beforeInvocation=true 代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除
public void deleteEmp(Integer id){
System.out.println("deleteEmp:"+id);
int i = 101/0;
employeeMapper.deleteEmpById(id);
}
}
6.创建controller文件
@RestController
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@GetMapping("/emp/{id}")
public Employee getEmpById(@PathVariable("id") Integer id){
Employee employee = employeeService.getEmpById(id);
return employee;
}
@GetMapping("/emp")
public Employee updateEmp(Employee employee){
Employee emp = employeeService.updateEmp(employee);
return emp;
}
@GetMapping("/delEmp")
public String deleteEmp(Integer id){
employeeService.deleteEmp(id);
return "success";
}
}
工程的结构如下图所示:
7.向数据库添加测试数据
8.启动服务,通过http://localhost:8080/emp/2查询员工信息
以上说明这次查询操作是通过查询数据库获取的结果。
再次通过http://localhost:8080/emp/2查询员工
这次没有打印“查询2号员工”,说明这次是从缓存查询的数据。