当前位置:   article > 正文

Java --- springboot3整合redis_springboot3 redis

springboot3 redis

目录​​​​​​​

一、整合redis

1.1、导入pom依赖

1.2、修改springboot配置文件

1.3、代码测试 

二、测试访问redis五大常用数据类型

三、自动配置原理

四、定制化

4.1、解决redis存储序列化乱码问题 

4.2、redis客户端使用jedis


一、整合redis

1.1、导入pom依赖

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

1.2、修改springboot配置文件

1.3、代码测试 

  1. @RestController
  2. public class RedisController {
  3. @Autowired
  4. private StringRedisTemplate stringRedisTemplate;
  5. @GetMapping("/count")
  6. public String count(){
  7. Long count = stringRedisTemplate.opsForValue().increment("k1");
  8. return "访问了"+count+"次";
  9. }
  10. }

二、测试访问redis五大常用数据类型

  1. @SpringBootTest
  2. class Boot3RedisApplicationTests {
  3. @Autowired
  4. private StringRedisTemplate stringRedisTemplate;
  5. /**
  6. * 普通字符串
  7. */
  8. @Test
  9. void contextLoads() {
  10. stringRedisTemplate.opsForValue().set("test1", UUID.randomUUID().toString());
  11. String k = stringRedisTemplate.opsForValue().get("test1");
  12. System.out.println(k);
  13. }
  14. /**
  15. * list类型
  16. */
  17. @Test
  18. void testList(){
  19. stringRedisTemplate.opsForList().leftPush("a1","1");
  20. stringRedisTemplate.opsForList().leftPush("a1","2");
  21. stringRedisTemplate.opsForList().leftPush("a1","3");
  22. String a1 = stringRedisTemplate.opsForList().leftPop("a1");
  23. Assertions.assertEquals("3",a1);
  24. }
  25. /**
  26. * set类型
  27. */
  28. @Test
  29. void testSet(){
  30. stringRedisTemplate.opsForSet().add("set1","1","2","3");
  31. Boolean set1 = stringRedisTemplate.opsForSet().isMember("set1", "2");
  32. Assertions.assertTrue(set1);
  33. }
  34. /**
  35. * zset类型
  36. */
  37. @Test
  38. void testZSet(){
  39. stringRedisTemplate.opsForZSet().add("zset1","张三",12);
  40. stringRedisTemplate.opsForZSet().add("zset1","李四",13);
  41. stringRedisTemplate.opsForZSet().add("zset1","王五",14);
  42. ZSetOperations.TypedTuple<String> zset1 = stringRedisTemplate.opsForZSet().popMax("zset1");
  43. System.out.println(zset1.getValue()+":"+zset1.getScore());
  44. }
  45. /**
  46. * hash类型
  47. */
  48. @Test
  49. void testHash(){
  50. stringRedisTemplate.opsForHash().put("hash1","name","tom");
  51. stringRedisTemplate.opsForHash().put("hash1","age","12");
  52. }
  53. }

三、自动配置原理

四、定制化

  1. @Data
  2. public class Person implements Serializable {
  3. private Integer id;
  4. private String name;
  5. private Integer age;
  6. }
  1. @GetMapping("/addPerson")
  2. public void addPerson(){
  3. Person person = new Person();
  4. person.setId(1);
  5. person.setName("tom");
  6. person.setAge(12);
  7. //序列化,将对象转为字符串
  8. redisTemplate.opsForValue().set("person",person);
  9. }
  10. @GetMapping("/getPerson")
  11. public Person getPerson(){
  12. Person person = (Person) redisTemplate.opsForValue().get("person");
  13. return person;
  14. }

4.1、解决redis存储序列化乱码问题 

发现存储在redis中的数据乱码,不可视

 因为RedisTemplate使用的是默认序列化机制,为了方便其他系统交互应使用JOSN数据交互

解决代码如下:

  1. @Configuration
  2. public class AppRedisConfig {
  3. @Bean
  4. public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
  5. RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
  6. redisTemplate.setConnectionFactory(redisConnectionFactory);
  7. //把对象转为json字符串的序列化
  8. redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
  9. return redisTemplate;
  10. }
  11. }

4.2、redis客户端使用jedis

 RedisTemplate底层默认使用lettuce

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>io.lettuce</groupId>
  8. <artifactId>lettuce-core</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <dependency>
  13. <groupId>redis.clients</groupId>
  14. <artifactId>jedis</artifactId>
  15. </dependency>

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号