ToB企服应用市场:ToB评测及商务社交产业平台

标题: springboot中操作redis [打印本页]

作者: 种地    时间: 2023-6-19 03:37
标题: springboot中操作redis
1.maven引入相关依赖
  1. <dependencies>
  2.        
  3.         <dependency>
  4.             <groupId>org.springframework.boot</groupId>
  5.             <artifactId>spring-boot-starter-data-redis</artifactId>
  6.         </dependency>
  7.         
  8.         <dependency>
  9.             <groupId>org.apache.commons</groupId>
  10.             <artifactId>commons-pool2</artifactId>
  11.             <version>2.11.1</version>
  12.         </dependency>
  13.         
  14.         <dependency>
  15.             <groupId>com.fasterxml.jackson.core</groupId>
  16.             <artifactId>jackson-databind</artifactId>
  17.         </dependency>
  18.         
  19.         <dependency>
  20.             <groupId>org.projectlombok</groupId>
  21.             <artifactId>lombok</artifactId>
  22.             <optional>true</optional>
  23.         </dependency>
  24.         
  25.         <dependency>
  26.             <groupId>org.springframework.boot</groupId>
  27.             <artifactId>spring-boot-starter-test</artifactId>
  28.             <scope>test</scope>
  29.         </dependency>
  30. </dependencies>
复制代码
2.配置redis

application.yml
  1. spring:
  2.   # 配置redis
  3.   redis:
  4.     host: 192.168.***.***
  5.     port: 6379
  6.     password: ******
  7.     lettuce:
  8.       pool:
  9.         max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
  10.         max-idle: 8 # 连接池中的最大空闲连接
  11.         max-wait: 100 # 连接池最大阻塞等待时间(使用负值表示没有限制)
  12.         min-idle: 0 # 连接池中的最小空闲连接
  13.     database: 0 # redis数据库索引(默认为0)
复制代码
3.配置序列化
  1. package com.example.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  7. import org.springframework.data.redis.serializer.RedisSerializer;
  8. /**
  9. * Redis  序列化方式配置
  10. *
  11. */
  12. @Configuration
  13. public class RedisConfig {
  14.     @Bean
  15.     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  16.         // 创建RedisTemplate<String, Object>对象
  17.         RedisTemplate<String, Object> template = new RedisTemplate<>();
  18.         // 配置连接工厂
  19.         template.setConnectionFactory(factory);
  20.         // json方式序列化对象
  21.         GenericJackson2JsonRedisSerializer jsonRedisSerializer =
  22.                 new GenericJackson2JsonRedisSerializer();
  23.         // key采用String的序列化方式
  24.         template.setKeySerializer(RedisSerializer.string());
  25.         // hash的key也采用String的序列化方式
  26.         template.setHashKeySerializer(RedisSerializer.string());
  27.         // value序列化方式采用jackson
  28.         template.setValueSerializer(jsonRedisSerializer);
  29.         // hash的value序列化方式采用jackson
  30.         template.setHashValueSerializer(jsonRedisSerializer);
  31.         return template;
  32.     }
  33. }
复制代码
4.测试类中进行测试
  1. package com.example;
  2. import com.example.entity.User;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.junit.jupiter.api.Test;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.core.StringRedisTemplate;
  10. @SpringBootTest
  11. class SpringDataRedisTestApplicationTests {
  12.     @Autowired
  13.     private RedisTemplate<String,Object> redisTemplate;
  14.     @Autowired
  15.     private StringRedisTemplate stringRedisTemplate;
  16.     private static final ObjectMapper objectMapper = new ObjectMapper();
  17.     /**
  18.      * 测试redis的String类型
  19.      */
  20.     @Test
  21.     void testString() {
  22.         redisTemplate.opsForValue().set("name", "minqiliang");
  23.         System.out.println(redisTemplate.opsForValue().get("name"));
  24.     }
  25.     /**
  26.      * 使用StringRedisTemplate操作redis(需要手动进行序列化和反序列化)
  27.      * @throws JsonProcessingException
  28.      */
  29.     @Test
  30.     void testString2() throws JsonProcessingException {
  31.         // 创建一个对象
  32.         User user = new User("001", "minqiliang", 18);
  33.         // 由于StringRedisTemplate默认使用的是String的序列化器,所以这里需要将对象转换成json字符串
  34.         String json = objectMapper.writeValueAsString(user);
  35.         // 存入redis
  36.         stringRedisTemplate.opsForValue().set("user:001", json);
  37.         // 从redis中取出数据
  38.         String s = stringRedisTemplate.opsForValue().get("user:001");
  39.         // 将json字符串转换成对象
  40.         User u = objectMapper.readValue(s, User.class);
  41.         System.out.println(u);
  42.     }
  43.     @Test
  44.     void testHash() {
  45.         // 存入redis
  46.         redisTemplate.opsForHash().put("user:002", "id", "002");
  47.         redisTemplate.opsForHash().put("user:002", "name", "张三");
  48.         redisTemplate.opsForHash().put("user:002", "age", 18);
  49.         // 获取对应的key的所有值
  50.         System.out.println(redisTemplate.opsForHash().entries("user:002"));
  51.         System.out.println("====================================");
  52.         // 获取对应的key的某个值
  53.         System.out.println(redisTemplate.opsForHash().get("user:002", "id"));
  54.         System.out.println(redisTemplate.opsForHash().get("user:002", "name"));
  55.         System.out.println(redisTemplate.opsForHash().get("user:002", "age"));
  56.     }
  57. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4