idea maven项目初步使用Jedis

木来 木来

Maven项目的创建就不再赘述。

首先需要在pom.xml中引入Jedis的相关jar包

<!-- https://mvnrepository.com/artifact/redis.clients/jedis --><dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>2.9.0</version>
</dependency>

然后,创建一个类,使用Jedis

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;
import java.util.Set;

public class JedisTest {


    @Test    public void test1(){
        Jedis jedis=new Jedis("localhost",6379);

        jedis.hset("user","username","123456");
        jedis.hset("user","password","123");
        jedis.hset("user","name","zhangsan");

        Map<String,String> user=jedis.hgetAll("user");

        Set<String> keyset = user.keySet();
        for (String key : keyset) {
            String value=user.get(key);
            System.out.println(key+":"+value);
        }
        jedis.close();
    }

    @Test    public void test2(){

        JedisPool jedisPool=new JedisPool();
        Jedis jedis = jedisPool.getResource();
        jedis.set("username","zhangsana");
        jedis.close();


    }


}
0 条评论