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

Springboot中redis序列化和反序列化配置

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

Springboot中redis序列化和反序列化配置

目录

1.说明

2.配置

1.引入依赖

        2.添加配置类

        3.测试实体对象

        4.测试集合

3.测试


1.说明

在java项目中使用redis时候是通过字节流的方式进行存取的,所以不论是查看还是获取都需要转换成java对象,这样会更加方便

2.配置

1.引入依赖
这里本来想要使用的org.springframework.boot的版本,但是会出现问题,可能是版本匹配问
1.4.7.RELEASE
        
            org.springframework.boot
            spring-boot-starter-redis
            ${spring-boot-starter-redis-version}
        

所以建议大家使用这个依赖


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


   com.alibaba
    fastjson
    1.2.72

        2.添加配置类

        可以参考下面创建配置类 RedisConfiguration

package com.hhmt.flowalliance.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
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.StringRedisSerializer;


@Configuration
public class RedisConfiguration {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(factory);

        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);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

添加配置文件

写好自己连接redis的参数信息

spring:
  application:
    name: 
  redis:
    client-name: redis
    host: 
    port: 
    password: 
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    connect-timeout: 3000

        3.测试实体对象

创建实体类User用于测试

package com.hhmt.flowalliance.model;

import lombok.Data;


@Data
public class User {

    private String name;
    private String age;

}

        4.测试集合

注意:这里使用的是set方法,对,你没看错,是存储字符串的set方法

        如果使用操作集合的方法会变成存进去和拿出来的数据会变成字符串,有兴趣的朋友可以尝试,也可能是我方法不对,如果感兴趣的朋友欢迎在下方和我留言互动.

 

3.测试

测试实体对象

测试存储List对象

在这里向大家推荐一款好用的redis界面化操作工具,好看也好用

GitHub - qishibo/AnotherRedisDesktopManager: A faster, better and more stable redis desktop manager [GUI client], compatible with Linux, Windows, Mac. What's more, it won't crash when loading massive keys.

原文可参考:springboot整合redis序列化(3步搞定)_潘哒哒曦的博客-CSDN博客_springboot序列化

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

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

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