当前位置:   article > 正文

SpringBoot篇——SpringBoot集成Redis,详细教程,一篇搞定!_springboot redis

springboot redis

了解redis在springboot中的自动配置类

分析源码,SpringBoot的底层,会对每一个功能都写一个自动配置类autoXXX,所以Redis也会存在。
找到这个自动配置类,分析这个配置类做了哪些事儿

另外每一个自动配置类都和一个.properties配置类进行绑定,我们可以通过他绑定的方式,在application.properties中个性化的配置

集成方法

一、导入整合的依赖

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

二、配置连接,在application.properties中写

  1. #配置Redis
  2. #配置主机
  3. spring.redis.host=127.0.0.1
  4. #配置端口
  5. spring.redis.port=6379


三、测试   

  1. @SpringBootTest
  2. class SpringbootRedisApplicationTests {
  3.     //想在springboot中使用redis,首先先将spring中给我们写的模板注入进来
  4.     @Autowired
  5.     private RedisTemplate redisTemplate;
  6.     @Test
  7.     void contextLoads() {
  8. //可以操作不同的数据类型......
  9.         //操作字符串,类似于String
  10.         redisTemplate.opsForValue().set("name","张三");
  11.         //操作List集合,类似于List
  12.         redisTemplate.opsForList();
  13. //除了基本的操作,我们还可以直接操作redisTemplate来执行事务或者增删改查
  14.     }
  15. }

自定义RedisTemplate(学习配置的内容)

一、首先我们需要了解为什么我们非得要自定义redisTemplate呢?
源码中的模板对于数据序列化是这么处理的

二、编写流程
在config包下新建一个类,命名为RedisConfig,将来关于Redis的配置都写在这个类中即可
这段配置,如果我们真正项目中用到了redis,直接拿着用

  1. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  2. import com.fasterxml.jackson.annotation.PropertyAccessor;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. //Redis配置类,需要用@Configuration注解标记这个类是一个配置类
  11. //现在这个配置类是一个固定的模板,如果需要配置redis,直接拿走用即可
  12. @Configuration
  13. public class RedisConfig {
  14. //编写我们自己的RedisTemplate
  15. @Bean
  16. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  17. RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
  18. template.setConnectionFactory(redisConnectionFactory);
  19. //Json序列化配置
  20. //设置序列化的方式,想要用jackson序列化,直接new出它的对象,传object.class,表示解析任何的对象,放进setKeySerializer方法中即可
  21. Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
  22. ObjectMapper om = new ObjectMapper();
  23. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  24. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  25. objectJackson2JsonRedisSerializer.setObjectMapper(om);
  26. //string的序列化
  27. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  28. //key采用String的序列化方式
  29. template.setKeySerializer(stringRedisSerializer);
  30. //hash的key采用String的序列化方式
  31. template.setHashKeySerializer(stringRedisSerializer);
  32. //value的序列化采用jackson
  33. template.setValueSerializer(objectJackson2JsonRedisSerializer);
  34. //hash的value采用jackson
  35. template.setHashValueSerializer(objectJackson2JsonRedisSerializer);
  36. template.afterPropertiesSet();
  37. return template;
  38. }
  39. }

拓展:将常规对象转化成json对象的方法(序列化的重要性)

  1. @SpringBootTest
  2. class SpringbootRedisApplicationTests {
  3. //想在springboot中使用redis,首先先将spring中给我们写的模板注入进来
  4. @Autowired
  5. private RedisTemplate redisTemplate;
  6. @Test
  7. void contextLoads() {
  8. //可以操作不同的数据类型......
  9. //操作字符串,类似于String
  10. redisTemplate.opsForValue().set("name","张三");
  11. //操作List集合,类似于List
  12. redisTemplate.opsForList();
  13. //除了基本的操作,我们还可以直接操作redisTemplate来执行事务或者增删改查
  14. }
  15. @Test
  16. public void test() throws JsonProcessingException {
  17. //真是开发中一般都是使用json来传递对象
  18. User user = new User("张三",12);
  19. //调用ObjectMapper()方法,调用writeValueAsString方法将常规的对象转化成json对象
  20. String jsonUser = new ObjectMapper().writeValueAsString(user);
  21. redisTemplate.opsForValue().set("user",jsonUser);
  22. redisTemplate.opsForValue().get("user");
  23. }
  24. }

序列化在将来的企业级开发中是非常常见的,所有的pojo类基本上都要先实现serializable接口,我们系统默认的序列化的方式是jdk,如果想要自定义序列化的方式,那么就要去上文中复习自定义的方法!
真实的开发中都是用json来传递对象,所以转化的方法很重要!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/152145
推荐阅读
相关标签
  

闽ICP备14008679号