1.pom依赖

    <!-- 添加redis支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.application.properties配置

#redis数据库名称  从0到15,默认为db0
spring.redis.database=1
#redis服务器名称
spring.redis.host=127.0.0.1
#redis服务器密码
#spring.redis.password=123456
#redis服务器连接端口号
spring.redis.port=6379
#redis连接池设置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
#spring.redis.sentinel.master=
#spring.redis.sentinel.nodes=
spring.redis.timeout=60000

3.将 五种数据类型 注入到 Srping中

package com.xsjt.redis;
import org.springframework.beans.factory.annotation.Autowired;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* ClassName:RedisConfig Date: 2017年11月14日 下午3:39:34
* 将 五种数据类型 注入到 Spring容器中
* @author Joe
* @version
* @since JDK 1.8
* 参考地址:https://www.cnblogs.com/skyessay/p/6485187.html
*/
@Configuration
public class RedisConfig { // 注入 RedisConnectionFactory
@Autowired
private RedisConnectionFactory redisConnectionFactory; @Bean
public RedisTemplate<String, Object> functionDomainRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
return redisTemplate;
} /**
* 设置数据存入 redis 的序列化方式
* @param redisTemplate
* @param factory
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
} /**
* 实例化 HashOperations 对象,可以使用 Hash 类型操作
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
} /**
* 实例化 ValueOperations 对象,可以使用 String 操作
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
} /**
* 实例化 ListOperations 对象,可以使用 List 操作
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
} /**
* 实例化 SetOperations 对象,可以使用 Set 操作
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
} /**
* 实例化 ZSetOperations 对象,可以使用 ZSet 操作
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}

4.封装String数据类型的方法

package com.xsjt.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
/**
* ClassName: StringRedisUtil
* String 数据类型
* date: 2017年11月14日 下午8:15:07
* @author Joe
* @version
* @since JDK 1.8
*/
@Component("stringRedis")
public class StringRedisUtil { @Autowired
private ValueOperations<String, Object> redisTemplate; /**
* set:(保存数据).
* @author Joe
* Date:2017年11月14日下午8:15:01
* @param key
* @param value
*/
public void set(String key, String value){
redisTemplate.set(key, value);
} /**
* get:(得到数据).
* @author Joe
* Date:2017年11月14日下午8:15:38
* @param key
* @return
*/
public Object get(String key) {
return redisTemplate.get(key);
} // 可自行扩展其他方法
}

5.封装Hash数据类型的方法

package com.xsjt.redis;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; /**
* ClassName:HashRedisUtil Date: 2017年11月14日 下午8:17:47
* Hash 数据类型
* @author Joe
* @version
* @param <T>
* @since JDK 1.8
*/
@Component("hashRedis")
public class HashRedisUtil<T> { @Autowired
protected RedisTemplate<String, Object> redisTemplate;
@Resource
protected HashOperations<String, String, Object> hashOperations; /**
* put:(添加).
* @param key
* @param hashKey
* @param doamin value
* @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间
*/
public void put(String key, String hashKey, T doamin, long expire) {
hashOperations.put(key, hashKey, doamin);
if (expire != -1) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
} /**
* remove:( 删除).
* @param key
* @param hashKey
*/
public void remove(String key, String hashKey) {
hashOperations.delete(key, hashKey);
} /**
* get:(查询).
* @param key
* @param hashKey
* @return
*/
public Object get(String key, String hashKey) {
return hashOperations.get(key, hashKey);
} /**
* getAll:(获取当前redis库下所有对象).
* @param key
* @return
*/
public List<Object> getAll(String key) {
return hashOperations.values(key);
} /**
* getKeys:(查询查询当前redis库下所有key).
* @param key
* @return
*/
public Set<String> getKeys(String key) {
return hashOperations.keys(key);
} /**
* isKeyExists:(判断key是否存在redis中).
* @param key
* @param hashKey
* @return
*/
public boolean isKeyExists(String key, String hashKey) {
return hashOperations.hasKey(key, hashKey);
} /**
* count:(查询当前key下缓存数量).
* @param key
* @return
*/
public long count(String key) {
return hashOperations.size(key);
}
}

6.测试类

