jedis

什么是Jedis

jedis是redis官方推荐的java连接开发工具!使用Java操作redis的中间件!如果你要使用Java操作redis,那么一定要对jedis十分熟悉

使用Jedis

前提

  1. 必须要放开redis允许启动主机访问。
    注释/usr/local/bin/config/redis.conf的 bind 127.0.0.1 这个句话
    在这里插入图片描述
  2. 取消保护模式
    redis处于保护模式,只能本地链接,我们需要修改配置文件redis.conf,将protected-mode yes改成no

在这里插入图片描述

3.防火墙开放6379端口

查看端口开放信息,如果没有看见6379,就需要设置
[root@centos7 config]# firewall-cmd --list-ports

#开启6379端口
[root@centos7 config]# firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
#重启防火墙
[root@centos7 config]# firewall-cmd --reload
success
查看端口开放信息
[root@centos7 config]# firewall-cmd --list-ports
6379/tcp

测试连接

  • 连接数据库
  • 操作命令
  • 断开连接
第一步:创建maven简单项目,添加依赖pom.xml
<?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>redis-01-jedis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--导入jedis依赖包-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <!--3.2.0-->
            <version>3.6.3</version>
        </dependency>

        <!--fastjson依赖包-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
    </dependencies>


</project>
第二步:测试代码
package org.example;

import redis.clients.jedis.Jedis;

public class TestPing {

    public static void main(String[] args) {
        //1.new Jedis对象
        Jedis jedis = new Jedis("192.168.222.129", 6379);
        //jedis 所有redis命令都在jedis同名的方法中
        System.out.println(jedis.ping());
    }
}

测试结果图
在这里插入图片描述

jedis常用的API

String、List、Set、Hash、Zset

所有的api命令,就是redis命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值