1、基于maven管理的Java项目,需要在pom文件中加入redis所需要的的依赖
<!-- redis 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--集成redis所需common-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<!--redis 对象序列化器-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2、新建一个配置类RedisConfig,实现键值的序列化
import com.fasterxml.jackson.annotation.JsonAutoDetect;
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.StringRedisSerializer;
/**
* @author: SUN
* @version: 1.0
* @date: 2019/12/30 10:22
* @description: redis配置类 实现key和value的序列化
*/
@Configuration
public class RedisConfig {
/**
* @param redisConnectionFactory
* @return 自定义redisTemplate,自带的bean没有序列化器
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
//将数据信息转化为json格式 redis服务器中可以查看数据信息
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置value的序列化规则和 key的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
3、通过测试类实现本地实体类或集合对象的存储和读取
import com.touchspring.ssdt.rfid.domain.entity.sys.SysWorker;
import com.touchspring.ssdt.rfid.repository.sys.SysWorkerRepository;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.List;
/**
* @author: SUN
* @version: 1.0
* @date: 2019/12/30 11:13
* @description:
*/
public class newTest extends TmallApplicationTests {
@Autowired
private SysWorkerRepository sysWorkerRepository;
@Autowired
private RedisTemplate redisTemplate;
@Test
// @Ignore("not ready yet") 此注解将不再执行方法内容
public void testRedisPush() {
List<SysWorker> sysWorkerList = sysWorkerRepository.findAll();
SysWorker sysWorker = sysWorkerList.get(0);
// ValueOperations是操作简单的value例如String工具类
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("workerKey", sysWorker);
// HashOperations是操作value为Map的工具类
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("workerListKey", "1", sysWorkerList);
}
@Test
public void testRedisGet() {
// 根据key值读取对象
ValueOperations valueOperations = redisTemplate.opsForValue();
SysWorker sysWorker = (SysWorker) valueOperations.get("workerKey");
System.out.println("- - - - " + sysWorker.toString());
// 根据key值读取对象
HashOperations hashOperations = redisTemplate.opsForHash();
List<SysWorker> sysWorkers = (List<SysWorker>) hashOperations.get("workerListKey", "1");
System.out.println("- - - - " + sysWorkers.toString());
}
}
4、redis存储如下
- (1)ValueOperations
- (2)HashOperations
5、存储到redis数据中如果包括日期(LocalDateTime),则从redis读取解析数据时可能存在报错,无法实现LocalDateTime日期的反序列化操作,则需要根据以下方式在日期属性上添加注解进行处理
- 1、LocalDateTime属性加上注解
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
public class Demo {
private Long id;
private String name;
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime time;
......
}
- 2、若未进行处理,在redis中存储的LocalDateTime格式为
{
"@class": "com.karmay3d.Demo",
"id": 10000000001,
"name": "测试序列化",
"time": {
"dayOfMonth": 15,
"dayOfWeek": "TUESDAY",
"dayOfYear": 227,
"month": "AUGUST",
"monthValue": 8,
"year": 2017,
"hour": 14,
"minute": 45,
"second": 51,
"nano": 921000000,
"chronology": {
"@class": "java.time.chrono.IsoChronology",
"id": "ISO",
"calendarType": "iso8601"
}
}
}
- 3、使用注解处理后 redis再次存入之后结构
{
"@class": "com.karmay3d.Demo",
"id": 10000000001,
"name": "测试序列化",
"time": [2017,8,15,14,57,37,525000000]
}
- 4、之后反序列化就可以取出实体对象了。