package com.xsjt.redis;
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.test.context.junit4.SpringRunner;
/**
* ClassName:TestRedis
* Date: 2017年11月14日 下午8:09:54
* @author Joe
* @version
* @since JDK 1.8
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis { /********************************测试String***********************************/ @Autowired
private StringRedisUtil stringRedis; @Test
public void setString() {
stringRedis.set("name", "张三");
} @Test
public void getString() {
Object value = stringRedis.get("name");
System.out.println("value=" + value);
} /**********************************测试Hash************************************/ @Autowired
private HashRedisUtil<Object> hashRedisUtil; @Test
public void setHash() {
hashRedisUtil.put("user", "userName", new Integer(6868), 5);
} @Test
public void getHash() {
Integer a = (Integer) hashRedisUtil.get("user", "userName");
System.out.println("a==" + a);
}
}

7.源码下载

  https://gitee.com/xbq168/spring-boot-learn

SpringBoot(十二)-- 整合Redis的更多相关文章

  1. SpringBoot进阶教程(二十九)整合Redis 发布订阅

    SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...

  2. SpringBoot进阶教程(二十八)整合Redis事物

    Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...

  3. SpringBoot进阶教程(二十六)整合Redis之共享Session

    集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...

  4. SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用

    在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...

  5. SpringBoot进阶教程(二十四)整合Redis

    缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓存的场合非 ...

  6. Activiti7整合SpringBoot(十二)

    1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...

  7. Spring Boot (二) 整合 Redis

    前言 本文将会基于 springboot 2.1.8.RELEASE 简单整合 Redis ,适合新手小白入门 Spring Boot 整合 Redis 入门 1.pom.xml 中引入 redis ...

  8. 【快学springboot】11.整合redis实现session共享

    前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...

  9. redis之(十二)redis数据的持久化

    [一]redis的数据为什么要持久化 --->redis的存取数据性能高,是由于将所有数据都存储在内存中.当redis重启的时候,存储在内存中的数据就容易丢失. --->把redis作为数 ...

  10. springboot(十二)-分布式锁(redis)

    什么是分布式锁? 要介绍分布式锁,首先要提到与分布式锁相对应的是线程锁.进程锁. 线程锁:主要用来给方法.代码块加锁.当某个方法或代码使用锁,在同一时刻仅有一个线程执行该方法或该代码段.线程锁只在同一 ...

随机推荐

  1. R语言进行数据预处理

    R语言进行数据预处理wranging li_volleyball 2016年3月22日 data wrangling with Rpackages:tidyr dplyr Ground rules l ...

  2. 【协议学习】SIP基本场景分析

    1.SIP业务基本知识 1.1 业务介绍 会话初始协议(Session Initiation Protocol)是一种信令协议,用于初始.管理和终止网络中的语音和视频会话,具体地说就是用来生成.修改和 ...

  3. Axiom3D写游戏:用Overlay实现Mesh浏览.

    从网上找了些资源,大多搜Ogre,Mesh资源,然后为了方便查看各个Mesh,以及对应骨骼动画.为了实用性,考虑放在原游戏窗口里实现.最开始打算窗口新建viewport来实现,后发现这种方式的局限性, ...

  4. Mybatis系列(一):Mybatis入门

    一.Mybatis是什么 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改 ...

  5. 联想服务器X3650 M2 配置 RAID5 + 热备盘

    实验环境: 1.  服务器型号联想 System X3650 M2 2.  六块300G  SAS硬盘 实验目的: 配置RAID 5 ,搭建重要文件备份服务器. 标注:本教程六块硬盘,其中五块硬盘做R ...

  6. sublime3 docblocker插件定制自己的注释,配置步骤

    DocBlockr很好用,不仅仅可以自动生成注释,还可以手动编辑注释的格式. 安装方法:   Cmd+Shift+P -> Install Package -> docblockr  wi ...

  7. ZeroClipboard插件——复制到剪切板

    ZeroClipboard是一个轻量级的jQuery“复制到剪贴板”插件采用了时下流行的零剪贴板库.官网:http://www.steamdev.com/zclip 参数及默认值path(必选)  Z ...

  8. jsTree 插件

    html代码 <div id="jstree1"></div> js代码: <script src="__STATIC__/h5/js/jq ...

  9. Android 布局学习之——Layout(布局)详解一

    layout(布局)定义了用户界面的可视化结构(visual structure),如Activity的UI,应用窗口的UI. 有两种方式声明layout: 1.在xml文件中声明UI组件. 2.在运 ...

  10. Postman测试api@RequestBody接收参数

    api postman 结果 { , "message": "Content type 'multipart/form-data;boundary=----------- ...