博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)SpringBoot系列—Redis使用
阅读量:6940 次
发布时间:2019-06-27

本文共 5484 字,大约阅读时间需要 18 分钟。

http://blog.csdn.net/jeofey/article/details/73468192

SpringBoot对的支持是通过 Data  来时闲的,使用之前引入jar包:

compile('org.springframework.boot:spring-boot-starter-data-redis')

org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration为我们默认配置了JedisConnectionFactory、redisTemplate以及stringRedisTemplate,让我们直接可以使用redis作为数据存储。

org.springframework.boot.autoconfigure.data.redis.RedisProperties给我们展示了可以使用以“spring.redis”为前缀的属性在application.yml中配置redis。

例如,默认为db0,在配置文件中将其设置为db1

 

spring:  redis:    database: 1

 

1.在下载redis镜像,并运行容器

2.新建对象User

 

package com.example.demo.bean;import java.io.Serializable;/** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-19 14:15. */public class User implements Serializable{    private static final long serialVersionUID = 934073895746700367L;    private String id;    private String name;    private Integer age;    public User(Integer age, String id, String name) {        super();        this.age = age;        this.id = id;        this.name = name;    }    get,set方法省略....}
3.数据访问

 

 

package com.example.demo.dao;import com.example.demo.bean.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Repository;import javax.annotation.Resource;/** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-19 14:17. */@Repositorypublic class UserDao {    @Autowired    StringRedisTemplate stringRedisTemplate;    @Resource(name = "stringRedisTemplate")    ValueOperations
valOpsStr; @Autowired RedisTemplate
redisTemplate; @Resource(name = "redisTemplate") ValueOperations
valOps; public void stringRedisTemplateDemo(){ valOpsStr.set("xx","yy"); } public void save(User user){ valOps.set(user.getId(),user); } public String getString(){ return valOpsStr.get("xx"); } public User getUser(String id){ return (User) valOps.get(id); }}
4.控制器,用userDao或RedisTemplate存取数据

 

 

package com.example.demo.controller;import com.example.demo.bean.User;import com.example.demo.dao.UserDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-19 14:26. */@RestControllerpublic class RedisDataController {    @Autowired    UserDao userDao;    @Autowired    RedisTemplate
redisTemplate; @RequestMapping("/set") public void set(){ User user = new User(32,"1","gg"); userDao.save(user); userDao.stringRedisTemplateDemo(); redisTemplate.opsForValue().set("22",user); } @RequestMapping("/get1") public String getString(){ return userDao.getString(); } @RequestMapping("get2") public User getUser(String id){ return userDao.getUser(id); }}

 

浏览器调用http://127.0.0.1:8082/set接口存数据

客户端查看

 

 

接口查看:

客户端看到的数据是“乱码”,因为序列化引起,解决办法:自己配置redistemplate并定义Serializer

RedisTemplate使用的是JdkSerizationRedisSerializer,使用二级制形式存储数据,对于演示redis client不直观。

 

package com.example.demo.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;import java.net.UnknownHostException;/** * SpringBootDemo1 * Created by xian.juanjuan on 2017-6-19 15:13. */@Configurationpublic class RedisConfig {    @Bean    public RedisTemplate
redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException{ RedisTemplate
redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); 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); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; }}
设置value的序列化采用Jackson2JsonRedisSerializer,key的序列化采用StringRedisSerializer

 

重启项目,执行http://127.0.0.1:8082/set接口

客户端查看:

接口查看:

 

序列化问题_no suitable constructor found

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.example.demo.bean.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

 at [Source: [B@3fbfca59; line: 1, column: 32]

User类使用时间序列化接口,使用Jackson序列化需要一个空构造,为什么?

 

public User() {}
重启项目,接口访问正常~

 

 
 
你可能感兴趣的文章
【Hibernate学习笔记-5】@Formula注解的使用
查看>>
java ClassLoader static
查看>>
公司交换机arp 绑定操作
查看>>
东大oj-1511: Caoshen like math
查看>>
在.NET4.5项目中添加HttpClient引用的办法
查看>>
[Android]基于RxJava、RxAndroid的EventBus实现
查看>>
八排序算法
查看>>
.NET/MVC-ViewBag、ViewData、TempData区别
查看>>
Android开发之Bitmap.Config.RGB_565
查看>>
android 实现2张图片层叠效果
查看>>
BI项目记笔记索引
查看>>
OpenCV-CL: OpenCL加速计算机视觉技术
查看>>
用SignalR做类似QQ登录的应用
查看>>
同步github工程gitcafe
查看>>
[转]用C#实现的条形码和二维码编码解码器
查看>>
_tcscat在Debug和Release根据问题
查看>>
LinearLayout 垂直滚动条
查看>>
7月19日Docker&Kubernetes技术沙龙总结 - DockOne.io
查看>>
【扩展知识3】一些困难的数组
查看>>
如何在Ubuntu 14.04中安装最新版Eclipse
查看>>