声明:小白,学习阶段,主要目的是为了记录学习过程,本文仅供参考,如有不足的地方欢迎指出讨论交流
本文基于Springboot2.1.3版本开发:
准备阶段
首先是pom.xml文件所需的依赖:
<dependencies>
<!--redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!--缓存模块所需依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
</dependencies>
接着就是application.properties,这里使用的是yum格式的配置文件:
spring:
datasource:
#配置mysql连接信息
url: jdbc:mysql://localhost:3306/enterprisetalentmanagement?serverTimezone=UTC
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: false
redis:
host: 192.168.0.120 #配置redis的IP地址
port: 6379 #该属性默认使用的是6379,如果你redis端口映射的不是6379可以通过这个属性来改
logging:
level:
#打印SQL信息
com.demo02.demo.Dao: debug
mybatis:
configuration:
#开启下划线到驼峰命名法的自动转换,将数据库字段根据驼峰规则自动注入到对象属性
map-underscore-to-camel-case: true
server:
port: 8080
debug: true
这个地方我们开启debug:true,这样我们就可以在程序运行的时候很方便的看到哪些配置类生效,结果会打印到控制台中,接着是springboot的启动类:
@SpringBootApplication //声明启动类
@MapperScan("com.demo02.demo.Dao.Mapper") //mapper文件扫描
@EnableCaching //开启注解
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
接下来在Service中我们来尝试一下缓存最基本的用法:
import com.demo02.demo.Dao.Mapper