目录

1.说明

2.配置

1.引入依赖

        2.添加配置类

        3.测试实体对象

        4.测试集合

3.测试


1.说明

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

2.配置

1.引入依赖

这里本来想要使用的org.springframework.boot的版本,但是会出现问题,可能是版本匹配问
<spring-boot-starter-redis-version>1.4.7.RELEASE</spring-boot-starter-redis-version>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>${spring-boot-starter-redis-version}</version>
        </dependency>

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

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.72</version>
</dependency>

        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;

/**
 * 辉煌明天
 * FileName: RedisConfiguration
 * Author:   huachun
 * email: huachun_w@163.com
 * Date:     2021/11/5 15:57
 * Description: Redis配置类
 */
@Configuration
public class RedisConfiguration {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        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;

/**
 * 辉煌明天
 * FileName: User
 * Author:   huachun
 * email: huachun_w@163.com
 * Date:     2022/1/21 14:15
 * Description:
 */
@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序列化

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