下载jar的地址
http://mvnrepository.com/artifact/redis.clients/jedis
http://mvnrepository.com/artifact/org.apache.commons/commons-pool2
package demo; import org.junit.Test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * jedis的测试 */ public class mydemo { /** * 连接redis */ @Test public void demo1() { //连接redis Jedis jedis = new Jedis("localhost", 6379); //设置 jedis.set("name", "Tom"); //获取 String name = jedis.get("name"); System.out.println(name); //关闭 jedis.close(); } /** * redis连接池 */ @Test public void demo2() { // 获得连接池的配置对象 JedisPoolConfig config = new JedisPoolConfig(); // 最大连接数 config.setMaxTotal(30); // 最大空闲连接数 config.setMaxIdle(10); //获得连接池 JedisPool jedisPool = new JedisPool(config, "localhost", 6379); // 获得Redis对象 Jedis jedis = null; try { // 通过连接池获得连接 jedis = jedisPool.getResource(); jedis.set("name", "张三"); String name = jedis.get("name"); System.out.println(name); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally{ // 关闭redis连接 if (jedis != null) { jedis.close(); } if (jedisPool != null) { jedisPool.close(); } } } }