- 在Spring Boot项目中,可以使用Spring Data Redis来简化Redis操作,maven的依赖坐标:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
8.3.3、操作Redis的步骤
(1)创建一个Spring Boot工程;
(2)在pom.xml文件中导入以上Spring Data Redis的依赖坐标;
(3)配置application.yml文件:
#spring的配置
spring:
application:
name: springdataRedis_demo
main:
#解决升级Spring Boot2.6后,因依赖循环引用导致启动时报错的问题
allow-circular-references: true
#Redis的相关配置(在spring下)
redis:
port: 6379 #默认端口号
host: localhost #表示连接的是本地的服务,要连接远程的话,需要更改为远程服务器的ip地址
#设置密码(在redis的redis.windows.conf配置文件中有设置密码的话,这里要配置)
#password: 123456
database: 0 #默认提供了16个数据库,编号为0-15,在命令行可以通过select index更改数据库
#jedis的配置
jedis:
#Redis连接池配置
pool:
max-active: 8 #最大连接数
max-wait: 1ms #连接池最大阻塞等待时间
max-idle: 4 #连接池中的最大空闲连接
min-idle: 0 #连接池中最小空闲连接
(4)配置序列化配置类:
package com.itcast.springdataredis_demo.config;
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.serializer.StringRedisSerializer;
/**
* @author :LTY
* @date :Created in 2023/7/20 16:37
* @description:序列化配置类
* @modified By:
* @version: $
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//1.创建RedisTemplate对象
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//默认的key序列化器为: JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer()); //String类型
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //Hash哈希类型
//设置Value的序列化
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
//2.设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
(5)测试各数据类型的数据
首先在测试类上进行如下修改:
-
String类型数据
测试代码:
/**
* 操作String类型数据
*/
@Test
public void testString() {
redisTemplate.opsForValue().set("city12", "beijing");
String city = (String)redisTemplate.opsForValue().get("city12");
System.out.println(city);
//设置10秒的过去时间(对应redis的String类型的setex key seconds value命令)
redisTemplate.opsForValue().set("key1", "value1", 10L, TimeUnit.SECONDS);
//对应redis的String类型的setnx key value命令
redisTemplate.opsForValue().setIfAbsent("city12","shanghai");
}
运行结果如下:
- Hash哈希类型数据
测试代码:
/**
* hash哈希类型数据
*/
@Test
public void testHash() {
HashOperations hashOperations = redisTemplate.opsForHash();
//存入数据(对应hset命令)
hashOperations.put("002","name","xiaohong");
hashOperations.put("002","age","21");
hashOperations.put("002","sex","male");
//获取单个字段的值(对应hget命令)
String name = (String)hashOperations.get("002", "name");
System.out.println("获得单个字段的值(hget key field): " +name);
//获得hash结构中所有的字段(对应hkeys命令)
System.out.println("获得hash结构中所有的字段(hkeys key): ");
Set keys = hashOperations.keys("002");
for (Object key : keys) {
System.out.println(key);
}
//获得hash结构中所有的字段的值(对应hvals命令)
System.out.println("获得hash结构中所有的字段的值(kvals key): ");
List values = hashOperations.values("002");
for (Object value : values) {
System.out.println(value);
}
//获取哈希表中指定key的所有字段和值(hgetall key)
System.out.println("获得哈希表中指定key的所有字段和值(hgetall key): ");
Map entries = hashOperations.entries("002");
System.out.println(entries);
}
运行结果如下:
-
List类型数据
测试代码:
/** * 操作List类型的数据 */ @Test public void testList() { ListOperations listOperations = redisTemplate.opsForList(); //存值(从左边存值) listOperations.leftPush("mylist", "a"); //存一个 listOperations.leftPushAll("mylist","b","c","d"); //存多个 //从右边存值 listOperations.rightPush("mylist", "e"); listOperations.rightPushAll("mylist", "f","g");//此时存入的值顺序:d c b a e f g //取值(从表头到表尾依次输出) List<String> mylist = listOperations.range("mylist", 0, -1); for (String s : mylist) { System.out.println(s); //输出:d c b a e f g } //获取列表的菜单llen System.out.println("...............移除..........."); Long size = listOperations.size("mylist"); int i1 = size.intValue(); for (int i = 0; i < i1; i++) { //出队 /** * leftPop:移除并获取第一个元素(从表头出队(左边)) * rightPop:移除并获取最后一个元素(从表尾移除(右边)) */ // String mylist1 = (String)listOperations.rightPop("mylist"); // System.out.println(mylist1); //输出:g f e a b c d //rightPopAndLeftPush:从mylist表尾移除数据,并在mylist2左边(表头)存值 listOperations.rightPopAndLeftPush("mylist","mylist2", 10l, TimeUnit.SECONDS);//存入顺序:d c b a e f g } //取值 System.out.println("..........mylist2.........."); List<String> mylist2 = listOperations.range("mylist2", 0, -1); for (String s1 : mylist2) { System.out.println(s1); //输出:d c b a e f g } }
运行结果如下:
- Set类型数据
①基本操作
测试代码:
/**
* 操作Set类型数据
*/
@Test
public void testSet() {
SetOperations setOperations = redisTemplate.opsForSet();
//存值(sadd)
setOperations.add("myset", "a", "b", "c","a");
//取值(smembers)
Set<String> myset = setOperations.members("myset");
//获得集合的成员数(scard)
System.out.println("集合的成员数为: " + setOperations.size("myset"));
for (String s : myset) {
System.out.println(s); //输出:
}
//删除一个或多个成员(srem)
setOperations.remove("myset", "a", "b");
//取值
myset = setOperations.members("myset");
for (String string : myset) {
System.out.println(string);
}
}
运行结果:
②交集、并集和差集
测试代码:
/**
* 交集、并集和差集
*/
@Test
public void testSet2() {
redisTemplate.opsForSet().add("myset2", "a", "b", "c");
redisTemplate.opsForSet().add("myset3", "a", "b", "e");
//交集
System.out.println("myset2与myset3的交集为: ");
Set<String> intersect = redisTemplate.opsForSet().intersect("myset2", "myset3");
for (String o : intersect) {
System.out.println(o);
}
//并集
System.out.println("myset2与myset3的并集为: ");
Set<String> union = redisTemplate.opsForSet().union("myset2", "myset3");
for (String o : union) {
System.out.println(o);
}
//差集
//myset2-myset3
System.out.println("myset2与myset3的差集为(myset2-myset3): ");
Set<String> difference1 = redisTemplate.opsForSet().difference("myset2", "myset3");
for (String o : difference1) {
System.out.println(o);
}
//myset3-myset2
System.out.println("myset3与myset2的差集为(myset3-myset2): ");
Set<String> difference2 = redisTemplate.opsForSet().difference("myset3", "myset2");
for (String o : difference2) {
System.out.println(o);
}
}
运行结果:
- ZSet类型数据
测试代码:
/**
* 操作ZSet类型数据
*/
@Test
public void testZSet() {
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//存值
zSetOperations.add("myZSet", "a", 20.0);
zSetOperations.add("myZSet", "b", 30.0);
zSetOperations.add("myZSet", "c", 40.0);
zSetOperations.add("myZSet", "a", 50.0);
//取值
Set<String> myZSet = zSetOperations.range("myZSet", 0, -1);
for (String s : myZSet) {
System.out.println(s);
}
运行结果:
- 通用操作
测试代码:
/**
* 通用操作,针对不同类型的数据类型都可以进行操作
*/
@Test
public void testCommon() {
//获取Redis中所有的key
Set<String> keys = redisTemplate.keys("*");
for (String key : keys) {
System.out.println(key);
}
//判断某个key是否存在
Boolean itcast = redisTemplate.hasKey("itcast");
System.out.println(itcast);
//删除指定的key
redisTemplate.delete("myZSet");
//获得指定的key对应的value的数据类型
DataType type = redisTemplate.type("myset");
System.out.println(type);
}
运行结果:文章来源:https://www.toymoban.com/news/detail-611980.html
文章来源地址https://www.toymoban.com/news/detail-611980.html
到了这里,关于Spring Data Redis操作Redis的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!