redis序列化配置

package com.juejiu.config;

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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        // 创建模板
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // 设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        // key和 hashKey采用 string序列化
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        // value和 hashValue采用 JSON序列化
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jsonRedisSerializer);
        return redisTemplate;
    }
}

yml中的配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/cloud_jiu_me?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
  redis:
    host: localhost
    port: 6379

测试

package com.juejiu;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class JueJiuApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
    @Test
    void contextLoads() {

        redisTemplate.opsForValue().set("name","张三");
        Object name = redisTemplate.opsForValue().get("name");

        System.out.println(name.toString());


    }

}

结果

ok,点赞收藏不迷路。

相关推荐

  1. RedisTemplate序列配置

    2024-04-03 19:32:03       54 阅读
  2. Springboot Jackson 序列与反序列配置

    2024-04-03 19:32:03       48 阅读
  3. springboot基础(81):设置redis序列

    2024-04-03 19:32:03       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-03 19:32:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-03 19:32:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-03 19:32:03       87 阅读
  4. Python语言-面向对象

    2024-04-03 19:32:03       96 阅读

热门阅读

  1. 当代深度学习模型介绍--Transformer模型

    2024-04-03 19:32:03       32 阅读
  2. 十八、Rust gRPC 多 proto 演示

    2024-04-03 19:32:03       33 阅读
  3. 入门级Python编程题(2)

    2024-04-03 19:32:03       38 阅读
  4. Transformer学习-最简DEMO实现字符串转置

    2024-04-03 19:32:03       27 阅读
  5. 【Linux】linux背景知识

    2024-04-03 19:32:03       36 阅读
  6. web蓝桥杯真题:健身大调查

    2024-04-03 19:32:03       35 阅读
  7. leetcode - 1248. Count Number of Nice Subarrays

    2024-04-03 19:32:03       36 阅读
  8. watch于watcheffect的区别

    2024-04-03 19:32:03       41 阅读
  9. STM32 GPIO输入检测——按键

    2024-04-03 19:32:03       46 阅读
  10. arm架构离线部署docker

    2024-04-03 19:32:03       36 阅读
  11. 短视频中可能触发降权的行为有哪些?

    2024-04-03 19:32:03       40 阅读
  12. Linux文件系统深入解析

    2024-04-03 19:32:03       38 阅读