Springboot使用@EnableCache缓存

1、pom.xml文件引入依赖



    org.springframework.boot
    spring-boot-starter-cache

 2、使用@EnableCaching注解在主配置类开启缓存

@EnableCaching//开启缓存
@SpringBootApplication
public class SpringbootmybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootmybatisApplication.class, args);
    }
    @Bean
    public UserFilter getUserFilter(){
        return new UserFilter();
    }
}

3、在方法前使用@Cacheable注解,将运行结果缓存

    @Cacheable(cacheNames = "user")//将方法运行结果缓存
    @RequestMapping("/findAllUser")
    @ResponseBody
    public String findAllUser(){
        List users = userDao.findAllUser();
        List list = new ArrayList();
        for (User user:users) {
            list.add(user.getId());
        }
        return list.toString();
    }

 

你可能感兴趣的:(Springboot)