Spring-boot通过redisTemplate使用redis(无须手动序列化)
来源:互联网 发布:windows信息发布系统 编辑:程序博客网 时间:2024/06/10 14:41
redisTemplate的一些操作可以参考下面俩篇文章
http://blog.csdn.net/whatlookingfor/article/details/51863286
http://www.jianshu.com/p/7bf5dc61ca06
导入redis依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
Jackson依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.2</version></dependency>
配置application.yml配置文件
# REDIS (RedisProperties)spring: redis: database: 0 # Redis数据库索引(默认为0) host: localhost # Redis服务器地址 port: 6379 # Redis服务器连接端口 password: # Redis服务器连接密码(默认为空) timeout: 0 # 连接超时时间(毫秒) pool: max-active: 8 # 连接池最大连接数(使用负值表示没有限制) max-idle: 8 # 连接池中的最大空闲连接 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) min-idle: 0 # 连接池中的最小空闲连接
使redisTemplate无须手动序列化的类,并用jackson替换默认的序列化工具
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;/** * Created by zhengwei on 2017/10/17. */@Configurationpublic 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.afterPropertiesSet(); return redisTemplate; }}
开始使用
import com.example.entity.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class RedisApplicationTests { @Autowired private RedisTemplate<Object, Object> template; @Test public void contextLoads() { User user = new User(1,"象拔蚌"); template.opsForValue().set(user.getId()+"",user); //原本opsForValue()是只能操作字符串的.现在就可以操作对象了 User result = (User) template.opsForValue().get(user.getId()+""); System.out.println(result.toString()); }}
输出结果:
User{id=1, name='象拔蚌'}
剩下的很多操作就自己去百度探索吧
阅读全文
0 0
- Spring-boot通过redisTemplate使用redis(无须手动序列化)
- spring boot data RedisTemplate 序列化问题
- 深入理解Spring Redis的使用 (二)、RedisTemplate事务支持、序列化
- 深入理解Spring Redis的使用 (二)、RedisTemplate事务支持、序列化
- Spring集成Redis使用RedisTemplate
- REDIS学习(4)spring boot redisTemplate 对REDIS的简单封装,以及对引用包的说明,以及对序列化的详细说明
- spring boot中使用resid-RedisTemplate
- spring boot RedisTemplate的简单使用
- 使用Spring-Data-Redis存储对象(redisTemplate)
- spring与redis整合,redisTemplate的使用
- Spring-Data-Redis之RedisTemplate的使用
- 使用Spring-Data-Redis存储对象(redisTemplate)
- Spring Data Redis(RedisTemplate的使用)
- Spring-Data-Redis之RedisTemplate的使用
- redis-springboot-redistemplate更改序列化方式
- Spring boot 手动配置redis
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- spring boot 自学笔记(三) Redis集成—RedisTemplate
- 机器学习、深度学习的理论与实战入门建议整理
- 浅谈JAVA8新特性
- 欢迎使用CSDN-markdown编辑器
- Tensorflow_GPU_1
- libstdc++.so.6: version `GLIBCXX_3.4.20' not found
- Spring-boot通过redisTemplate使用redis(无须手动序列化)
- mongodb根据id获取时间____js
- Wi-Fi 爆重大安全漏洞,Android、iOS、Windows 等所有无线设备都不安全了
- 安卓端double取小数点后2位4种方法(都有bug)
- 查找操作------折半查找(二分查找)
- 前端基础HTML 笔记
- [二分 ST表 杂题] Codeforces875D. High Cry
- 快学Spark 2.0(新特性、含真实项目、纯Scala语言开发、CDH5.7) 分享下载地址——https://pan.baidu.com/s/1jHVvIAi 密码: sirk 本课程从基础开
- 面试总结——高级JAVA工程师(三)