在spring boot环境下有个StringRedisTemplate对象,默认已经为我们配置好了,只需要自动注入过来就能用,但是使用它只能在Redis中存放字符串。具体操作如下:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class Test {
@Autowired
private StringRedisTemplate stringRedisTemplate; @Test
public void test() throws Exception {
stringRedisTemplate.opsForValue().set("aaa", "111");
Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa")); }
}

因为在StringRedisTemplate的构造器中,设置的序列化器是字符串,所以它只能存取字符串。构造器:

public StringRedisTemplate() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
this.setKeySerializer(stringSerializer);
this.setValueSerializer(stringSerializer);
this.setHashKeySerializer(stringSerializer);
this.setHashValueSerializer(stringSerializer);
}

现在,如果我们想使用RedisTemplate存取对象,那我们只需要设置相应的序列化器就行了。操作如下:

package com.newegg.core.service.config;

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; @Configuration
public class RedisConfig { /**
* redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory); // 使用Jackson2JsonRedisSerialize 替换默认序列化
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;
}
}

接着我们来测试一下:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class Test {
@Autowired
private RedisTemplate<Object, Object> template; @Test
public void savereids() {
User u = new User(1, "王伟", 21);
template.opsForValue().set(u.getId() + "", u);
User result = (User) template.opsForValue().get(u.getId() + "");
System.out.println(result.toString());
} @Test
public void saveHashReids(){
for(int i=1;i<10;i++){
User u=new User(i,"王伟",21);
template.opsForHash().put("myCache",u.getId(),u);
}
ArrayList<User> list=template.opsForHash().values("myCache")
}
}

最新文章

  1. iOS----应用的旋转---Orientations
  2. iOS--关于同步下载
  3. python之环境搭建windows版
  4. (2) 深入理解Java Class文件格式(一)
  5. C++中引用的本质
  6. 《算法导论》习题解答 Chapter 22.1-4(去除重边)
  7. Python学习 之 爬虫
  8. storm的特性
  9. C#将十六进制的文本转换到整型数据
  10. 一个简单的shell脚本
  11. Oracle触发器(trigger):view,schema,database
  12. php内存分析
  13. poj1236 有向图加边变成强连通图
  14. adodb.RecordSet的属性和方法
  15. hihoCoder #1037 : 数字三角形 (动态规划)
  16. golang 六宫格、九宫格头像生成
  17. mysql 和 Oracle 数据类型对照
  18. PyQt5--QToggleButton
  19. (原创)用c++11打造类似于python的range
  20. wireshark in ubuntu

热门文章

  1. 【原】Python基础-函数
  2. Mac搭建C语言环境
  3. Mininet系列实验(四):基于Mininet测量路径的损耗率
  4. php接口数据安全解决方案(一)
  5. &quot;Could not resolve host: mirrorlist.centos.org; Unknown error&quot;解决方法
  6. Win10系统安装VMware-viclient-6.0无响应问题解决方法
  7. ISO/IEC 9899:2011 条款6.4.1——关键字
  8. php注册自动加载函数
  9. laravel门面DB返回数组配置
  10. eclipse 解决POM文件错误:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)