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());
    }
}
相关推荐
皮皮林55117 分钟前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
weixin_4569042731 分钟前
Spring Boot 用户管理系统
java·spring boot·后端
趁你还年轻_31 分钟前
异步编程CompletionService
java
DKPT35 分钟前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
sibylyue40 分钟前
Guava中常用的工具类
java·guava
奔跑吧邓邓子43 分钟前
【Java实战㉞】从0到1:Spring Boot Web开发与接口设计实战
java·spring boot·实战·web开发·接口设计
专注API从业者1 小时前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python
奔跑吧邓邓子1 小时前
【Java实战㉝】Spring Boot实战:从入门到自动配置的进阶之路
java·spring boot·实战·自动配置
ONLYOFFICE1 小时前
【技术教程】如何将ONLYOFFICE文档集成到使用Spring Boot框架编写的Java Web应用程序中
java·spring boot·编辑器
叫我阿柒啊1 小时前
Java全栈开发工程师的实战面试经历:从基础到微服务
java·微服务·typescript·vue·springboot·前端开发·后端开发