一:配置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("(♥◠‿◠)ノ゙ 若依启动成功!!!");
}
}