1、在springboot的 Application上开启缓存注解
1 @SpringBootApplication 2 @EnableCaching //开启基于注解的缓存 3 public class DemoApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(DemoApplication.class, args); 7 } 8 9 }
2、创建controller、service、dao层结构
3、在类或者方法里面使用注解
1 @Service 2 public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService { 3 4 5 @Override 6 @Cacheable(cacheNames = "user") //开启命名为user的缓存 7 public String colUserName(Long id) { 8 User user = new User(); 9 user.setId(id); 10 return (String) column("name",user);; 11 } 12 13 @Override 14 @CacheEvict(cacheNames = "user",allEntries = true) //删除命名为user的全部缓存 15 public void deleteUserCache() { 16 17 } 18 }
ps:缓存注解的具体说明,请查看 https://blog.csdn.net/dreamhai/article/details/80642010 这篇博客的文章