本文将介绍怎样利用Jedis库访问Redis数据库,下面将提供一个简单的实例,具体步骤如下:
你可以到 https://mvnrepository.com/ 去查找jedis库文件maven依赖,如下:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
package com.huangx.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* 第一个Redis程序
*/
public class Hello {
public static void main(String[] args) {
// 使用默认的配置获取jedis对象
// 连接到本地 localhost:6379 Redis服务器
JedisPool jedisPool = new JedisPool();
Jedis jedis = jedisPool.getResource();
// 存入值
jedis.append("title", "hello redis!");
// 获取值
String value = jedis.get("title");
System.out.println(value);
// 释放资源
jedis.close();
jedisPool.close();
}
}