StringRedisTemplate

复制代码
package com.wsd;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@SpringBootApplication
public class SpringDataRedis {

    public static void main(String[] args) throws Exception{

        ConfigurableApplicationContext context = SpringApplication.run(SpringDataRedis.class, args);

        //ObjectMapper类是Jackson库的核心类之一,用于在Java对象和JSON数据之间进行序列化和反序列化。
        ObjectMapper mapper = new ObjectMapper();

        //key,value 都将以String的形式存入redis
        StringRedisTemplate stringRedisTemplate =  context.getBean(StringRedisTemplate.class);

        Person person1  = new Person("罗小白","罗小黑");
        //将对象转换为json字符串
        String json = mapper.writeValueAsString(person1);

        stringRedisTemplate.opsForValue().set("1",json);


        String person =  stringRedisTemplate.opsForValue().get("1");

        Person s = mapper.readValue(person,Person.class);

        System.out.println(s.getName() + ":" + s.getCat());
    }
}
相关推荐
我很好我还能学19 分钟前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
程序员JerrySUN32 分钟前
[特殊字符] 深入理解 Linux 内核进程管理:架构、核心函数与调度机制
java·linux·架构
2302_8097983235 分钟前
【JavaWeb】Docker项目部署
java·运维·后端·青少年编程·docker·容器
蓝婷儿40 分钟前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
渣渣盟1 小时前
基于Scala实现Flink的三种基本时间窗口操作
开发语言·flink·scala
网安INF1 小时前
CVE-2020-17519源码分析与漏洞复现(Flink 任意文件读取)
java·web安全·网络安全·flink·漏洞
一叶知秋哈1 小时前
Java应用Flink CDC监听MySQL数据变动内容输出到控制台
java·mysql·flink
jackson凌1 小时前
【Java学习笔记】SringBuffer类(重点)
java·笔记·学习
sclibingqing1 小时前
SpringBoot项目接口集中测试方法及实现
java·spring boot·后端
程序员JerrySUN1 小时前
全面理解 Linux 内核性能问题:分类、实战与调优策略
java·linux·运维·服务器·单片机