源码地址:仓库 - Allen (datadogapache) - Gitee.com

一、安装Redis

安装环境:centOS7、docker

1、在docker仓库里查看Redis版本

2、下载最新版本

docker pull redis:latest

3、查看镜像

docker images

4、启动Redis

docker run -itd --name redis-test -p 6379:6379 redis

5、查看Redis镜像

docker ps

6、启动Redis客户端命令行

docker exec -it redis-test /bin/bash

二、新建Spring项目整合Redis

1、添加相关依赖

       #redis依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>#json序列化依赖<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.58</version></dependency>

2、配置文件

server.port=8099
spring.redis.host=192.168.91.131
spring.redis.port=6379
#连接池最大连接数
spring.redis.jedis.pool.max-active=8
#连接池最大阻塞等待时间,负数表示没有限制
spring.redis.jedis.pool.max-wait=-1
#连接池最大空闲连接数
spring.redis.lettuce.pool.max-idle=8

3、建立User实体类

package com.example.springredis.Entity;import lombok.Data;import java.util.ArrayList;
import java.util.List;@Data
public class User {//用户名private String userName;//密码private String passWord;private List detail=new ArrayList();
}

4、对密码用MD5进行编码

package com.example.springredis.Utils;import com.example.springredis.Entity.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;public class Crypt {public StringBuilder Crypt(User user) throws NoSuchAlgorithmException, UnsupportedEncodingException {MessageDigest md5 = MessageDigest.getInstance("MD5");byte[] digest = md5.digest(user.getPassWord().getBytes("UTF-8"));StringBuilder sb = new StringBuilder();for (byte item : digest) {sb.append(Integer.toHexString((item & 0xFF) |0x100).substring(1, 3));}return sb;}
}

5、模拟对User实体赋值并进行提交到Redis服务器

package com.example.springredis.Service;import com.alibaba.fastjson.JSONObject;
import com.example.springredis.Entity.User;
import com.example.springredis.Utils.Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;@Service
public class UserService {@ResourceRedisTemplate redisTemplate;public User user() throws NoSuchAlgorithmException, UnsupportedEncodingException {User user=new User();Crypt crypt=new Crypt();StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(List.class);user.setUserName("Allen");user.setPassWord("Lowy");user.getDetail().add("贵州省");user.getDetail().add("遵义市");user.getDetail().add("遵义四中");StringBuilder crypt1 = crypt.Crypt(user);user.setPassWord(crypt1.toString());//对key序列化redisTemplate.setKeySerializer(stringRedisSerializer);//对value序列化redisTemplate.setValueSerializer(stringRedisSerializer);redisTemplate.opsForValue().set("userName",user.getUserName());redisTemplate.opsForValue().set("passWord",user.getPassWord());String detail = JSONObject.toJSONString(user.getDetail());redisTemplate.opsForList().rightPush("detail",detail);return user;}
}

6、建立Controller类进行调用

package com.example.springredis.Controller;import com.example.springredis.Entity.User;
import com.example.springredis.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;@RestController
@CrossOriginpublic class TestRedisController {@AutowiredRedisTemplate redisTemplate;@AutowiredUserService userService;@RequestMapping("redisTest")public String testRedis() throws NoSuchAlgorithmException, UnsupportedEncodingException {User user = userService.user();return "从redis中取出的用户名:"+redisTemplate.opsForValue().get("userName")+"\n"+"密码为:"+redisTemplate.opsForValue().get("passWord")+"\n"+redisTemplate.opsForList().range("detail",0,-1);}
}

7、访问接口http://localhost:8099/redisTest

三、安装Redis客户端查看值

其中spring.redis.database的配置通常使用0即可(默认为0),Redis在配置的时候可以设置数据库数量,默认为16,可以理解为数据库的schema.

SpringBoot整合Redis实战相关推荐

  1. SpringBoot 整合 redis 实战

    本文基于 SpringBoot,RedisTemplate API 操作 Redis 缓存,相较于Jedis客户端,RedisTemplate跟 springboot 的集成性更好,但是性能比 Jed ...

