<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
@Autowired RedisTemplate redisTemplate; @Autowired StringRedisTemplate stringRedisTemplate; @Autowired RedisTemplate<Object, Object> myRedisTemplate; @Test void test() { //stringRedisTemplate.opsForValue().set("msg","hello,word"); Employee empId = employeeService.getEmpId(1); myRedisTemplate.opsForValue().set("emp-01",empId); }
@Configuration public class MyRedisConfig { @Bean public RedisTemplate<Object, Object> myRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer(Object.class); template.setDefaultSerializer(serializer); return template; } }
public class Employee implements Serializable
原文:https://www.cnblogs.com/shunWcs/p/14912416.html