Redis安装与开启

  我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/releases网址,直接点击下载解压就安装成功。开启也很简单:

  1.打开cmd,进入安装目录,输入命令:

redis-server.exe redis.windows.conf

  2.接着新打开一个cmd,原先的cmd不要关闭,不然后续步骤会出错,接着输入命令:

redis-cli.exe -h localhost -p 6379

  到这里windows上的redis已经安装并打开,可以正常使用了。

springboot整合redis

  1. 添加依赖:

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

  2.配置redis

#redis配置
spring:
redis:
database: 0
host: localhost
port: 6379
password:

3.操作redis需要使用RedisTemplate这个对象,但是需要修改RedisTemplate对象的序列化方式。

SDR(spring data redis)默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
StringRedisTemplate默认采用String的序列化策略
RedisTemplate默认采用JDK的序列化策略
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; @Configuration
public class RedisConfiguration { @Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory){
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jsonRedisSerializer.setObjectMapper(mapper);
//值序列化
stringRedisTemplate.setValueSerializer(jsonRedisSerializer);
stringRedisTemplate.afterPropertiesSet();
return stringRedisTemplate;
}
}
3.简单测试操作redis:
@RunWith(SpringRunner.class)
@SpringBootTest
public class PsringbootdemoApplicationTests { private static final Logger logger = LoggerFactory.getLogger(PsringbootdemoApplicationTests.class); @Resource
RedisTemplate redisTemplate; @Test
public void testRedis(){
logger.info("测试redis");
redisTemplate.opsForValue().set("lll","sgf");
String value = (String) redisTemplate.opsForValue().get("lll");
System.out.println(value);
} }

结果如下:

RedisTemplate

  RedisTemplate中定义了对5种数据结构操作

redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

具体的操作可以查看源码,或者推荐查看这篇博客Spring中使用RedisTemplate操作Redis(spring-data-redis)

 

springboot整合redis(简单整理)的更多相关文章

  1. springboot整合JPA(简单整理,待续---)

    整合步骤 引入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</group ...

  2. SpringBoot整合redis简单实现

    1.创建springboot项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...

  3. springboot整合redis简单示例

    一.项目架构 二.项目内容 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  4. SpringBoot整合Redis、ApachSolr和SpringSession

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

  5. SpringBoot 整合 Redis缓存

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

  6. SpringBoot系列十:SpringBoot整合Redis

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

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

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

  8. springboot整合redis——redisTemplate的使用

    一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...

  9. SpringBoot整合Redis使用Restful风格实现CRUD功能

    前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...

随机推荐

  1. | dp-the Treasure Hunter

    题目: A. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 mega ...

  2. 泡泡一分钟:Cooperative Object Transportation by Multiple Ground and Aerial Vehicles: Modeling and Planning

    张宁 Cooperative Object Transportation by Multiple Ground and Aerial Vehicles: Modeling and Planning 多 ...

  3. python经常使用的十进制、16进制、字符串、字节串之间的转换(长期更新帖)

    进行协议解析时.总是会遇到各种各样的数据转换的问题,从二进制到十进制,从字节串到整数等等 废话不多上.直接上样例 整数之间的进制转换: 10进制转16进制: hex(16)  ==>  0x10 ...

  4. JavaScript中innerHTML与innerText,createTextNode的区别

    innerHTML和innerText 它们都会把元素内内容替换掉,区别在于: innerHTML 会把替换内容里的 HTML 标记解释执行. innerText 会把替换内容里的 HTML 标记原样 ...

  5. 在多个平台如何安装Python

    最近的一个项目,架构师米洛需要用的mqtt,服务器开发的时候,竟然用的python脚本.运行python就需要安装开发环境,好吧,百度一下就知道了大神廖雪峰.以下部分测试通过,转载记录如下. 因为Py ...

  6. 线性表->链式存储->线形链表(单链表)

    文字描述: 为了表示前后两个数据元素的逻辑关系,对于每个数据元素,除了存储其本身的信息之外(数据域),还需存储一个指示其直接后继的信息(即直接后继的存储位置,指针域). 示意图: 算法分析: 在单链表 ...

  7. pandas(三)

    合并数据集: 创建一个能创建dataframe的函数 def make_data(cols,ind): data={c:[strc(c)+str(i) for i in ind] for c in c ...

  8. django集成ansibe实现自动化

    动态生成主机列表和相关参数 def create_admin_domain(admin_node): workpath = BASE_DIR + '/tools/ansible/script' hos ...

  9. Java面试题和解答(二)

    1.字符流和字节流的区别,使用场景是什么,相关类有哪些 http://blog.csdn.net/zj8692286/article/details/126507312.线程安全的概念,实现线程安全的 ...

  10. openpyxl.utils.exceptions.IllegalCharacterError

    如果是手动调用 xlwt 这种第三方库除了错可能没法找错误,但是从错误中我们看到错误是由 openpyxl 抛出的,我们试着从 openpyxl 中找解决方案 出错处的代码 value = value ...