首页 > 其他分享 >Jedis快速入门和Jedis操作string

Jedis快速入门和Jedis操作string

时间:2022-08-22 14:48:01浏览次数:58  
标签:set string System jedis println Jedis out 入门

Java客户端Jedis

  • Jedis:一款java操作redis数据库的工具
  • 使用步骤:
    • 下载jedis的jar包
    • 使用
    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>4.3.0-m1</version>
    </dependency>
package com.ailyt.jiedis.test;

import org.junit.Test;
import redis.clients.jedis.Jedis;

public class JedisTest {
    /**
     * 快速入门
     */
    @Test
    public void MyTest01() {
        //获取连接
        Jedis jedis = new Jedis("localhost",6379);
        //操作
        jedis.set("username", "zhangsan");
        String set = jedis.set("password", "root");
        System.out.println(set);
 
        //关闭连接
        jedis.close();
    }
}

  • Jedis操作各种redis中的数据结构
    • 字符串类型string
      • set
      • get
package com.ailyt.jiedis.test;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.Set;

public class JedisTest {

    @Test
    public void MyTest01() {
        //获取连接
        Jedis jedis = new Jedis("localhost",6379);
        //操作
//        jedis.set("username", "zhangsan");
//        String set = jedis.set("password", "root");
//        System.out.println(set);

        String username = jedis.get("username");
        String password = jedis.get("password");
        System.out.println(username);//zhangsan
        System.out.println(password);//root

        long del1 = jedis.del("username");//1
        long del2 = jedis.del("password");//1
        System.out.println(del1);
        System.out.println(del2);

        Set<String> keys = jedis.keys("*");
        System.out.println(keys.toString());

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

标签:set,string,System,jedis,println,Jedis,out,入门
From: https://www.cnblogs.com/ailhy/p/16612588.html

相关文章