  2. SpringBoot实战(四):SpringBoot整合Redis

    强烈推荐一个大神的人工智能的教程:http://www.captainai.net/zhanghan​ [前言] 最近自己在整理过去搭建过的框架,将用到的各个组件进行了梳理并融入自己新建的项目中(ht ...

  3. 【案例实战】SpringBoot整合Redis的GEO实现查找附近门店功能

    像我们平常美团点外卖的时候,都会看到一个商家距离我们多少米.还有类似QQ附近的人,我们能看到附近的人距离我们有多少米. 那么这些业务是怎么做的呢?是如何实现 基于位置的附近服务系统呢. 在去了解基于位 ...

  4. SpringBoot整合Redis+mybatis,封装RedisUtils工具类等实战(附源码)

    点击上方蓝色字体,选择"标星公众号" 优质文章,第一时间送达 关注公众号后台回复pay或mall获取实战项目资料+视频 作者:陈彦斌 cnblogs.com/chenyanbin/ ...

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

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

  6. SpringBoot2.x整合redis实战讲解

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

  7. SpringBoot2.0整合Redis实战

    SpringBoot2.x整合Redis实战 1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/down ...

  8. 【七】springboot整合redis(超详细)

    springboot篇章整体栏目: [一]springboot整合swagger(超详细 [二]springboot整合swagger(自定义)(超详细) [三]springboot整合token(超 ...

  9. springboot整合redis实现HyperLogLog统计文章浏览量使用过期策略完成数据库同步

    springboot整合redis实现HyperLogLog统计文章浏览量&&使用过期策略完成数据库同步 本文目录 springboot整合redis实现HyperLogLog统计文章 ...

  10. SpringBoot第九篇: springboot整合Redis

    这篇文章主要介绍springboot整合redis,至于没有接触过redis的同学可以看下这篇文章:5分钟带你入门Redis. 引入依赖: 在pom文件中添加redis依赖: <dependen ...

最新文章

  1. Annu. Rev. Genet:植物微生物组——系统性见解与展望
  2. 【解决方案】在Pycharm使用jupyter要求填写token【please enter your jupyter notebook url】
  3. 熟悉常用的Linux命令操作
  4. 查看mysql SQL物理读_Oracle查看逻辑读、物理读资源占用排行的SQL语句
  5. JavaScript的面向对象特性
  6. 文件操作(stat)
  7. 作者:张金芳(1970-),男,中国科学院软件研究所副研究员
  8. Hibernate 学习-1
  9. jquery radio设置选中_前端jQuery实战之 attr() 和 prop() 的区别
  10. 基于STM32的人体红外测温
  11. 中国电信CTWAP和CTNET已经融合
  12. 计算机配置8c16g,腾讯云服务器价格表(1核2G/2核4G/4核8G/8核16G配置)
  13. android 播放提示音,[转载]android播放音效例子 (翻页音效、警报音效通用
  14. 数据库SQL查询练习
  15. [Linux 基础] -- Linux input 子系统要点总结
  16. ZYNQ从放弃到入门(三)- 中断(一)
  17. 一文解读如何评估项目的价值和可行性?
  18. 一个网站域名价值 1亿人民币,互联网寸土寸金!
  19. 超码、候选码、主码(主键)、主属性、非主属性、关系数据库中的依赖、关系数据库范式、反范式
  20. 魔术师的猜牌术(一维数组) C++程序

热门文章

  1. 计算机病毒手动查杀,如何手动查杀计算机病毒
  2. linux漏洞抓鸡,风靡全球的Ms08-067漏洞抓鸡大曝光
  3. textbox控件变成透明怎么办
  4. VR是什么,去哪里学习?
  5. linux中UDP编程
  6. 【软件需求工程】北理的恶龙们——软件需求规格说明
  7. μc/os-II原理简介(笔记)
  8. 大型网站技术架构思维导图
  9. 火狐一键检测自己的邮箱是否被泄露信息 输入邮箱即可查看是否安全
  10. 使用Python和MySQL实现网上购物管理系统