undefined

Jedis

什么是 jedis,是redis 官方推荐的java 连接开发工具!使用Java 操作redis 中间件

1
2
3
4
5
6
7
8
9
10
11
12
<!--导入jedis的包-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.learn.redis;

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("127.0.0.1", 6379);

jedis.flushDB();

JSONObject jsonObject = new JSONObject();
jsonObject.put("hello", "world");
jsonObject.put("name", "noahyzhang");

// 开启事务
Transaction multi = jedis.multi();
String result = jsonObject.toJSONString();

try {
multi.set("user1", result);
multi.set("user2", result);
} catch (Exception e) {
multi.discard();
e.printStackTrace();
} finally {
multi.exec();
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();
}


// 关闭连接
jedis.close();
}
}