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());
    }
}
相关推荐
Doker 多克1 分钟前
Spring Integration AMQP 和 Spring AMQP详解
java·后端·spring
NE_STOP8 分钟前
Spring AI Alibaba -- 文生图、文生音及向量化
java
知行EDI11 分钟前
eMudhra CA 私钥安全事件:Java 运行环境(JRE)风险评估与修复指南
java·emudhra
郑同学的笔记12 分钟前
【c++随笔30】C++ 注册回调函数
开发语言·c++
旋律41415 分钟前
什么是抽象类与接口
java
七夜zippoe27 分钟前
启元 Q1 腿部运动学逆解:Python 算站立下蹲关节角
开发语言·python·ai·机器人·腿部运动学
旋律41428 分钟前
String、StringBuilder、StringBuffer:三个"字符串",到底区别在哪?
java·后端
熊猫钓鱼>_>31 分钟前
精准碰一碰近场分享与拖拽投送实战:把文件“碰“进另一台设备【鸿蒙心迹】
开发语言·华为·harmonyos·鸿蒙·openharmony
一条泥憨鱼1 小时前
【Redis | 黑马点评】基于Redis的消息队列 | stream消息队列 源码解读
java·数据库·redis·缓存·lua
CodeGL1 小时前
9、Python基础之IO编程
开发语言·数据库·python