导入依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.2.3</version>
</dependency>
测试demo
package com.item.test;
import redis.clients.jedis.Jedis;
public class JedisTest {
public static void main(String[] args) {
Jedis jedis =new Jedis("161.222.25.19",6379);
jedis.auth("297");
jedis.select(0);
String set = jedis.set("name", "南宁");
System.out.println(set);
String name = jedis.get("name");
System.out.println(name);
jedis.close();
}
}
存入成功
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e9002dca065c4ca9de0498f576afabd9.png)
使用连接池连接
package com.item.test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashMap;
import java.util.Map;
public class JedisConnectionFactory {
private static final JedisPool jedispool;
static {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(8);
jedisPoolConfig.setMaxIdle(8);
jedisPoolConfig.setMinIdle(0);
jedisPoolConfig.setMaxWaitMillis(1000);
jedispool = new JedisPool(jedisPoolConfig,"185.222.22.22",6379,1000,"2323113");
}
public static Jedis getJedis(){
return jedispool.getResource();
}
public static void main(String[] args) {
Jedis jedis = getJedis();
jedis.select(0);
long hmset = jedis.hset("user:1", "name", "Jack1");
System.out.println(hmset);
Map<String, String> stringStringMap = jedis.hgetAll("user:1");
System.out.println(stringStringMap);
}
}