• RedisTemplate序列化


    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            ObjectMapper om = new ObjectMapper();
            jackson2JsonRedisSerializer.setObjectMapper(om);
            redisTemplate.setKeySerializer(stringRedisSerializer);
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.setHashKeySerializer(stringRedisSerializer);
            redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    }

     测试类内容如下:

    @SpringBootTest
    class Demo4ApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
        @Test
        void redisTest() {
            redisTemplate.opsForValue().set("name","chengxu");
            Object name = redisTemplate.opsForValue().get("name");
            System.out.println(name);
        }
    
    }
  • 相关阅读:
    利用binlog2sql闪回丢失数据
    MySQL 5.7基于GTID的主从复制
    执行SQL查询导致磁盘耗尽故障演示
    mysqld_safe之三言两语
    利用MyFlash闪回丢失数据(续)
    泛型方法的局限及解决之道
    操作系统学习笔记:文件系统实现
    动态绑定滚动条事件
    asp.net mvc + javascript导入文件内容
    asp.net mvc + javascript生成下载文件
  • 原文地址:https://www.cnblogs.com/fengzi7314/p/15424688.html
Copyright © 2020-2023  润新知