1、download jedis的源码:
jedis采用的是git托管的,这边使用的是2.1.0版本:https://github.com/xetorthio/jedis/releases/tag/jedis-2.1.0;
2、解压打开可以看到,jedis采用的是maven构建工程的,所以我们的开发工具最好能支持maven工程。
3、eclipse导入jedis工程,在Package Exploer右键Import,选择maven工程
点击Finish即可完成导入工作,工程结构如下:
4、完成基本连接操作
修改pom.xml文件,将此处的localhost修改为自己的redis server IP
<properties>
<!-- <redis-hosts>localhost:6379,localhost:6380</redis-hosts> -->
<redis-hosts>192.168.1.70:4002,192.168.1.87:6339</redis-hosts>
</properties>
创建RedisClient类:
package com.xyx.redis.client;
import redis.clients.jedis.Jedis;
import com.xyx.redis.common.Constant;
public class RedisClient {
public static Jedis jedis = null;
public static Jedis getClient(){
if(jedis == null){
jedis = new Jedis(Constant.HOST,Constant.PORT);
}
return jedis;
}
}
Constant类:
package com.xyx.redis.common;
/**
* 常量配置
* @author Administrator
*
*/
public class Constant {
//redis主机IP地址
public static final String HOST="192.168.1.70";
//redis主机端口
public static final Integer PORT=4002;
}
RedisTest类:
package com.xyx.redis.test;
import org.junit.Test;
import com.xyx.redis.client.RedisClient;
/**
* 测试类
* @author Administrator
*
*/
public class redisTest {
/**
*
* @throws Exception
*/
public static void setUpBeforeClass() throws Exception{
}
/**
*
* @throws Exception
*/
public static void tearDownAfterClass() throws Exception{
}
@Test
public void test(){
System.out.println("放入数据="+RedisClient.getClient().set("key","123456"));
System.out.println("获取数据="+RedisClient.getClient().get("key"));
}
}
此时此刻,测试代码已经写好了, 开启redis-server
junit方式运行test方法:
使用工具redis-desktope查看: