1、创建项目并导入redis
2、配置redis
3、写测试类
4、在redis中创建key
5、访问8082
6、在集成测试中测试方法
package com.example.boot3.redis;
import org.junit.jupiter.api.Assertions;
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.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import java.util.UUID;
@SpringBootTest
class Boot307RedisApplicationTests {
@Autowired
StringRedisTemplate redisTemplate;
@Test
void contextLoads() {
redisTemplate.opsForValue().set("haha", UUID.randomUUID().toString());
String haha = redisTemplate.opsForValue().get("haha");
System.out.println(haha);
}
@Test
void testList(){
String listName = "listtest";
redisTemplate.opsForList().leftPush(listName,"1");
redisTemplate.opsForList().leftPush(listName,"2");
redisTemplate.opsForList().leftPush(listName,"3");
String pop = redisTemplate.opsForList().leftPop(listName);
Assertions.assertEquals("3",pop);
}
@Test
void testSet(){
String setName = "setest";
redisTemplate.opsForSet().add(setName,"1","2");
Boolean aBoolean = redisTemplate.opsForSet().isMember(setName,"2");
Assertions.assertTrue(aBoolean);
Boolean aBoolean1 = redisTemplate.opsForSet().isMember(setName, "5");
Assertions.assertFalse(aBoolean1);
}
@Test
void testZSet(){
String setName = "zsetest";
redisTemplate.opsForZSet().add(setName,"类放羊",90.00);
redisTemplate.opsForZSet().add(setName,"张三",91.00);
redisTemplate.opsForZSet().add(setName,"李四",91.20);
redisTemplate.opsForZSet().add(setName,"王五",92.00);
ZSetOperations.TypedTuple<String> popMax = redisTemplate.opsForZSet().popMax(setName);
String value = popMax.getValue();
Double score = popMax.getScore();
System.out.println(value + "==>" + score);
}
@Test
void testHash(){
String mapName = "amap";
redisTemplate.opsForHash().put(mapName,"name","张三");
redisTemplate.opsForHash().put(mapName,"age","18");
System.out.println(redisTemplate.opsForHash().get(mapName,"name"));
System.out.println(redisTemplate.opsForHash().get(mapName,"age"));
}
}