引入依赖
org.springframework.boot spring-boot-starter-data-redis
@Test
void testHash2() throws JsonProcessingException {
Blog blog = new Blog();
ValueOperations vo = redisTemplate.opsForValue();
vo.set("blog", blog);
System.out.println(vo.get("blog"));
//=====
HashOperations ho = redisTemplate.opsForHash();
ObjectMapper objectMapper = new ObjectMapper();
blog.setId(11L);
blog.setTitle("mapp");
String jsonStr = objectMapper.writevalueAsString(blog);//对象转成json字符串
Map map = objectMapper.readValue(jsonStr, Map.class);//字符串转成map
ho.putAll("blog-map", map);
// ho.put("blog-map", "title", "mapp");
System.out.println(ho.entries("blog-map"));
}
修改默认的序列化规则:
package com.jt.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
@Configuration
public class RedisConfig {
//配置json序列化
public RedisSerializer jsonSerializer() {
//定义json序列化 反序列化对象
Jackson2JsonRedisSerializer



