1、SpringBoot与缓存
SpringBoot使用Spring-Cache来操作缓存,常用的有@Cacheable、@CacheEvict、@CachePut、@EnableCache
1.1、引入cache相关的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
1.2、在主程序类前加@EnableCaching开启缓存
@EnableCaching
@SpringBootApplication
public class Springboot02RedisApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot02RedisApplication.class, args);
}
}
1.3、连接MySQL数据库进行前期工作准备
#连接mysql数据库
spring.datasource.url=jdbc:mysql://localhost:3306/spring-cache
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#开启驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true
1.4、创建实体类
- Employee 类
public class Employee implements Serializable {
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 + "]";
}
}
1.5、建立相应的mapper文件查询数据库
@Mapper
public interface EmployeeMapper {
//通过id查询雇员的信息
@Select("SELECT * FROM employee WHERE id = #{id}")
public Employee getById(Integer id);
//更新员工的信息
@Update("UPDATE employee SET lastname = #{lastName}," +
"email = #{email}," +
"gender = #{gender}," +
"d_id = #{dId} WHERE id = #{id}")
public void updateEmployee(Employee employee);
}
1.6、创建相应的service类并将方法返回的结果存储到缓存
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
@Cacheable(cacheNames = "emp") //将方法返回的结果存储到名为emp的缓存中
public Employee getById(Integer id){
System.out.println("查询了" + id + "员工");
Employee employee = employeeMapper.getById(id);
return employee;
}
//方法被调用,结果也被缓存
@CachePut(cacheNames = "emp")
public Employee updateEmp(Employee employee){
employeeMapper.updateEmployee(employee);
return employee;
}
}
1.7、创建对应的controller类
@RestController
public class EmployeeController {
@Autowired
EmployeeService employeeService;
//@PathVariable("id")注解从路径中取值
@GetMapping("/search/{id}")
public Employee getById(@PathVariable("id") Integer id){
Employee employee = employeeService.getById(id);
return employee;
}
@GetMapping("/emp")
public Employee updateEmployee(Employee employee){
Employee emp = employeeService.updateEmp(employee);
return emp;
}
}
1.8、查看缓存是否生效
- 通过日志可以看出,进行第一遍查询时出现了"查询了1员工",以后的查询控制台不再打印,即缓存已经生效。