PS:如有问题,评论留言。
先引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
激活启动类注解@EnableCaching
实现类相应方法上加上这个注解
@Service
public class ImsSysClientServiceImpl implements ImsSysClientService {
@Resource
ImsSysClientMapper imsSysClientMapper;
@Cacheable(value = "client", key = "#id", unless = "#result == null")
public ImsSysClient selectByPrimaryKey(Integer id) {
return imsSysClientMapper.selectByPrimaryKey(id);
}
controller层测试代码
public void test(){
System.out.println("test");
ImsSysClient client1=imsSysClientService.selectByPrimaryKey(1);
System.out.println(client1);
ImsSysClient client2=imsSysClientService.selectByPrimaryKey(1);
System.out.println(client2);
}
默认用redis缓存
如果不想用redis缓存,只是把它放内存中。(在serviceImpl里面加入下面这个管理器)
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("client"),new ConcurrentMapCache("clientId"),new ConcurrentMapCache("jwtinfo")));
cacheManager.afterPropertiesSet();
return cacheManager;
}
}
如果配了多个不同名字的缓存注解,在红线部分把名字配上去。不过这个管理器不能放在单独的文件中,否则又会使用redis缓存。(我也不知道为什么)