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. CSS遮罩层的实现

    偶然发现自己原来写了一个CSS遮罩层,虽然这个东西没什么技术含量,但如果本人离开公司后又遇见此类问题,那么可能又得花些时间来找资料了.所以决定还是把它记下来吧. 直接上代码吧. 第一步,html代码: ...

  2. qt 字体的相关问题

    (一)qtconfig字体列表不全的问题? 发现界面的上的文字不能正常显示,后调用qtconfig发现里面识别的字体非常少,怀疑是编译的时候参数未能设置正确,于是经过多次试验,终于成功找到根结所在,. ...

  3. 自然语言交流系统 phxnet团队 创新实训 个人博客 (六)

    讯飞的语音sdk是需要申请的,地址是:http://dev.voicecloud.cn/developer.php?vt=1 .申请一个讯飞的开发者账号,然后申请一个appid,申请的时候需要填写开发 ...

  4. android SpannableString使用详解

    /** * 超链接 */ private void addUrlSpan() { SpannableString spanString = new SpannableString("超链接& ...

  5. thinkphp 对百度编辑器里的内容进行保存

    模板代码 <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="U ...

  6. 关于Unity的NGUI

    NGUI是严格遵循KISS原则并用C#编写的Unity(适用于专业版和免费版)插件,提供强大的UI系统和事件通知框架 KISS原则:Keep It Simple,Stupid NGUI实例 1.创建U ...

  7. Axiom3D:Ogre公告板集与合并批次

    在上文中,我们把Ogre里的网格分解成点线面后,我们要完成一个新的功能,在点上突出显示. 得到顶点位置后,这个功能也就是一个很简单的事,最开始是每个顶点添加一个子节点,节点上添加一个圆点. forea ...

  8. zXing使用注意事项-转

    zXing使用注意事项   ------------------ zxing和zbar的比较: ZXing项目的示例程序对于摄像头的控制写的非常全面,ZBar的没有ZBar基于C语言编写,解码效率高于 ...

  9. python获取代码行号

    获取行号: def get_file(): print("文件名 :",__file__,sys._getframe().f_lineno) print("函数名: &q ...

  10. Lambda动态排序

    private static IList<T> IListOrderBy<T>(IList<T> list, string propertyName) where ...