关于Jedis的用法以及Jedis使用Redis事务
作者:茂桑 时间:2023-06-28 07:22:56
我们要使用java来操作redis
什么是Jedis?
什么是Jedis 是Redis官方推荐的java连接开发工具!使用Java操作Redis 中间件!(就是一个jar包)如果你要使用java操作redis,那么一定要对Jedis十分的熟悉!
测试
1、导入对应的依赖
<!-- 导入jedis的包 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.2</version>
</dependency>
<!-- fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.50</version>
</dependency>
2、编码测试
连接redis数据库
操作命令
断开连接!
public class TestPing {
public static void main(String[] args) {
//1、new Jedis对象即可
Jedis jedis = new Jedis("127.0.0.1",6379);
//Jedis所有的命令就是我们之前学习的指令!
System.out.println(jedis.ping());;
}
}
输出:说明连接成功
PONG
jedis所有方法和redis里的所有命令一模一样,没有任何改变,在redis上能操作的,使用jedis同样可以操作
String
执行结果:
string的其他方法:
list
执行结果:
set
执行结果:
hash
执行结果:
zset
也是一样,在linux上执行的命令就是jedis的方法
所有的api命令,就是我们学习的指令,一一对应,一个都没有变化!
事务
1、在java中使用redis的事务:
package com.uno;
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);
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name", "UNO");
String result = jsonObject.toJSONString();
//1、开启事务
Transaction multi = jedis.multi();
//jedis.watch(result); //加乐观锁,监视
try {
//2、命令入队
multi.set("user1", result);
multi.set("user2",result);
//3、执行,这三个步骤也都是和linux中操作redis命令一模一样
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();
}
}
}
执行结果:成功
{"name":"UNO","hello":"world"}
{"name":"UNO","hello":"world"}
2、如果出现错误:
package com.uno;
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(); //先清理一下数据库,免得被上面的代码在redis设置的值影响
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name", "UNO");
String result = jsonObject.toJSONString();
//1、开启事务
Transaction multi = jedis.multi();
// jedis.watch(result); //加乐观锁,监视
try {
//2、命令入队
multi.set("user1", result);
multi.set("user2",result);
int i = 1/0; //手动设置一个运行时异常,看是不是会出现运行时异常,然后被catch捕获
//3、执行,这三个步骤也都是和linux中操作redis命令一模一样
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();
}
}
}
执行结果:出现运行时异常,被catch捕获,在catch中放弃事务,就是都不执行
null
null
Exception in thread "main" java.lang.RuntimeException: java.lang.ArithmeticException: / by zero
at com.uno.TestTx.main(TestTx.java:28)
Caused by: java.lang.ArithmeticException: / by zero
at com.uno.TestTx.main(TestTx.java:22)
注意事项:jedis连不上远程服务器
用Jedis连接阿里云等服务器上的redis
一. 配置redis.conf
1.设置访问redis的密码:requirepass 要设置密码
2.注释bind 127.0.0.1
(重启redis-server服务,进入redis后要先验证密码,用这个命令:auth 密码 ,然后ping一下看有没有配置成功)
二 . idea访问时添加auth密码
Jedis jedis = new Jedis("服务器的外网ip",6379);
jedis.auth("redis的密码");
System.out.println(jedis.ping());
(输出PONG的话就成功了)
题外话:为什么事务transaction简称为TX
transaction的读法是/træn’zækʃən/
x的读法是/ɛks/
所以tx分别代表:t - tran; x-saction
来源:https://blog.csdn.net/dayuiicghaid/article/details/125680456
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity之绕轴进行旋转的操作
![](https://img.aspxhome.com/file/2023/9/104329_0s.jpg)
C#实现Array添加扩展实例
![](https://img.aspxhome.com/file/2023/2/74822_0s.png)
android当前apn的状态以及获取方法
修改maven本地仓库路径的方法
SpringCloud Eureka服务治理之服务注册服务发现
![](https://img.aspxhome.com/file/2023/0/79950_0s.png)
Android 中启动自己另一个程序的activity如何实现
Java计时新姿势StopWatch的使用方法详解
![](https://img.aspxhome.com/file/2023/2/70512_0s.png)
Android 开发程序锁应用简单实例
![](https://img.aspxhome.com/file/2023/4/139224_0s.png)
Java SE之了解泛型
![](https://img.aspxhome.com/file/2023/8/84788_0s.jpg)
c#定时器和global实现自动job示例
轻松理解Java面试和开发中的IoC(控制反转)
![](https://img.aspxhome.com/file/2023/4/58184_0s.jpg)
java实现递归菜单树
Java字符串拼接详解
java开发实现五子棋游戏
![](https://img.aspxhome.com/file/2023/4/125264_0s.jpg)
Android 显示刷新频率的实现代码
![](https://img.aspxhome.com/file/2023/5/137555_0s.jpg)
Java map为什么不能遍历的同时进行增删操作
![](https://img.aspxhome.com/file/2023/4/105944_0s.png)
java的各种类型转换全部汇总(推荐)
详解Spring MVC3返回JSON数据中文乱码问题解决
![](https://img.aspxhome.com/file/2023/7/77107_0s.jpg)