外卖项目day05-在Java中操作Redis_Spring Data Redis使用方式_操作哈希类型的数据
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package com.sky.test;
|
||||
|
||||
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.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@SpringBootTest //让springboot识别测试类,并能调用IOC容器
|
||||
public class SpringDateRedisTest {
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
@Test
|
||||
public void testRedisTemplate(){
|
||||
System.out.println(redisTemplate);
|
||||
//字符串类型
|
||||
ValueOperations valueOperations = redisTemplate.opsForValue();
|
||||
//哈希数据类型
|
||||
HashOperations hashOperations = redisTemplate.opsForHash();
|
||||
//列表数据
|
||||
ListOperations listOperations = redisTemplate.opsForList();
|
||||
//集合数据
|
||||
SetOperations setOperations = redisTemplate.opsForSet();
|
||||
//有序集合
|
||||
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
|
||||
|
||||
System.out.println(valueOperations);
|
||||
System.out.println(hashOperations);
|
||||
System.out.println(listOperations);
|
||||
System.out.println(setOperations);
|
||||
System.out.println(zSetOperations);
|
||||
}
|
||||
|
||||
/*
|
||||
操作redis字符串数据类型
|
||||
*/
|
||||
@Test
|
||||
public void testString(){
|
||||
//set get setex setnx
|
||||
ValueOperations ops = redisTemplate.opsForValue();
|
||||
//set get 命令
|
||||
ops.set("city","常州");//设置值
|
||||
String city= (String) ops.get("city");
|
||||
System.out.println(city);
|
||||
//setex--计时保存【验证码】
|
||||
ops.set("code","1234",30, TimeUnit.SECONDS);//15秒有效的验证码
|
||||
//setnx---先判断是否存在,再保存key
|
||||
System.out.println(ops.setIfAbsent("age", 100));//true
|
||||
System.out.println(ops.setIfAbsent("age", 200));//false
|
||||
}
|
||||
|
||||
/*
|
||||
操作redis--哈希数据类型
|
||||
*/
|
||||
@Test
|
||||
public void testHash(){
|
||||
//hset hget hdel hkeys hvals
|
||||
HashOperations ops = redisTemplate.opsForHash();
|
||||
//hset
|
||||
ops.put("p100","name","tom");
|
||||
ops.put("p100","age","22");
|
||||
//hget
|
||||
String name = (String) ops.get("p100", "name");
|
||||
String age = (String) ops.get("p100", "age");
|
||||
System.out.println(name+"--"+age);
|
||||
//hkeys
|
||||
Set hkeys = ops.keys("p100");
|
||||
System.out.println(hkeys);
|
||||
//hvals
|
||||
List hvalues = ops.values("p100");
|
||||
System.out.println(hvalues);
|
||||
//hdel:删除某个人对象的age属性
|
||||
ops.delete("p100","age");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user