当前位置:   article > 正文

springboot整合redis

springboot整合redis

一.springboot整合Redis 

springboot整合redis时提供了两个模板工具类,StringRedisTemplate和RedisTemplate.

1.StringRedisTemplate

 (1) 引入相关的依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

(2)注入StringRedisTemplate该类对象

  1. @Autowired
  2. private StringRedisTemplate redisTemplate;

(3)使用StringRedisTemplate

该类把对每种数据类型的操作,单独封了相应的内部类。

  1. package com.wt;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.data.redis.core.HashOperations;
  6. import org.springframework.data.redis.core.StringRedisTemplate;
  7. import org.springframework.data.redis.core.ValueOperations;
  8. import java.time.Duration;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Set;
  13. import java.util.concurrent.TimeUnit;
  14. @SpringBootTest
  15. class SpringbootRedis01ApplicationTests {
  16. //里面所有的key还是value field 它的类型必须都是String类型
  17. //因为key和value获取field它们使用的都是String的序列化方式
  18. @Autowired
  19. private StringRedisTemplate redisTemplate;
  20. //以hash方式对String操作
  21. @Test
  22. public void test01(){
  23. HashOperations<String, Object, Object> forHash = redisTemplate.opsForHash();
  24. //以hash方式向Redis存储数据
  25. forHash.put("k2","name","王戎");
  26. forHash.put("k2","age","25");
  27. forHash.put("k2","sex","男");
  28. Map map = new HashMap();
  29. map.put("name","山涛");
  30. map.put("age","23");
  31. map.put("sex","男");
  32. forHash.putAll("k3",map);
  33. //通过hash获取指定的key下指定键的值
  34. Object o = forHash.get("k3", "name");
  35. System.out.println(o);
  36. //获取K3下所有的键
  37. Set<Object> k3 = forHash.keys("k3");
  38. System.out.println(k3);
  39. //获取k3下所有的值
  40. List<Object> k31 = forHash.values("k3");
  41. System.out.println(k31);
  42. //获取k3下所有键值对
  43. Map<Object, Object> k32 = forHash.entries("k3");
  44. System.out.println(map);
  45. //删除k3下的指定键
  46. Long delete = forHash.delete("k3", "age");
  47. System.out.println(delete);
  48. }
  49. //对String的操作
  50. @Test
  51. void contextLoads() {
  52. ValueOperations<String, String> forValue = redisTemplate.opsForValue();
  53. //Set<String> keys = redisTemplate.keys("k1");
  54. forValue.set("k1","k2", Duration.ofSeconds(3000));
  55. String k1 = forValue.get("k1");
  56. System.out.println(k1);
  57. //相当于setnx ------如果存在该键则不存入,不存在则存入
  58. Boolean aBoolean = forValue.setIfAbsent("k1", "刘伶", 30, TimeUnit.SECONDS);
  59. Boolean aBoolea = forValue.setIfAbsent("k11", "刘伶", 30, TimeUnit.SECONDS);
  60. System.out.println(aBoolea);
  61. forValue.append("k11","斤斤计较");
  62. }
  63. }

2. RedisTemplate

  1. package com.wt;
  2. import com.wt.entity.Dept;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.data.redis.core.ValueOperations;
  8. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. @SpringBootTest
  11. public class SprigbootRedis02 {
  12. //当你存储的value类型为对象类型使用redisTemplate
  13. //存储的value类型为字符串,StringRedisTemplate
  14. @Autowired
  15. private RedisTemplate redisTemplate;
  16. @Test
  17. public void test01(){
  18. /在使用RedisTemplate前必须序列化,否则数据库会出现乱码
  19. redisTemplate.setKeySerializer(new StringRedisSerializer()); //给key添加序列化
  20. redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));//给value添加序列化
  21. //对String类型操作类
  22. ValueOperations forValue = redisTemplate.opsForValue();
  23. //如果不设置序列化,key和value默认采用jdk的序列化方式
  24. forValue.set("k1","陈奕迅");
  25. Object k1 = forValue.get("k1");
  26. System.out.println(k1);
  27. //如果不实现序列化,value默认采用jdk的序列化方式,可以给实体类加序列化接口
  28. forValue.set("k3",new Dept(1,"刘念牛",23));
  29. }
  30. }

上面的RedisTemplate需要每次都指定key value以及field的序列化方式,所以我们可以写一个配置类,为RedisTemplate指定好序列化。以后再用就无需再指定。

  1. package com.wt.conf;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.cache.CacheManager;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.data.redis.cache.RedisCacheConfiguration;
  9. import org.springframework.data.redis.cache.RedisCacheManager;
  10. import org.springframework.data.redis.connection.RedisConnectionFactory;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  13. import org.springframework.data.redis.serializer.RedisSerializationContext;
  14. import org.springframework.data.redis.serializer.RedisSerializer;
  15. import org.springframework.data.redis.serializer.StringRedisSerializer;
  16. import java.time.Duration;
  17. /**
  18. * @Author wt
  19. * @Date 2022/8/2 19:56
  20. * @PackageName:com.wt.conf
  21. * @ClassName: RedisConfig
  22. * @Description: RedisTemplate的序列化配置文件
  23. * @Version 1.0
  24. */
  25. @
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/745029
推荐阅读
相关标签
  

闽ICP备14008679号