Jedis是Redis官方推荐的java连接开发工具!使用Java操作Redis的中间件!
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>redis01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
</dependencies>
</project>
import redis.clients.jedis.Jedis;
public class TestPing {
public static void main(String[] args) {
//new Jedis对象
Jedis jedis = new Jedis("服务器ip",6379);
//Jedis所有命令都在对象里
System.out.println(jedis.ping());
}
}
执行成功,输出pong
注意:想要连接远程服务器的redis,必须注意以下几点
String List Set Hash Zset
import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class TestTX {
public static void main(String[] args) {
Jedis jedis = new Jedis("203.195.154.132",6379);
jedis.flushDB();
Transaction multi = jedis.multi();
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name","dengwei");
String s = jsonObject.toJSONString();
try {
multi.set("user1",s);
multi.set("user2",s);
multi.exec();
}catch (Exception e) {
multi.discard();
e.printStackTrace();
}finally {
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();
}
}
}
原文:https://www.cnblogs.com/dwwzone/p/13157697.html