设为首页 收藏本站
查看: 1460|回复: 0

[经验分享] (转)SpringBoot系列

[复制链接]

尚未签到

发表于 2017-12-21 19:34:43 | 显示全部楼层 |阅读模式
  SpringBoot对Redis的支持是通过spring Data redis 来时闲的,使用之前引入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.在Docker下载redis镜像,并运行容器
  2.新建对象User
  

package com.example.demo.bean;  

  
import java.io.Serializable;
  

  
/**
  
* SpringBootDemo1
  
* Created by xian.juanjuan on 2017-6-19 14:15.
  
*/

  
public>  

  
private static final long serialVersionUID = 934073895746700367L;

  
private String>  
private String name;
  
private Integer age;
  


  
public User(Integer age, String>  
super();
  
this.age = age;

  
this.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.
  
*/
  
@Repository

  
public>  

  
@Autowired
  
StringRedisTemplate stringRedisTemplate;
  

  
@Resource(name = "stringRedisTemplate")
  
ValueOperations<String, String> valOpsStr;
  

  
@Autowired
  
RedisTemplate<Object, Object > redisTemplate;
  

  
@Resource(name = "redisTemplate")
  
ValueOperations<Object, Object> 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>  
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.
  
*/
  
@RestController

  
public>  

  
@Autowired
  
UserDao userDao;
  

  
@Autowired
  
RedisTemplate<Object,Object> 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>  
return userDao.getUser(id);
  
}
  
}
  

  

  浏览器调用http://127.0.0.1:8082/set接口存数据
  客户端查看

DSC0000.png
DSC0001.jpg
  

  接口查看:
DSC0002.png

  客户端看到的数据是“乱码”,因为序列化引起,解决办法:自己配置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.
  
*/
  
@Configuration

  
public>  

  
@Bean
  
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException{
  
RedisTemplate<Object, Object> 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接口
  客户端查看:
DSC0003.png

  接口查看:
DSC0004.png


  序列化问题_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() {  
}
  
重启项目,接口访问正常~

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426587-1-1.html 上篇帖子: C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远 下篇帖子: redis 在 php 中的应用(Sorted-set篇)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表