首先创建一个maven工程,然后倒入下面的pom
- <dependencies>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- dependency>
-
- <dependency>
- <groupId>redis.clientsgroupId>
- <artifactId>jedisartifactId>
- <version>2.8.0version>
- dependency>
- dependencies>
然后写一个测试类
测试方法分为 3步和jdbc差不多
1、获取连接,new一个Jedis对象
2、执行操作,大致和操作命令差不多
3、关闭连接
- @Test
- public void testReids(){
- //1 获取连接
- Jedis jedis = new Jedis("localhost", 6379);
- //2 执行具体操作
- jedis.set("username","xiaoming");
- String s = jedis.get("username");
- System.out.println(s);
- jedis.del("username");
-
- jedis.hset("myhash","addr","beijing");
- String hget = jedis.hget("myhash", "addr");
- System.out.println(hget);
- Set
keys = jedis.keys("*"); - Map
myhash = jedis.hgetAll("myhash"); - for (String key : keys){
- if ("myhash".equals(key))
- System.out.println("------"+key);
- }
- //3 关闭连接
- jedis.close();
- }
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.4.5version>
- <relativePath/>
- parent>
- <groupId>com.itheimagroupId>
- <artifactId>springdataredis_demoartifactId>
- <version>1.0-SNAPSHOTversion>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
- dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <version>2.4.5version>
- plugin>
- plugins>
- build>
- project>
首先创建一个RedisTemplate对象
- @Autowired
- private RedisTemplate redisTemplate;
Spring Data Redis中提供了一个高度封装的类:RedisTemplate,针对 Jedis 客户端中大量api进行了归类封装,将同一类型操作封装为operation接口,具体分类如下:
ValueOperations:简单K-V操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:针对hash类型的数据操作
ListOperations:针对list类型的数据操作
- /**
- * 操作String类型数据
- */
- @Test
- public void testString(){
- redisTemplate.opsForValue().set("city132","beijing");
-
- String value = (String) redisTemplate.opsForValue().get("city132");
- System.out.println(value);
- System.out.println(redisTemplate.delete("city132"));
- System.out.println(redisTemplate.delete("city12345"));
-
- redisTemplate.opsForValue().set("key1","value1",10l, TimeUnit.SECONDS);
-
- Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "nanjing");
- System.out.println(aBoolean);
- }
- /**
- * 操作Hash类型数据
- */
- @Test
- public void testHash(){
- HashOperations hashOperations = redisTemplate.opsForHash();
-
- //存值
- hashOperations.put("002","name","xiaoming");
- hashOperations.put("002","age","20");
- hashOperations.put("002","address","bj");
-
- //取值
- String age = (String) hashOperations.get("002", "age");
- System.out.println(age);
-
- //获得hash结构中的所有字段
- Set keys = hashOperations.keys("002");
- for (Object key : keys) {
- System.out.println(key);
- }
-
- //获得hash结构中的所有值
- List values = hashOperations.values("002");
- for (Object value : values) {
- System.out.println(value);
- }
- }
- /**
- * 操作List类型的数据
- */
- @Test
- public void testList(){
- ListOperations listOperations = redisTemplate.opsForList();
-
- //存值
- listOperations.leftPush("mylist","a");
- listOperations.leftPushAll("mylist","b","c","d");
-
- //取值
- List
mylist = listOperations.range("mylist", 0, -1); - for (String value : mylist) {
- System.out.println(value);
- }
-
- //获得列表长度 llen
- Long size = listOperations.size("mylist");
- int lSize = size.intValue();
- for (int i = 0; i < lSize; i++) {
- //出队列
- String element = (String) listOperations.rightPop("mylist");
- System.out.println(element);
- }
- }
- /**
- * 操作Set类型的数据
- */
- @Test
- public void testSet(){
- SetOperations setOperations = redisTemplate.opsForSet();
-
- //存值
- setOperations.add("myset","a","b","c","a");
-
- //取值
- Set
myset = setOperations.members("myset"); - for (String o : myset) {
- System.out.println(o);
- }
-
- //删除成员
- setOperations.remove("myset","a","b");
-
- //取值
- myset = setOperations.members("myset");
- for (String o : myset) {
- System.out.println(o);
- }
-
- }
- /**
- * 操作ZSet类型的数据
- */
- @Test
- public void testZset(){
- ZSetOperations zSetOperations = redisTemplate.opsForZSet();
-
- //存值
- zSetOperations.add("myZset","a",10.0);
- zSetOperations.add("myZset","b",11.0);
- zSetOperations.add("myZset","c",12.0);
- zSetOperations.add("myZset","a",13.0);
-
- //取值
- Set
myZset = zSetOperations.range("myZset", 0, -1); - for (String s : myZset) {
- System.out.println(s);
- }
-
- //修改分数
- zSetOperations.incrementScore("myZset","b",20.0);
-
- //取值
- myZset = zSetOperations.range("myZset", 0, -1);
- for (String s : myZset) {
- System.out.println(s);
- }
-
- //删除成员
- zSetOperations.remove("myZset","a","b");
-
- //取值
- myZset = zSetOperations.range("myZset", 0, -1);
- for (String s : myZset) {
- System.out.println(s);
- }
- }
- /**
- * 通用操作,针对不同的数据类型都可以操作
- */
- @Test
- public void testCommon(){
- //获取Redis中所有的key
- Set
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 dataType = redisTemplate.type("myset");
- System.out.println(dataType.name());
-
- }