幾個緩存注解的作用:
@Cacheable:將方法的返回結(jié)果根據(jù)key指定的鍵保存在緩存中,以后要獲取相同的數(shù)據(jù)直接從緩存中共獲取
@CachePut:不管緩存中是否有需要的數(shù)據(jù),都會執(zhí)行該注解標(biāo)注的方法,并將結(jié)果更新到緩存,屬性見上
@CacheEvit:執(zhí)行方法后,清除key指定的緩存
@CacheConfig:定義一些通用或公共的規(guī)則,如cacheNames、keyGenerator等
可使用的SpEL表達式:

使用緩存的步驟:
(1)創(chuàng)建一個Spring Boot應(yīng)用,勾選Cache、Web、MySQL、Mybatis模塊,在主程序類上添加注解,開啟基于注解的緩存
@MapperScan(basePackages = "com.youngpain.cache.mapper")@SpringBootApplication@EnableCaching
(2)創(chuàng)建JavaBean,和數(shù)據(jù)庫中的表對應(yīng),并配置數(shù)據(jù)源
spring: datasource: url: jdbc:mysql://localhost:3306/mybatis_database username: root password: 1741248769 driver-class-name: com.mysql.jdbc.Driver redis: host: 39.108.114.57#開啟駝峰命名法mybatis: configuration: map-underscore-to-camel-case: truelogging: level: com.youngpain.cache.mapper: debug
(3)創(chuàng)建mapper接口進行增刪改查操作
/** * 部門表的增刪改查操作 */public interface DepartmentMapper { @Insert("insert into department(id,depart_name,depart_build) values(#{id},#{depart_name},#{depart_build})") void insertDepartment(Department department); @Delete("delete from department where id=#{id}") void deleteDepartment(Integer id); @Update("update department set depart_name=#{departName},depart_build=#{departBuild} where id=#{id}") void updateDepartment(Department department); @Select("select * from department where id=#{id}") Department getDepartmentById(Integer id);}(4)創(chuàng)建service
@Service@CacheConfig(cacheNames = {"departs"})public class DepartmentService { @Autowired DepartmentMapper departmentMapper; @Cacheable(key = "#a0.id") public void insertDepartment(Department department) { departmentMapper.insertDepartment(department); } @CacheEvict(key = "#p0") public void deleteDepartment(Integer id) { departmentMapper.deleteDepartment(id); } @CachePut(key = "#a0.id") public Department updateDepartment(Department department) { departmentMapper.updateDepartment(department); return department; } @Cacheable(key = "#id", condition = "#p0>=1") public Department getDepartmentById(Integer id) { return departmentMapper.getDepartmentById(id); }}(5)創(chuàng)建controller
@Controllerpublic class DepartmentController { @Autowired DepartmentService departmentService; @GetMapping("/index") public String index() { return "index"; } @GetMapping("/deleteDepart/{id}") public String deleteDepart(@PathVariable("id") Integer id, Model model) { model.addAttribute("condition", "delete"); Department delete = departmentService.getDepartmentById(id); model.addAttribute("department", delete); departmentService.deleteDepartment(id); return "success"; } @PostMapping("/updateDepart") public String updateDepart(Department department, Model model) { model.addAttribute("condition", "update"); Department update = departmentService.updateDepartment(department); model.addAttribute("department", update); return "success"; } @GetMapping("/getDepart/{id}") public String getDepartmentById(@PathVariable("id") Integer id, Model model) { model.addAttribute("condition", "delete"); Department get = departmentService.getDepartmentById(id); model.addAttribute("department", get); return "success"; }}(6)測試結(jié)果:
@Cacheable:第一次查詢數(shù)據(jù),控制臺發(fā)出sql語句,之后再查詢直接從緩存中獲取
@CachePut:調(diào)用方法修改某個數(shù)據(jù)后,再次查詢該數(shù)據(jù)是從緩存中獲取的更新后的數(shù)據(jù)
@CacheEvict:調(diào)用該方法后,再次查詢某個數(shù)據(jù)需要重新發(fā)出sql語句查詢
ps:之前只是用markdown記筆記,今天第一次用markdown寫文章,寫起來好舒服啊QAQ
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對VeVb武林網(wǎng)的支持。
新聞熱點
疑難解答
圖片精選