Springboot之缓存

发布时间 2023-03-26 10:00:35作者: his365

依赖

<!-- 开启缓存 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

开启缓存

  • EnableCaching
@SpringBootApplication
@EnableCaching
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

使用

  • Cacheable
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>
        implements DeptService {
    @Resource
    private DeptMapper mapper;
    @Cacheable(value = "cacheSpace" ,key="#deptno")
    @Override
    public Dept getByDeptCode(Integer deptno) {
        return mapper.getByDeptCode(deptno);
    }
}