Skip to content

Commit 4c43be0

Browse files
author
曾梓健
committed
@CacheEvict的实例
1 parent 11a0758 commit 4c43be0

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

springboot_cache/src/main/java/com/hand/springboot_cache/controller/EmployeeController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.hand.springboot_cache.service.EmployeeService;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.cache.annotation.Cacheable;
7+
import org.springframework.web.bind.annotation.DeleteMapping;
78
import org.springframework.web.bind.annotation.PathVariable;
89
import org.springframework.web.bind.annotation.PutMapping;
910
import org.springframework.web.bind.annotation.RequestBody;
@@ -31,4 +32,9 @@ public Employee getEmpById(@PathVariable("id") Integer id) {
3132
public Employee updateEmp(@RequestBody Employee employee) {
3233
return employeeService.updateEmp(employee);
3334
}
35+
36+
@DeleteMapping("/emp/{id}")
37+
public void delEmp(@PathVariable("id") Integer id) {
38+
employeeService.deleteEmp(id);
39+
}
3440
}

springboot_cache/src/main/java/com/hand/springboot_cache/service/EmployeeService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.hand.springboot_cache.entity.Employee;
44
import com.hand.springboot_cache.mapper.EmployeeMapper;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cache.annotation.CacheEvict;
67
import org.springframework.cache.annotation.CachePut;
78
import org.springframework.cache.annotation.Cacheable;
89
import org.springframework.stereotype.Service;
@@ -46,4 +47,10 @@ public Employee updateEmp(Employee employee) {
4647
}
4748

4849

50+
@CacheEvict(cacheNames = {"emp"}, key = "#id", beforeInvocation = true)
51+
public void deleteEmp(int id) {
52+
employeeMapper.deleteEmpById(id);
53+
}
54+
55+
4956
}

0 commit comments

Comments
 (0)