栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java连接redis之RedisTemplate

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java连接redis之RedisTemplate

引入依赖
 

        
            org.springframework.boot
            spring-boot-starter-data-redis
        

    @Test
    void testHash2() throws JsonProcessingException {
        Blog blog = new Blog();
        ValueOperations vo = redisTemplate.opsForValue();
        vo.set("blog", blog);
        System.out.println(vo.get("blog"));

        //=====
        HashOperations ho = redisTemplate.opsForHash();
        ObjectMapper objectMapper = new ObjectMapper();
        blog.setId(11L);
        blog.setTitle("mapp");
        String jsonStr = objectMapper.writevalueAsString(blog);//对象转成json字符串
        Map map = objectMapper.readValue(jsonStr, Map.class);//字符串转成map
        ho.putAll("blog-map", map);
//        ho.put("blog-map", "title", "mapp");
        System.out.println(ho.entries("blog-map"));
    }

 

修改默认的序列化规则:

package com.jt.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
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.RedisSerializer;

@Configuration
public class RedisConfig {

    //配置json序列化
    public RedisSerializer jsonSerializer() {
        //定义json序列化 反序列化对象
        Jackson2JsonRedisSerializer jsonSerializer =
                new Jackson2JsonRedisSerializer<>(Object.class);

        //定义序列化规则(规则由ObjectMapper定义)
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.GETTER,//类中的get方法
                JsonAutoDetect.Visibility.ANY);//任一访问修饰符

        //属性值为null时 该属性不进行序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        //将对象类型写入到序列化字符串中
        objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypevalidator(),//多态校验
                ObjectMapper.DefaultTyping.NON_FINAL,//非final类型运行序列化
                JsonTypeInfo.As.PROPERTY);//类型以属性形式进行存储

        jsonSerializer.setObjectMapper(objectMapper);

        return jsonSerializer;
    }


    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(factory);
        //修改默认的序列化方式(默认为jdk的序列化方式)
        //设置key的序列化方式
        template.setKeySerializer(RedisSerializer.string());//大key
        template.setHashKeySerializer(RedisSerializer.string());//小key

        //设置value的序列化方式
//        template.setValueSerializer(RedisSerializer.json());
//        template.setHashValueSerializer(RedisSerializer.json());
        template.setValueSerializer(jsonSerializer());
        template.setHashValueSerializer(jsonSerializer());

        template.afterPropertiesSet();
        return template;
    }
}
 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号