博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot整合Redis
阅读量:5039 次
发布时间:2019-06-12

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

偷懒了几天,好几天没写springboot了。真的不是没什么可写,是因为坚持做一件事真的很难。

今天抽空弄了一个springboot整合redis的小例子。

首先做好准备工作,在本地安装一个redis,具体步骤可以自行百度,然后启动redis。出现下图页面就启动成功了。

162d69598a76311a?w=980&h=730&f=jpeg&s=133100

然后新建项目,加入redis依赖,pom文件如下:

4.0.0
com.dalaoyang
springboot_redis
0.0.1-SNAPSHOT
jar
springboot_redis
springboot_redis
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin

然后在application.properties加入redis配置:

##端口号server.port=8888# Redis数据库索引(默认为0)spring.redis.database=0 # Redis服务器地址spring.redis.host=localhost# Redis服务器连接端口spring.redis.port=6379 # Redis服务器连接密码(默认为空)spring.redis.password=#连接池最大连接数(使用负值表示没有限制)spring.redis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制)spring.redis.pool.max-wait=-1 # 连接池中的最大空闲连接spring.redis.pool.max-idle=8 # 连接池中的最小空闲连接spring.redis.pool.min-idle=0 # 连接超时时间(毫秒)spring.redis.timeout=0

RedisConfig配置类,其中@EnableCaching开启注解

package com.dalaoyang.config;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.config * @email yangyang@dalaoyang.cn * @date 2018/4/18 */@Configuration@EnableCaching//开启缓存public class RedisConfig extends CachingConfigurerSupport {    @Bean    public CacheManager cacheManager(RedisTemplate
redisTemplate) { CacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager; } @Bean public RedisTemplate
redisTemplate(RedisConnectionFactory factory) { RedisTemplate
redisTemplate = new RedisTemplate
(); redisTemplate.setConnectionFactory(factory); return redisTemplate; }}

由于只是简单整合,我只创建了一个RedisService来用来存取缓存数据,实际项目中可以根据需求创建interface,impl等等,代码如下:

package com.dalaoyang.service;import javax.annotation.Resource;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.stereotype.Service;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.service * @email yangyang@dalaoyang.cn * @date 2018/4/18 */@Servicepublic class RedisService {    @Resource    private RedisTemplate
redisTemplate; public void set(String key, Object value) { //更改在redis里面查看key编码问题 RedisSerializer redisSerializer =new StringRedisSerializer(); redisTemplate.setKeySerializer(redisSerializer); ValueOperations
vo = redisTemplate.opsForValue(); vo.set(key, value); } public Object get(String key) { ValueOperations
vo = redisTemplate.opsForValue(); return vo.get(key); }}

实体类City:

package com.dalaoyang.entity;import java.io.Serializable;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.Entity * @email 397600342@qq.com * @date 2018/4/7 */public class City implements Serializable {    private int cityId;    private String cityName;    private String cityIntroduce;    public City(int cityId, String cityName, String cityIntroduce) {        this.cityId = cityId;        this.cityName = cityName;        this.cityIntroduce = cityIntroduce;    }    public City(String cityName, String cityIntroduce) {        this.cityName = cityName;        this.cityIntroduce = cityIntroduce;    }    public City() {    }    public int getCityId() {        return cityId;    }    public void setCityId(int cityId) {        this.cityId = cityId;    }    public String getCityName() {        return cityName;    }    public void setCityName(String cityName) {        this.cityName = cityName;    }    public String getCityIntroduce() {        return cityIntroduce;    }    public void setCityIntroduce(String cityIntroduce) {        this.cityIntroduce = cityIntroduce;    }}

测试类CityController

package com.dalaoyang.controller;import com.dalaoyang.entity.City;import com.dalaoyang.service.RedisService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * @author dalaoyang * @Description * @project springboot_learn * @package com.dalaoyang.controller * @email 397600342@qq.com * @date 2018/4/7 */@RestControllerpublic class CityController {    @Autowired    private RedisService redisService;    //http://localhost:8888/saveCity?cityName=北京&cityIntroduce=中国首都&cityId=1    @GetMapping(value = "saveCity")    public String saveCity(int cityId,String cityName,String cityIntroduce){        City city = new City(cityId,cityName,cityIntroduce);        redisService.set(cityId+"",city);        return "success";    }    //http://localhost:8888/getCityById?cityId=1    @GetMapping(value = "getCityById")    public City getCity(int cityId){        City city = (City) redisService.get(cityId+"");        return city;    }}

到这里配置基本上都完成了,然后启动项目访问http://localhost:8888/saveCity?cityName=北京&cityIntroduce=中国首都&cityId=18

162d69d28543f5a6?w=2376&h=632&f=jpeg&s=243162

发现报错了,看了一下后台,如下

162d69d7c8c07b71?w=2432&h=738&f=jpeg&s=654839

发现是实体类没有序列化,然后给City类序列化,然后访问http://localhost:8888/saveCity?cityName=北京&cityIntroduce=中国首都&cityId=18发现这次成功了。

然后查看redis,发现key值的编码不对

162d6a378440f8aa?w=666&h=336&f=jpeg&s=91551

在RedisService中加入

//更改在redis里面查看key编码问题        RedisSerializer redisSerializer =new StringRedisSerializer();        redisTemplate.setKeySerializer(redisSerializer);

在查看redis的key发现编码正确了

162d6a434427cfc5?w=424&h=252&f=jpeg&s=56347

源码下载 :

个人网站:

转载于:https://www.cnblogs.com/dalaoyang/p/8873866.html

你可能感兴趣的文章
Web框架和Django基础
查看>>
python中的逻辑操作符
查看>>
CSS兼容性常见问题总结
查看>>
HDU 1548 A strange lift (Dijkstra)
查看>>
每天一个小程序—0005题(批量处理图片大小)
查看>>
C# 启动进程和杀死进程
查看>>
tcp实现交互
查看>>
IIS的各种身份验证详细测试
查看>>
JavaScript特效源码(3、菜单特效)
查看>>
聊聊、Zookeeper Linux 单服务
查看>>
Linux常用命令总结
查看>>
KRPano动态热点专用素材图50多个,加动态热点使用方法
查看>>
yii模型ar中备忘
查看>>
C#线程入门
查看>>
CSS清除浮动方法
查看>>
JVM内存回收机制简述
查看>>
洛咕 P2480 [SDOI2010]古代猪文
查看>>
js-创建对象的几种方式
查看>>
JDK JRE Java虚拟机的关系
查看>>
2018.11.20
查看>>