SpringBoot整合Redis
首先创建 Springboot 项目。
spring-data-redis针对jedis提供了如下功能:
1.连接池自动管理,并提供了一个高度封装的“RedisTemplate”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
ValueOperations:简单K-V操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:针对map类型的数据操作
ListOperations:针对list类型的数据操作
(一)创建SpringBoot程序引入启动器
注意这里依赖的选择的是 redis,注意不要选择错误。
或者是pom.xml文件当中引入启动器依赖
<!--整合redis的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
完整 pom 文件代码如下:要注意SpringBoot版本及Java version,如果报错需要修改项目结构中的jdk版本,注意版本一致。
<?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>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>redisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redisdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
(二)application.yml配置连接信息
需要将 properties 改名为 yml 文件,并且添加 redis 相关配置,不需要添加其他配置,因为是在 test 里面进行测试。redis的用户名密码需要配置正确,可以用redis客户端连接测试。
spring:
data:
redis:
host: 127.0.0.1
port: 6379
password:
(三)使用RedisTemplate操作Redis
1 值类型操作
首先上图最下面打印出来了,这是操作string类型的。
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
void test1() {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
//ops 存
ops.set("username","caojun");
// ops取
String username = ops.get("username");
System.out.println(username);
//移除
// redisTemplate.delete("username");
}
}
下图就是在redis客户端中的截图。
2 Set类型操作
通过测试下图打印出来了
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// set集合类型
@Test
void test2() {
SetOperations<String, String> ops = redisTemplate.opsForSet();
//存:
ops.add("set", "caojun", "JACK", "orange");
//取:
Set<String> set = ops.members("set");
System.out.println(set);
//移除:移除单个元素
ops.remove("set", "jack");
//删除key
redisTemplate.delete("set");
}
}
3 List集合操作
3.1 右压栈
这是压栈操作。
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
//右压栈
ops.rightPush("myList","a");
ops.rightPush("myList","b");
ops.rightPush("myList","c");
//取值:
List<String> myList = ops.range("myList", 0, -1);
System.out.println(myList);
}
}
3.2 左压栈
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
//左压栈
ops.leftPush("myList","A");
ops.leftPush("myList","B");
ops.leftPush("myList","C");
//取值:
List<String> myList = ops.range("myList", 0, -1);// CBAabc
System.out.println(myList);
}
}
3.3 根据索引查询元素
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 根据索引查询元素
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
String value = ops.index("myList", 1);
System.out.println(value);
}
}
3.4 移除某个元素的值
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
ops.remove("myList",1,"A");
List<String> myList = ops.range("myList", 0, -1);//
System.out.println(myList);// CBabc
}
}
4 Hash类型操作
4.1 存入值
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 存入值
ops.put("user","username","曹俊");
ops.put("user","address","浙江省");
ops.put("user","age","18");
}
}
4.2 提取所有的KEY
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
Set<Object> user = ops.keys("user");
System.out.println(user);// username address age
}
}
4.3 提取所有的值
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
List<Object> user = ops.values("user");
System.out.println(user);
}
}
4.4 根据KEY提取值
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
String o = (String) ops.get("user", "username");
System.out.println(o);
}
}
4.5根据KEY移除值
package com.example.redisdemo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
ops.delete("user", "username");
}
}