1、pom.xml文件里面增加如下依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、yml文件增加如下配置:
redis:host: localhostport: 6379password: hemingdatabase: 10
配置结构如下图所示:
3、新增RedisConfiguration.java类
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
@Slf4j
public class RedisConfiguration {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {//创建TemplateRedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();//设置连接工厂redisTemplate.setConnectionFactory(redisConnectionFactory);//设置序列化工具GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();//key和hashKey采用string序列化方式redisTemplate.setKeySerializer(RedisSerializer.string());redisTemplate.setHashKeySerializer(RedisSerializer.string());//value和hashValue采用Json序列化方式redisTemplate.setValueSerializer(jsonRedisSerializer);redisTemplate.setHashValueSerializer(jsonRedisSerializer);return redisTemplate;}}
4、新增RedisTestController.java类
import com.hmblogs.backend.entity.Girl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.TimeUnit;@RestController
@Slf4j
public class RedisTestController {@Autowiredprivate RedisTemplate redisTemplate;/*** redis test* @return*/@GetMapping(value = "/redisTest")public void getGirlList(){log.info("bbbbb");redisTemplate.opsForValue().set("name","小明"); // 设置k vString city = (String) redisTemplate.opsForValue().get("name"); // 拿到key为name的值log.info(city);}/*** redis test* @return*/@GetMapping(value = "/redisTestJson")public void redisTestJson(){log.info("ccccc");Girl girlData = new Girl();girlData.setId(1);girlData.setCupSize("xxxx");girlData.setAge(12);redisTemplate.opsForValue().set("user:100",girlData);Girl girl = (Girl) redisTemplate.opsForValue().get("user:100");log.info(girl.toString());}
}
4、验证
浏览器直接访问GET接口
http://localhost:8081/redisTest
然后查看数据
发现没有乱码。
访问GET接口http://localhost:8081/redisTestJson
查看redis数据,如下图所示:
5、操作哈希类型数据
/*** redis test* @return*/@GetMapping(value = "/redisTestHash")public void redisTestHash(){log.info("ddddd");HashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("100","name","tom"); // 设置k为100,哈希k为name v为100hashOperations.put("100","age","20");String name = (String) hashOperations.get("100", "name"); // 获取k为100,hashk为name的vlog.info(name);Set keys = hashOperations.keys("100"); // 获取k为100的集合klog.info(keys.toString());List values = hashOperations.values("100"); // 获取k为100的集合vlog.info(values.toString());hashOperations.delete("100","age"); // 删除age字段}
访问http://localhost:8081/redisTestHash
查看数据
6、操作列表类型数据
/*** redis test* @return*/@GetMapping(value = "/redisTestList")public void redisTestList(){log.info("eeeee");//lpush lrange rpop llenListOperations listOperations = redisTemplate.opsForList();listOperations.leftPushAll("mylist","a","b","c"); // 往k为mylist插入a,b,clog.info(listOperations.range("mylist", 0, -1).toString());listOperations.leftPush("mylist","d"); // 插入dList mylist = listOperations.range("mylist", 0, -1);log.info(mylist.toString()); // 输出listOperations.rightPop("mylist"); // 删除Long size = listOperations.size("mylist");log.info(size.toString());log.info(listOperations.range("mylist", 0, -1).toString());}
访问如下接口:http://localhost:8081/redisTestList
服务端console打印如下:
rpop的时候,移除了最先放入的a,规则就是先进先出。
数据如下图所示:
7、操作集合类型数据
/*** redis test* @return*/@GetMapping(value = "/redisTestSet")public void redisTestSet(){log.info("fffff");//sadd smembers scard sinter sunion sremSetOperations setOperations = redisTemplate.opsForSet();setOperations.add("set1","a","b","c","d"); // 往set1 插入abcdsetOperations.add("set2","a","b","x","y"); // 往set2 插入abxySet members1 = setOperations.members("set1");log.info("set1:"+members1); // 输出Long size = setOperations.size("set1");log.info(size.toString()); // 输出Set members2 = setOperations.members("set2");log.info("set2:"+members2); // 输出Set intersect = setOperations.intersect("set1", "set2"); // 他们的交集log.info("inter:"+intersect);Set union = setOperations.union("set1", "set2");// 他们的并集log.info("union:"+union);setOperations.remove("set1","a","b"); // 在set1里面删除ab}
查看服务端的console打印如下:
查看set1的数据,如下图所示:
查看set2的数据,如下图所示:
8、操作有序集合类型数据
/*** redis test* @return*/@GetMapping(value = "/redisTestZSet")public void redisTestZSet(){log.info("ggggg");//zadd zrange zincrby zremZSetOperations zSetOperations = redisTemplate.opsForZSet();zSetOperations.add("zset1","a",10); // 设置k为zset1v为a,分数为10zSetOperations.add("zset1","b",12); //同理zSetOperations.add("zset1","c",9); // 同理Set zset1 = zSetOperations.range("zset1", 0, -1);log.info(zset1.toString()); // 输出zSetOperations.incrementScore("zset1","c",10); // 为k为zset1v为c的加10分Set zsetAfter = zSetOperations.range("zset1", 0, -1);log.info(zsetAfter.toString()); // 输出zSetOperations.remove("zset1","a","b"); // 删除abSet zsetDel = zSetOperations.range("zset1", 0, -1);log.info(zsetDel.toString()); // 输出}
查看console打印如下:
查看数据,如下图所示:
9、通用命令操作
/*** redis test* @return*/@GetMapping(value = "/redisTestCommon")public void redisTestCommon(){log.info("hhhhh");//keys exists type delSet keys = redisTemplate.keys("*"); // 查找所有符合给定模式( pattern)的 keylog.info(keys.toString());Boolean name = redisTemplate.hasKey("name");log.info(name.toString());Boolean set1 = redisTemplate.hasKey("set1");log.info(set1.toString());for (Object key : keys) {DataType type = redisTemplate.type(key); // 查找他们的类型是什么log.info(type.name());}redisTemplate.delete("mylist"); // 删除mylistlog.info("删除后:");for (Object key : keys) {DataType type = redisTemplate.type(key); // 查找他们的类型是什么log.info(type.name());}}
查看console打印如下内容:
mylist被删掉了,截图如下:
10、小结:
Spring Data Redis中提供了一个高度封装的类:RedisTemplate,对相关api进行了归类封装,将同一类型操作封装为operation接口,具体分类如下:
ValueOperations:string数据操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:hash类型的数据操作
ListOperations:list类型的数据操作