pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.yml

spring:
redis:
host: 192.168.16.128
port: 6379
# 下面这些可以不加
jedis:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接

测试类(测试需要关闭Linux的防火墙)

  • StringRedisTemplate采用String的序列化策略;RedisTemplate采用JDK的序列化策略。

  • 如果redis里存字符串使用StringRedisTemplate即可。

  • 如果redis里存储对象类型,而取出时又不想做数据转换,建议使用RedisTemplate。

StringRedisTemplate

package com.ah.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.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Map; @RunWith(SpringRunner.class)
@SpringBootTest
public class StringRedisTemplateTests {
@Autowired
StringRedisTemplate redis; @Test
public void contextLoads() {
// 测试字符串(opsForValue)
ValueOperations<String, String> strKV = redis.opsForValue();
strKV.set("name", "Dog" + new java.util.Date().getTime());
System.out.println(strKV.get("name")); // 测试Hash结构的数据(boundHashOps)
BoundHashOperations<String, Object, Object> hash = redis.boundHashOps("book");
hash.put("name", "西游记");
hash.put("author", "吴承恩");
System.out.println(hash.get("name")); // 直接获取Hash数据的键值对
Map<Object, Object> entries = hash.entries();
System.out.println("entries=" + entries);
}
}

RedisTemplate(操作对象)

package com.ah.redis;

import org.springframework.cache.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper; @Configuration
@EnableCaching
public class RedisTemplateConf extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 使用Jackson2JsonRedisSerializer来序列化/反序列化redis的value值
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL,
com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// value
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // 使用StringRedisSerializer来序列化/反序列化redis的key值
RedisSerializer<?> redisSerializer = new StringRedisSerializer();
// key
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer); redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
package com.ah.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.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTemplateTest { @Autowired
private RedisTemplate<String, Object> redisTemplate; @Test
public void test() {
// 删(如果有,删除成功)
Boolean delete = redisTemplate.delete("user");
System.out.println("delete:" + delete);
// 增
String key = "user";
User value = new User("八戒");
redisTemplate.opsForValue().set(key, value);
// 查
User user = (User) redisTemplate.opsForValue().get("user");
System.out.println(user);
}
} class User {
private String name; public User() {// 必须
} public User(String name) {
this.name = name;
} @Override
public String toString() {
return "User [name=" + name + "]";
}
}

SpringBoot2整合Redis的更多相关文章

  1. SpringBoot2整合Redis缓存

    遵循SpringBoot三板斧 第一步加依赖 <!-- Redis --> <dependency> <groupId>org.springframework.bo ...

  2. SpringBoot2整合Redis多数据源

    配置文件属性 spring: redis: database: 1 host: 192.168.50.144 port: 6379 password: timeout: 600 #Springboot ...

  3. springboot2 整合redis

    1.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  4. 手把手教你SpringBoot2整合Redis

    此文仅为初学java的同学学习,大佬请勿喷,文末我会附上完整代码包供大家参考 redis的搭建教程此处略过,大家自行百度,本文的教程开始: 一.先在pom.xml中添加相关依赖 <!--redi ...

  5. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download          2.新手 ...

  6. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解

    笔记 3.SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-bo ...

  7. springboot2.2.2企业级项目整合redis与redis 工具类大全

    1.springboot2.2.2整合redis教程很多,为此编写了比较完整的redis工具类,符合企业级开发使用的工具类 2.springboot与redis maven相关的依赖 <depe ...

  8. Springboot2.0整合Redis(注解开发)

    一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  9. SpringBoot2.0整合Redis

    Spring Boot2.0在2018年3月份正式发布,相比1.0还是有比较多的改动,例如SpringBoot 自2.0起支持jdk1.8及以上的版本.第三方类库升级.响应式 Spring 编程支持等 ...

随机推荐

  1. 【转】Loading PNGs with SDL_image

    FROM:http://lazyfoo.net/tutorials/SDL/06_extension_libraries_and_loading_other_image_formats/index2. ...

  2. 原生JS结合cookie实现商品评分组件

    开发思路如下: 1.利用JS直接操作DOM的方式开发商品评分组件,主要实现功能有:显示评价评分的样式以及将用户操作后对应的数据返回到主页面 2.主页面引入商品评分组件的js文件并根据规定格式的数据,生 ...

  3. python数据类型互相转换

    类型转换 关注公众号"轻松学编程"了解更多. 主要针对几种存储工具:list.tuple.dict.set 特殊之处:dict是用来存储键值对的. 1.list 转换为set l1 ...

  4. 如何k个一组反转链表

    之前的文章「递归反转链表的一部分」讲了如何递归地反转一部分链表,有读者就问如何迭代地反转链表,这篇文章解决的问题也需要反转链表的函数,我们不妨就用迭代方式来解决. 本文要解决「K 个一组反转链表」,不 ...

  5. 计算机二级考试:Java

    目录 第 1 章 Java 语言概论 第 2 章 基本数据类型 2.1 概述 2.1.1 标识符 2.1.2 关键字 2.1.3 常量 2.2 基本数据类型 第 3 章 运算符和表达式 3.2 算术运 ...

  6. 自动化测试之Selenium篇(一):环境搭建

    当前无论找工作或者是实际项目应用,自动化测试扮演着非常重要的角色,今天我们来学习下Selenium的环境搭建 Selenium简述 Selenium是一个强大的开源Web功能测试工具系列 可进行读入测 ...

  7. 【Kata Daily 190912】Alphabetical Addition(字母相加)

    题目: Your task is to add up letters to one letter. The function will be given a variable amount of ar ...

  8. 【SpringCloud】08.客户端负载均衡器:Ribbon

    客户端负载均衡器:Ribbon Ribbon实现软负载均衡核心: 服务发现 :依据服务的名字,把该服务下所有的实例都找出来 服务选择规则:依据规则策略,如果从多个实例中,选出有效的服务 服务监听:检测 ...

  9. 13 SOAP

    13 SOAP SOAP(原为Simple Object Access Protocol的首字母缩写,即简单对象访问协议)是交换数据的一种协议规范,使用在计算机网络Web服务(web service) ...

  10. vuex和axios的基本操作

    1.在src目录下创建一个api 是用于集中处理axios的相关配置 index.js就是处理axios的文件 具体如何使用axios 还请百度axios 2.URLs.js是存放需要请求的地址的 3 ...