JAVA-springboot 整合Redis
·
SpringBoot从入门到精通-第16章 Redis
一、Redis简介
Redis是一款基于内存的Key-Value结构的数据库,在加上其底层采用单线程、多路I/O复用模型,使得Redis的运行速度非常快,可以很好的完成高并发、大数据的吞吐任务。
二、Redis常用命令
- 键值命令
**赋值取值操作**
[root@localhost ~]# sh redis-cli.sh
10.0.0.132:6379> set name zhang3
OK
10.0.0.132:6379> get zhang3
(nil)
10.0.0.132:6379> get "zhang3"
(nil)
10.0.0.132:6379> get name
"zhang3"
10.0.0.132:6379>
**库操作**
10.0.0.132:6379[6]> select 0
OK
10.0.0.132:6379> get name
"zhang3"
10.0.0.132:6379> move name 6
(integer) 1
10.0.0.132:6379> get name
(nil)
10.0.0.132:6379> select 6
OK
10.0.0.132:6379[6]> get name
"zhang3"
10.0.0.132:6379[6]>
**清空库**
10.0.0.132:6379[6]> FLUSHALL
OK
10.0.0.132:6379[6]> get name
(nil)
10.0.0.132:6379[6]>
**查看所有**
10.0.0.132:6379[6]> KEYS *
(empty list or set)
10.0.0.132:6379[6]> set name wang5
OK
10.0.0.132:6379[6]> set age 222222
OK
10.0.0.132:6379[6]> KEYS *
1) "age"
2) "name"
10.0.0.132:6379[6]>
- 哈希命令
为哈希类型赋值
10.0.0.132:6379[8]> HSET user 001 zhang3
(integer) 1
10.0.0.132:6379[8]> HSET user 002 wang5
(integer) 1
10.0.0.132:6379[8]> HSET user 003 li4
(integer) 1
10.0.0.132:6379[8]> KEYS *
1) "user"
10.0.0.132:6379[8]> HGET *
(error) ERR wrong number of arguments for 'hget' command
10.0.0.132:6379[8]> HGET user 001
"zhang3"
10.0.0.132:6379[8]> HGET user 002
"wang5"
10.0.0.132:6379[8]> HGET user 003
"li4"
10.0.0.132:6379[8]>
- 列表命令
赋值、取值
10.0.0.132:6379> SELECT 10
OK
10.0.0.132:6379[10]> LPUSH demo 1 2 3 4 5 6
(integer) 6
10.0.0.132:6379[10]> LPUSH test a s d f g h
(integer) 6
10.0.0.132:6379[10]> RPUSH demo 33 22 11
(integer) 9
10.0.0.132:6379[10]> LRANGE demo 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
7) "33"
8) "22"
9) "11"
10.0.0.132:6379[10]> LRANGE test 0 3
1) "h"
2) "g"
3) "f"
4) "d"
- 集合命令
10.0.0.132:6379[10]> SELECT 3
OK
10.0.0.132:6379[3]> SADD demo aa ss dd ff gg a
(integer) 6
10.0.0.132:6379[3]> SMEMBERS demo
1) "ss"
2) "ff"
3) "dd"
4) "a"
5) "aa"
6) "gg"
10.0.0.132:6379[3]> SPOP demo
"ss"
10.0.0.132:6379[3]> SPOP demo
"dd"
10.0.0.132:6379[3]> SPOP demo
"gg"
10.0.0.132:6379[3]> SPOP demo
"aa"
三、示例
- 使用SpringBoot项目给Redis存入key
- 使用SpringBoot项目读取Redis的key
四、整合Redis
-
2.1、创建springboot项目
-
2.2、引入Redis依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>_20250704springboot_redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>20250704springboot_redis</name>
<description>20250704springboot_redis</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 2.3、添加配置文件
spring.application.name=20250704springboot_redis
spring.data.redis.host=10.0.0.132
spring.data.redis.port=6379
- 2.4、编写代码使用Redis
package com.example._20250704springboot_redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@Autowired
private StringRedisTemplate template;
@RequestMapping("/redis")
// public String sout(){
// System.out.println("redis_fun");
// return "redis";
// }
public String set(){
template.opsForValue().set("name","zhang3");
return "set over";
}
@RequestMapping("/getredis")
public String get(){
return template.opsForValue().get("name");
}
}
- 2.5、测试Redis,增、查


更多推荐
所有评论(0)