一.创建springboot项目
二.引入2个依赖
<!-- redis依赖-->这个已经引入了,因为创建的时候勾选了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- common-pool连接池依赖-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
三.写yml配置文件
记得把文件后缀改为yml
spring:
redis:
host: 127.0.0.1
port: 6380
password: 123321
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 1000ms
四.注解注入RedisTemplate
@Autowired private RedisTemplate redisTemplate;
五.然后就可以使用了
@SpringBootTest
class RedisDemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
redisTemplate.opsForValue().set("name","lisi");
Object name =redisTemplate.opsForValue().get("name");
System.out.println("name = " + name);
}
}