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());
    }
}
相关推荐
IT 行者12 分钟前
GitHub Spec Kit 实战(五):/speckit.tasks 怎么拆——Spec Kit 五部曲收官
java·ai编程·claude
AC赳赳老秦19 分钟前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw
提笔了无痕21 分钟前
如何用Go实现整套RAG流程
开发语言·后端·golang
(Charon)23 分钟前
【C++ 面试高频基础:指针、引用、const、static、new/delete 总结】
java·开发语言
Yeats_Liao41 分钟前
Feed流系统设计(三):数据模型与存储设计,从表结构到Redis收件箱
java·javascript·redis
JiaHao汤1 小时前
分布式事务方案全景:从理论到 Seata 落地
java·分布式·spring·spring cloud
2601_961875241 小时前
法考考试时间安排及科目|时间表|资料已整理
开发语言·c#·inverted-index·suffix-tree·sstable·r-tree·lsm-tree
AI科技星1 小时前
数术工坊第八卷:算力革命
c语言·开发语言·网络·量子计算·agi
geovindu1 小时前
go: Generators Pattern
开发语言·后端·设计模式·golang·生成器模式
色空大师2 小时前
【debug调试详解-idea】
java·ide·intellij-idea·调试·远程调试