1.启动器依赖
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-data
-redis
</artifactId
>
</dependency
>
2.配置连接参数
redis
:
host
: localhost
port
: 6379
3.测试类
package com
.itheima
.redis
;
import org
.junit
.Test
;
import org
.junit
.runner
.RunWith
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.boot
.test
.context
.SpringBootTest
;
import org
.springframework
.data
.redis
.core
.RedisTemplate
;
import org
.springframework
.test
.context
.junit4
.SpringRunner
;
import java
.util
.List
;
import java
.util
.Set
;
@RunWith(SpringRunner
.class)
@SpringBootTest
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate
;
@Test
public void test(){
redisTemplate
.boundValueOps("str").set("heima");
System
.out
.println("str = " + redisTemplate
.opsForValue().get("str"));
redisTemplate
.boundHashOps("h_key").put("name", "heima");
redisTemplate
.boundHashOps("h_key").put("age", 13);
Set set
= redisTemplate
.boundHashOps("h_key").keys();
System
.out
.println(" hash散列的所有域:" + set
);
List list
= redisTemplate
.boundHashOps("h_key").values();
System
.out
.println(" hash散列的所有域的值:" + list
);
redisTemplate
.boundListOps("l_key").leftPush("c");
redisTemplate
.boundListOps("l_key").leftPush("b");
redisTemplate
.boundListOps("l_key").leftPush("a");
list
= redisTemplate
.boundListOps("l_key").range(0, -1);
System
.out
.println(" list列表中的所有元素:" + list
);
redisTemplate
.boundSetOps("s_key").add("a", "b", "c");
set
= redisTemplate
.boundSetOps("s_key").members();
System
.out
.println(" set集合中的所有元素:" + set
);
redisTemplate
.boundZSetOps("z_key").add("a", 30);
redisTemplate
.boundZSetOps("z_key").add("b", 20);
redisTemplate
.boundZSetOps("z_key").add("c", 10);
set
= redisTemplate
.boundZSetOps("z_key").range(0, -1);
System
.out
.println(" zset有序集合中的所有元素:" + set
);
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-13278.html