The address of the original text is updated, and the reading effect is better!
Introduction
Jedis is a Java client for Redis designed for performance and ease of use.
Jedis is a Java client for Redis, designed for performance and ease of use.
#official address
Official address of Jedis: https://github.com/redis/jedisopen in new window
#quickstart _
- Create a new Maven project and introduce the following dependencies
<!--引入Jedis依赖-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.0</version>
</dependency>
<!--引入单元测试依赖-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
- Write a test class and establish a connection with Redis
private Jedis jedis;
@BeforeEach //被该注解修饰的方法每次执行其他方法前自动执行
void setUp(){
// 1. 获取连接
jedis = new Jedis("192.168.230.88",6379);
// 2. 设置密码
jedis.auth("codermast");
// 3. 选择库(默认是下标为0的库)
jedis.select(0);
}
- Write a method for manipulating data (here we take the operation of String type as an example)
@Test
public void testString(){
// 1.往redis中存放一条String类型的数据并获取返回结果
String result = jedis.set("url", "https://www.codermast.com");
System.out.println("result = " + result);
// 2.从redis中获取一条数据
String url = jedis.get("url");
System.out.println("url = " + url);
}
- Finally, write a method to release resources
@AfterEach //被该注解修饰的方法会在每次执行其他方法后执行
void tearDown(){
// 1.如果jedis被使用过,则释放资源
if (jedis != null){
jedis.close();
}
}
- Test the result after executing the testString() method.
result = OK
url = https://www.codermast.com