SpringBoot integrates Redis
First create a Springboot project.
spring-data-redis针对jedis提供了如下功能:
1.连接池自动管理,并提供了一个高度封装的“RedisTemplate”类
2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
ValueOperations:简单K-V操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:针对map类型的数据操作
ListOperations:针对list类型的数据操作
(1) Create a SpringBoot program to introduce the starter
Note that the dependent choice here is redis, and be careful not to choose wrongly.
Or introduce the starter dependency into the pom.xml file
<!--整合redis的启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
The code of the complete pom file is as follows: Pay attention to the SpringBoot version and Java version. If an error is reported, you need to modify the jdk version in the project structure, and pay attention to the version consistency.
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>redisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redisdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2) application.yml configuration connection information
You need to rename the properties to the yml file, and add redis-related configurations. No other configurations are needed because the tests are performed in the test. The redis username and password need to be configured correctly, and you can use the redis client connection test.
spring:
data:
redis:
host: 127.0.0.1
port: 6379
password:
(3) Use RedisTemplate to operate Redis
1 value type operation
First of all, the bottom of the picture above is printed out, which is the type of operation string.
package com.example.redisdemo;
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.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String,String> redisTemplate;
@Test
void test1() {
ValueOperations<String, String> ops = redisTemplate.opsForValue();
//ops 存
ops.set("username","caojun");
// ops取
String username = ops.get("username");
System.out.println(username);
//移除
// redisTemplate.delete("username");
}
}
The following figure is a screenshot in the redis client.
2 Set type operation
After testing, the image below is printed
package com.example.redisdemo;
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.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// set集合类型
@Test
void test2() {
SetOperations<String, String> ops = redisTemplate.opsForSet();
//存:
ops.add("set", "caojun", "JACK", "orange");
//取:
Set<String> set = ops.members("set");
System.out.println(set);
//移除:移除单个元素
ops.remove("set", "jack");
//删除key
redisTemplate.delete("set");
}
}
3 List collection operation
3.1 Right push stack
This is a push operation.
package com.example.redisdemo;
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.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
//右压栈
ops.rightPush("myList","a");
ops.rightPush("myList","b");
ops.rightPush("myList","c");
//取值:
List<String> myList = ops.range("myList", 0, -1);
System.out.println(myList);
}
}
3.2 Push to the left
package com.example.redisdemo;
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.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
//左压栈
ops.leftPush("myList","A");
ops.leftPush("myList","B");
ops.leftPush("myList","C");
//取值:
List<String> myList = ops.range("myList", 0, -1);// CBAabc
System.out.println(myList);
}
}
3.3 Query elements based on index
package com.example.redisdemo;
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.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 根据索引查询元素
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
String value = ops.index("myList", 1);
System.out.println(value);
}
}
3.4 Remove the value of an element
package com.example.redisdemo;
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.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
ListOperations<String, String> ops = redisTemplate.opsForList();
ops.remove("myList",1,"A");
List<String> myList = ops.range("myList", 0, -1);//
System.out.println(myList);// CBabc
}
}
4 Hash type operations
4.1 Deposit value
package com.example.redisdemo;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 存入值
ops.put("user","username","曹俊");
ops.put("user","address","浙江省");
ops.put("user","age","18");
}
}
4.2 Extract all KEYs
package com.example.redisdemo;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
Set<Object> user = ops.keys("user");
System.out.println(user);// username address age
}
}
4.3 Extract all values
package com.example.redisdemo;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
List<Object> user = ops.values("user");
System.out.println(user);
}
}
4.4 Extract value according to KEY
package com.example.redisdemo;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
String o = (String) ops.get("user", "username");
System.out.println(o);
}
}
4.5 Remove value based on KEY
package com.example.redisdemo;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
@SpringBootTest
class RedisdemoApplicationTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
void test2() {
HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
// 提取所有的KEY[field]
ops.delete("user", "username");
}
}