1.1 导入需要的依赖
xml
<dependencies>
<!-- 1、Jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.1</version>
</dependency>
<!-- 2、 Junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 3、 Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
1.2 测试
java
import redis.clients.jedis.Jedis;
public class TestJedisConnection {
// Redis服务器信息
private static final String REDIS_HOST = "192.168.200.141";
private static final int REDIS_PORT = 6379;
private static final String REDIS_PASSWORD = "sl183691";
public static void main(String[] args) {
//1. 连接Redis
Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT);
jedis.auth(REDIS_PASSWORD);
//2. 操作Redis - Redis的命令是什么,Jedis的方法就是什么
jedis.set("city", "Shanghai");
//2. 获取操作结果
String value = jedis.get("city");
System.out.println(value);
//3. 释放资源
jedis.close();
}
}
测试结果: