SpringBoot + JSP 开启缓存功能

一:配置pom文件,加入依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.3.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.2</version>
</dependency>
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <version>9.0.37</version>
</dependency>

二:修改配置文件

server:
  port: 8080
spring:
  mvc:
    view: #引用 /WEB-INF/jsp/ 下的jsp文件
      prefix: /WEB-INF/jsp/ 
      suffix: .jsp 
  cache:
     type: simple

三:在类或方法上添加缓存

可以在类或方法上使用 @Cacheable 注解开启 SpringBoot 中的缓存功能。

@Cacheable 可以指定缓存的名称、缓存的 key 值和缓存的过期时间等参数

在示例中,我们使用 @Cacheable 注解将 getUserById 方法的返回值缓存到名为 userCache 的缓存中,并指定缓存的 key 值为方法的参数 id。

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Cacheable(value = "userCache", key = "#id")
    public User getUserById(String id) {
        return userDao.getUserById(id);
    }
}

四:在启动类上加入注解

在启动类上加入@EnableCaching 此注解,开启缓存功能。

@EnableCaching //加入此注解,开启缓存功能
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
{
    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(RuoYiApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  若依启动成功!!!");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值