使用jedis在IDE中操作Redis
POM文件中添加依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
服务器端允许端口访问
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ce275f308ec6869c0c298abd087b6910.png#pic_center)
测试能否访问redis数据库
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/911ba3e3f8471f186a894bb719a5fe7f.png#pic_center)
public class Main {
public static void main(String[] args) {
Jedis jedis=new Jedis(localhost,6379);
String ping=jedis.ping();
System.out.println(ping);
}
}
利用jedis访问和操作redis
public static void main(String[] args) {
Jedis jedis=new Jedis("47.96.10.50",6379);
JSONObject jsonObject=new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name","ylq");
jedis.flushAll();
Transaction multi =jedis.multi();
String res=jsonObject.toString();
try {
multi.set("user1",res);
multi.set("user2",res);
int i=1/0;
multi.exec();
} catch (Exception e) {
multi.discard();
throw new RuntimeException(e);
} finally {
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();
}
}