官方网址:GitHub - redis/jedis: Redis Java client
整合过程
1.引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
2.进行测试
private Jedis jedis;
@Before
public void setup(){
//建立链接
jedis = new Jedis("192.168.2.182", 6379);
//设置密码
jedis.auth("123456");
//选择库,默认0号库
jedis.select(0);
}
//string
@Test
public void test(){
//存入数据
String set = jedis.set("名字", "张三");
System.out.println("set"+set);
//读取数据
String s = jedis.get("名字");
System.out.println("读取数据"+s);
}
//hash
@Test
public void htest(){
HashMap<String, String> stringStringHashMap = new HashMap<>();
stringStringHashMap.put("age","13");
stringStringHashMap.put("xc","1312");
//单条添加
jedis.hset("user:1", "name", "张三");
//批量添加
jedis.hmset("user:1",stringStringHashMap);
Map<String, String> stringStringMap = jedis.hgetAll("user:1");
System.out.println(stringStringMap);
}
@After
public void down(){
if (jedis!=null){
//关闭链接
jedis.close();
}
}
jedis连接池
创建连接池
public class JedisConnectionFactory {
private static final JedisPool jedispool;
static {
//配置连接池
JedisPoolConfig PoolConfig = new JedisPoolConfig();
PoolConfig.setMaxTotal(8);
PoolConfig.setMaxIdle(8);
PoolConfig.setMaxIdle(0);
PoolConfig.setMaxWaitMillis(1000);
//创建连接池对象
jedispool = new JedisPool(PoolConfig, "192.168.2.182", 6379, 1000, "123456");
}
public static Jedis getjedis(){
return jedispool.getResource();
}
}
使用
jedis = JedisConnectionFactory.getjedis();