二:添加Redis依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>

  

三:添加Redis配置信息

application.properties中添加

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=0
spring.redis.password=

  

四:创建RedisConfigurer

package com.example.demo.core.configurer;

import org.springframework.boot.context.properties.ConfigurationProperties;
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.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig; /**
* @author
* @Description: redis配置
* @date 2018/4/30 10:28
*/
@Configuration
@EnableCaching //开启缓存
public class RedisConfigurer extends CachingConfigurerSupport { @Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
} @Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory getConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
} @Bean
public RedisTemplate<?, ?> getRedisTemplate(){
RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());
return template;
}
}

  

五:创建Redis常用方法

RedisService

package com.example.demo.service;

import java.util.List;

/**
* @author 张瑶
* @Description: redis常用方法
* @date 2018/4/30 10:35
*/
public interface RedisService { /**
* 设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。
* @param key
* @param value
* @return
*/
boolean set(String key, String value); /**
* 获取指定 key 的值。如果 key 不存在,返回 nil 。如果key 储存的值不是字符串类型,返回一个错误。
* @param key
* @return
*/
String get(String key); /**
* 设置 key 的过期时间。key 过期后将不再可用。
* @param key
* @param expire
* @return
*/
boolean expire(String key, long expire); /**
* 存集合
* @param key
* @param list
* @param <T>
* @return
*/
<T> boolean setList(String key, List<T> list); /**
* 取集合
* @param key
* @param clz
* @param <T>
* @return
*/
<T> List<T> getList(String key, Class<T> clz); /**
* 将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。
* 当 key 存在但不是列表类型时,返回一个错误。
* @param key
* @param obj
* @return
*/
long lpush(String key, Object obj); /**
* 将一个或多个值插入到列表的尾部(最右边)。
* 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。
* @param key
* @param obj
* @return
*/
long rpush(String key, Object obj); /**
* 移除并返回列表的第一个元素。
* @param key
* @return
*/
String lpop(String key); /**
* 删除已存在的键。不存在的 key 会被忽略。
* @param key
* @return
*/
long del(final String key);
}

  

RedisServiceImpl

package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.service.RedisService;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit; /**
* @author 张瑶
* @Description: redis配置
* @date 2018/4/30 10:38
*/
@Service
public class RedisServiceImpl implements RedisService { @Resource
private RedisTemplate<String, ?> redisTemplate; @Override
public boolean set(final String key, final String value) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
} @Override
public String get(final String key){
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
} @Override
public long del(final String key){
long result = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
long value = connection.del(serializer.serialize(key));
return value;
}
});
return result;
} @Override
public boolean expire(final String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
} @Override
public <T> boolean setList(String key, List<T> list) {
String value = JSON.toJSONString(list);
return set(key,value);
} @Override
public <T> List<T> getList(String key,Class<T> clz) {
String json = get(key);
if(json!=null){
List<T> list = JSON.parseArray(json, clz);
return list;
}
return null;
} @Override
public long lpush(final String key, Object obj) {
final String value = JSON.toJSONString(obj);
long result = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));
return count;
}
});
return result;
} @Override
public long rpush(final String key, Object obj) {
final String value = JSON.toJSONString(obj);
long result = redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
long count = connection.rPush(serializer.serialize(key), serializer.serialize(value));
return count;
}
});
return result;
} @Override
public String lpop(final String key) {
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] res = connection.lPop(serializer.serialize(key));
return serializer.deserialize(res);
}
});
return result;
} }

  

六:接口测试

创建RedisController

package com.example.demo.controller;

import com.example.demo.core.ret.RetResponse;
import com.example.demo.core.ret.RetResult;
import com.example.demo.service.RedisService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /**
* @author 张瑶
* @Description:
* @date 2018/4/30 11:28
*/
@RestController
@RequestMapping("redis")
public class RedisController { @Resource
private RedisService redisService; @PostMapping("/setRedis")
public RetResult<String> setRedis(String name) {
redisService.set("name",name);
return RetResponse.makeOKRsp(name);
} @PostMapping("/getRedis")
public RetResult<String> getRedis() {
String name = redisService.get("name");
return RetResponse.makeOKRsp(name);
} }

  

(九)SpringBoot整合redis框架的更多相关文章

  1. 九、springboot整合redis二之缓冲配置

    1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSu ...

  2. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  3. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  4. SpringBoot整合Redis并完成工具类

    SpringBoot整合Redis的资料很多,但是我只需要整合完成后,可以操作Redis就可以了,所以不需要配合缓存相关的注解使用(如@Cacheable),而且我的系统框架用的日志是log4j,不是 ...

  5. SpringBoot整合Redis及Redis工具类撰写

            SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...

  6. SpringBoot系列十:SpringBoot整合Redis

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...

  7. springboot整合redis(注解形式)

    springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...

  8. springboot整合redis(简单整理)

    Redis安装与开启 我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/ ...

  9. SpringBoot整合redis哨兵主从服务

    前提环境: 主从配置 http://www.cnblogs.com/zwcry/p/9046207.html 哨兵配置 https://www.cnblogs.com/zwcry/p/9134721. ...

随机推荐

  1. 【转载】一致性哈希算法(consistent hashing)

    一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈 ...

  2. Material Design (四),AppBarLayout的使用

    前言  AppBarLayout,顾名知意.就是用来给AppBar布局的容器,是LinearLayout的子类.而AppBar就包括我们通常所知道的ActionBar,Toolbar. AppBarL ...

  3. 判断IPv6地址合法性

    在 <netinet/in.h> 头文件下有下列这些宏用于判断IPv6地址合法性 返回0代表true,返回非零值代表ipv6地址为非指定类型的的地址(false) int IN6_IS_A ...

  4. DataFactory 5.6注册码

    一.DataFactory 5.6注册码 数据工厂5.6注册码如下,希望能帮助需求之人 AuthKey: 0-87093-23830-05141-17507 SiteMsg: FREELAND EVO ...

  5. Devices下设备的进程显示为问号的问题

     adb shell getprop ro.debuggable      返回  ro.debuggable=0     说明这个机子的版本不是userdebug版本,是user版本     只有这 ...

  6. HDOJ1006

    #include <cstdio>#include <algorithm>using namespace std;const double UB=43200;const dou ...

  7. armel、armhf和arm64

    1 这些名词是什么的缩写 1.1 armel 是arm eabi little endian的缩写.eabi是软浮点二进制接口,这里的e是embeded,是对于嵌入式设备而言. 1.2 armhf 是 ...

  8. Maven group, artifact or version defined in the pom file do not match the file ...

    在把library上传到bintray空间的时候报以下错误 Could not upload to 'https://api.bintray.com/content/ping/maven/comm-a ...

  9. HDU3338 Kakuro Extension —— 最大流、方格填数类似数独

    题目链接:https://vjudge.net/problem/HDU-3338 Kakuro Extension Time Limit: 2000/1000 MS (Java/Others)     ...

  10. HDU2089 不要62 —— 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 不要62 Time Limit: 1000/1000 MS (Java/Others)    M ...