pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

application.yml

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

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

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

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

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

StringRedisTemplate

  1. package com.ah.redis;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.data.redis.core.*;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. import java.util.Map;
  9. @RunWith(SpringRunner.class)
  10. @SpringBootTest
  11. public class StringRedisTemplateTests {
  12. @Autowired
  13. StringRedisTemplate redis;
  14. @Test
  15. public void contextLoads() {
  16. // 测试字符串(opsForValue)
  17. ValueOperations<String, String> strKV = redis.opsForValue();
  18. strKV.set("name", "Dog" + new java.util.Date().getTime());
  19. System.out.println(strKV.get("name"));
  20. // 测试Hash结构的数据(boundHashOps)
  21. BoundHashOperations<String, Object, Object> hash = redis.boundHashOps("book");
  22. hash.put("name", "西游记");
  23. hash.put("author", "吴承恩");
  24. System.out.println(hash.get("name"));
  25. // 直接获取Hash数据的键值对
  26. Map<Object, Object> entries = hash.entries();
  27. System.out.println("entries=" + entries);
  28. }
  29. }

RedisTemplate(操作对象)

  1. package com.ah.redis;
  2. import org.springframework.cache.annotation.*;
  3. import org.springframework.context.annotation.*;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.serializer.*;
  7. import com.fasterxml.jackson.annotation.PropertyAccessor;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. @Configuration
  10. @EnableCaching
  11. public class RedisTemplateConf extends CachingConfigurerSupport {
  12. @Bean
  13. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
  14. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  15. redisTemplate.setConnectionFactory(connectionFactory);
  16. // 使用Jackson2JsonRedisSerializer来序列化/反序列化redis的value值
  17. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
  18. Object.class);
  19. ObjectMapper objectMapper = new ObjectMapper();
  20. objectMapper.setVisibility(PropertyAccessor.ALL,
  21. com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);
  22. objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  23. jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  24. // value
  25. redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
  26. redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
  27. // 使用StringRedisSerializer来序列化/反序列化redis的key值
  28. RedisSerializer<?> redisSerializer = new StringRedisSerializer();
  29. // key
  30. redisTemplate.setKeySerializer(redisSerializer);
  31. redisTemplate.setHashKeySerializer(redisSerializer);
  32. redisTemplate.afterPropertiesSet();
  33. return redisTemplate;
  34. }
  35. }
  1. package com.ah.redis;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.data.redis.core.RedisTemplate;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class RedisTemplateTest {
  11. @Autowired
  12. private RedisTemplate<String, Object> redisTemplate;
  13. @Test
  14. public void test() {
  15. // 删(如果有,删除成功)
  16. Boolean delete = redisTemplate.delete("user");
  17. System.out.println("delete:" + delete);
  18. // 增
  19. String key = "user";
  20. User value = new User("八戒");
  21. redisTemplate.opsForValue().set(key, value);
  22. // 查
  23. User user = (User) redisTemplate.opsForValue().get("user");
  24. System.out.println(user);
  25. }
  26. }
  27. class User {
  28. private String name;
  29. public User() {// 必须
  30. }
  31. public User(String name) {
  32. this.name = name;
  33. }
  34. @Override
  35. public String toString() {
  36. return "User [name=" + name + "]";
  37. }
  38. }

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. MySQL主主模式+Keepalived高可用

    今天闲来无事,打算搭建一个MySQL的高可用架构,采用的是MySQL的主主结构,再外加Keepalived,对外统一提供虚IP.先来说说背景吧,现在的项目为了高可用性,都是避免单节点的存在的,比如,我 ...

  2. java POI Excel 单元格样式

    正如Html需要CSS一样,我们的POI生成的Excel同样需要样式才能更完美的表现我们的数据.下面还是从简单的例子出发,学习和了解POI的样式设计. 一.我的位置. 1 package com.my ...

  3. PHP获取文件拓展名的方法

    1.用strrchar()函数,查找字符串在另一字符串中最后出现的位置,并返回该位置到字符串最后的所有字符(返回结果包括点).即返回拓展名前  点  到结尾的字符,即为扩展名.注意与strchar() ...

  4. SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂)

    SpringBoot第四集:整合JdbcTemplate和JPA(2020最新最易懂) 当前环境说明: Windows10_64 Maven3.x JDK1.8 MySQL5.6 SpringTool ...

  5. Mybatis执行SQL的完整过程及四大组件介绍

    一切的执行从MapperProxy开始,MapperProxy是MapperProxyFactory使用SqlSession创建出来的.所以MapperProxy中包含SqlSession. 可以看到 ...

  6. SQLServer连接cache数据库

    开始文章之前首先要了解一下什么是Caché数据库. Caché数据库是美国Intersystems公司产品,后关系型数据库(Post Relational database)中的领头羊.Caché数据 ...

  7. Hill密码解密过程(Java)

    Hill密码是一种传统的密码体系.加密原理:选择一个二阶可逆整数矩阵A称为密码的加密矩阵,也就是这个加密体系的密钥.加密过程: 明文字母依次逐对分组,例如加密矩阵为二阶矩阵,明文就两个字母一组,如果最 ...

  8. 视频直播源码开发中的流媒体协议:rtmp协议

    一.概念与摘要 视频直播源码的RTMP协议从属于应用层,被设计用来在适合的传输协议(如TCP)上复用和打包多媒体传输流(如音频.视频和互动内容).RTMP提供了一套全双工的可靠的多路复用消息服务,类似 ...

  9. 为研发同学定制的MySQL面试指南 - “能谈谈基数统计吗?”

    ** 目录 推荐阅读原文链接 一.基数是啥? 二.InnoDB更新基数的时机? 三.基数是估算出来 四.持久化基数 四.如何主动更新基数? 欢迎关注 Hi,大家好!我是白日梦. 今天我要跟你分享的话题 ...

  10. ubuntu下安装nginx -php

    mysql : sudo apt-get install mysql-server mysql-client nginx: sudo apt-get install nginx安装Nginx稳定版本 ...