首先,application使用@EnableCaching注解。
@EnableCaching
@SpringBootApplication
public class GatheringApplication {
public static void main(String[] args) {
SpringApplication.run(GatheringApplication.class, args);
}
@Bean
public IdWorker idWorkker(){
return new IdWorker(1, 1);
}
}
第二、在service find系列方法中增加缓存。使用@Cacheable注解。
/**
* 根据ID查询实体
* @param id
* @return
*/
@Cacheable(value = "gathering",key="#id")//{"value::key":"{活动内容}"}
public Gathering findById(String id) {
return gatheringDao.findById(id).get();
}
@Cacheable注解的作用是:
第三、在修改类service方法中使用@CacheEvict去除缓存。
/**
* 修改
* @param gathering
*/
@CacheEvict(value = "gathering",key = "gathering.id")
public void update(Gathering gathering) {
gatheringDao.save(gathering);
}
就是这么简单!再也不用判空了!