Springboot对redis的操作封装了两个
StringRedisTemplate和RedisTemplate类,StringRedisTemplate是RedisTemplate的子类,StringRedisTemplate它只能存储字符串类型,无法存储对象类型。要想用StringRedisTemplate存储对象必须把对象转为json字符串
利用Springboot 的环境搭建和开发便利等优势、
1.StringRedisTemplate
(1)引入相关的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
(2)注入StringRedisTemplate该类对象
@Autowired
private StringRedisTemplate redisTemplate;
(3) 使用StringRedisTemplate
该类把对每种数据类型的操作,单独封了相应的内部类。
package com.qy151redisspringboot;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import ja