SpringData是Spring中数据操作的模块,包含对各种数据库的集成,其中对Redis的集成模块就叫做SpringDataRedis,官网地址:https://spring.io/projects/spring-data-redis
SpringDataRedis中提供了RedisTemplate工具类,其中封装了各种对Redis的操作。并且将不同数据类型的操作API封装到了不同的类型中。

<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-pool2artifactId>
dependency>
spring.redis.host=192.168.150.101
spring.redis.port=6379
spring.redis.password=123456
# 最大连接
spring.redis.lettuce.pool.max-active=8
# 最大空闲
spring.redis.lettuce.pool.max-idle=8
# 最小空闲连接
spring.redis.lettuce.pool.min-idle=0
# 连接等待时间(ms)
spring.redis.lettuce.pool.max-wait=100
@SpringBootTest
@Slf4j
public class RedisStudy {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testSpring() {
// 插入一条string类型数据
redisTemplate.opsForValue().set("name", "李四");
// 读取一条string类型数据
final Object name = redisTemplate.opsForValue().get("name");
log.info("name:{}", name);
}
}