赞
踩
使用SpringBoot的测试来实现这些相关的操作:
@Autowired
private RedisTemplate<String,String> redisTemplate;
redisTemplate.boundHashOps("tvs").put("cctv","中央电视台"); Set tvs = redisTemplate.boundHashOps("tvs").keys(); List tvs = redisTemplate.boundHashOps("tvs").values(); String string= (String)redisTemplate.boundHashOps("tvs").get("hntv"); // size Long size= redisTemplate.boundHashOps("tvs").size(); System.out.println("hash集合的大小" + size); //List redisTemplate.boundListOps("sbootList").leftPush("dd"); redisTemplate.boundListOps("sbootList").rightPush("11"); // range 0,-1为List查询 所有 List<String> sbootList = redisTemplate.boundListOps("sbootList").range(0, -1); // 表示把bb删除两个 redisTemplate.boundListOps("sbootList").remove(2,"bb"); //list set修改 1相当于修改第二个值 redisTemplate.boundListOps("sbootList").set(1,"me");
比如String类:
@Test
public void test(){
redisTemplate.boundValueOps("str").set("诸葛亮");
}
@Test
public void testGetValue(){
String address = redisTemplate.boundValueOps("address").get();
System.out.println(address);
}
pom.xml文件的引入:
<parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.1.14.RELEASE</version> </parent> <dependencies> <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> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies>
TestHash测试类:
package test; import com.hou.StuApplication; 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(classes = StuApplication.class) public class TestHash { @Autowired private RedisTemplate redisTemplate; @Test public void test(){ redisTemplate.boundHashOps("tvs").put("cctv","中央电视台"); redisTemplate.boundHashOps("tvs").put("hntv","湖南电视台"); redisTemplate.boundHashOps("tvs").put("bjtv","北京电视台"); } // 查询所有的key @Test public void test1(){ Set tvs = redisTemplate.boundHashOps("tvs").keys(); System.out.println(tvs); } // 获取所有的values @Test public void test2(){ List tvs = redisTemplate.boundHashOps("tvs").values(); System.out.println(tvs); } // 获取单个的值 @Test public void test3(){ String string= (String)redisTemplate.boundHashOps("tvs").get("hntv"); // size Long size= redisTemplate.boundHashOps("tvs").size(); System.out.println("hash集合的大小" + size); System.out.println("hntv的值"+string); } // 删除单个 @Test public void test4(){ Long del = redisTemplate.boundHashOps("tvs").delete("cctv"); Long size = redisTemplate.boundHashOps("tvs").size(); System.out.println(del + size); } }
TestRedisList:
package test; import com.hou.StuApplication; 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; @RunWith(SpringRunner.class) @SpringBootTest(classes = StuApplication.class) public class TestRedisList { @Autowired RedisTemplate<String, String> redisTemplate; //List添加 leftPush rightPush @Test public void test() { redisTemplate.boundListOps("sbootList").leftPush("aa"); redisTemplate.boundListOps("sbootList").leftPush("bb"); redisTemplate.boundListOps("sbootList").leftPush("cc"); redisTemplate.boundListOps("sbootList").leftPush("dd"); redisTemplate.boundListOps("sbootList").rightPush("11"); redisTemplate.boundListOps("sbootList").rightPush("22"); redisTemplate.boundListOps("sbootList").rightPush("33"); redisTemplate.boundListOps("sbootList").rightPush("44"); } @Test //boundsListOps("list").range(0,-1); public void test1(){ // range 0,-1为List查询 所有 List<String> sbootList = redisTemplate.boundListOps("sbootList").range(0, -1); System.out.println(sbootList); } @Test //list set修改 1相当于修改第二个值 public void test2(){ redisTemplate.boundListOps("sbootList").set(1,"me"); } // 删除 @Test public void test3(){ // 表示把bb删除两个 redisTemplate.boundListOps("sbootList").remove(2,"bb"); } // 集合查询单个 @Test public void test4(){ String index = redisTemplate.boundListOps("sbootList").index(Long.parseLong("1")); Long size = redisTemplate.boundListOps("sbootList").size(); System.out.println(index + "集合的大小是" + size); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。