首页 > 数据库 >spring-boot 引入redis

spring-boot 引入redis

时间:2022-11-08 21:00:09浏览次数:37  
标签:om redis spring boot jackson2JsonRedisSerializer template new ObjectMapper

1.引入redis

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>

2. 新建配置类

@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(factory);
//key序列化方式
        template.setKeySerializer(redisSerializer);
//value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(600))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
}

 

3. 使用

@Test
public void testRedis(){
    redisTemplate.opsForValue().set("name","cj");
    String name = (String) redisTemplate.opsForValue().get("name");
    System.out.println(name);
    System.out.println("redis");
}

 

标签:om,redis,spring,boot,jackson2JsonRedisSerializer,template,new,ObjectMapper
From: https://www.cnblogs.com/cciscc/p/16871184.html

相关文章

  • Redis笔记
    概述Redis是一个内存数据库,也就是指存储到内存条上的数据,而MySQL是一个外存数据库,将数据库存储在硬盘(外存)中一、Linux安装下载完毕后,解压压缩包(自己找目录)tar-zxv......
  • SpringBoot05(内部加载配置顺序)
    1-优先级小结:每个文件里面的配置都会被解析,但是会根据1-4的优先级而选取......
  • 我服了!SpringBoot升级后这服务我一个星期都没跑起来!(上)
    最近由于各方面的原因在准备升级SpringCloud和SpringBoot,经过一系列前置的调研和分析,决定把SpringBoot相关版本从2.1.6升级到2.7.5,SpringCloud相关版本从Gree......
  • spring
    1spring1.1简介spring:春天—->给软件行业带来了春天2002年,RodJahnson首次推出了Spring框架雏形interface21框架。2004年3月24日,Spring框架以interface21框架为基......
  • 在 windows 上安装 Redis
    在windows上安装RedisRedis官方不建议在windows下使用Redis,所以官网没有windows版本可以下载。还好微软团队维护了开源的windows版本,虽然只有3.2版本,对于......
  • bootstrap布局
      关于作者:郑云飞(天放),程序员Javaweibo:@tianFang学习路线图 ​​跟我一步一步学习bootstrap​​    ​​bootstrap在网页中使用​​    ​​bootstra......
  • springMVC+maven+mybatis+Intellij IDEA环境搭建
         之前工作中一直用springMVC和ibatis,现在要从0开始学习maven和mybatis,所以这篇博客就记录我的学习maven,mybatis,intellijIDEA的心路历程。     首......
  • 跟我一步一步学习bootstrap
    关于作者:郑云飞(天放),程序员Javaweibo:@tianFang学习路线图    ​​bootstrap在网页中使用​​    ​​bootstrap布局​​    ​​bootstrap响应式布......
  • SpringCloud实现简单的远程服务调用
    一、服务的提供者。1、Spring脚手架创建工程。填写项目信息: 添加web依赖: 添加MyBatis依赖: 填写项目位置: 生成的项目结构:  pom.xml文件中的依赖也自动......
  • SpringBoot-拦截器
    1.概述对访问路径进行拦截和放行的,底层使用的是java反射机制(动态代理)2.创建拦截器创建拦截类importorg.springframework.web.servlet.HandlerInterceptor;importo......