1、下载驱动
jedis驱动源码地址
jedis驱动下载地址
2、测试代码
package com.djhu.redis.test; import java.util.HashMap; import java.util.List; import java.util.Map; import redis.clients.jedis.Jedis; public class ConnectionTest { public static Jedis getConnection(String ip, int port) { Jedis jedis = new Jedis(ip, port); //jedis.auth("password"); return jedis; } public static void cleanAll(Jedis jedis) { jedis.flushDB(); } public static void stringTest(Jedis jedis) { jedis.set("key01", "a"); jedis.set("key02", "b"); jedis.set("key03", "c"); jedis.mset("key04", "d", "key05", "e", "key06", "f"); jedis.del("key04"); System.out.println("key01 is " + jedis.get("key01")); System.out.println("key04 is " + jedis.get("key04")); } public static void mapTest(Jedis jedis) { Map<String, String> map = new HashMap<String, String>(); map.put("username", "hansen"); map.put("usersex", "male"); jedis.hmset("mapkey01", map); map.put("username", "neohope"); map.put("usersex", "male"); jedis.hmset("mapkey02", map); map.put("username", "tuzi"); map.put("usersex", "female"); jedis.hmset("mapkey03", map); List<String> rsmap = jedis.hmget("mapkey03", "username", "usersex"); System.out.println("query for hmget(\"mapkey03\", \"username\", \"usersex\") is " + rsmap); jedis.hdel("mapkey02", "usersex"); System.out.println("query for hmget jedis.hmget(\"mapkey02\", \"username\") is "+jedis.hmget("mapkey02", "username")); System.out.println("query for jedis.hmget(\"mapkey02\", \"usersex\") is " + jedis.hmget("mapkey02", "usersex")); System.out.println("query for jedis.hlen(\"mapkey01\") is " + jedis.hlen("mapkey01")); System.out.println("query for jedis.exists(\"mapkey01\") is " + jedis.exists("mapkey01")); System.out.println("query for jedis.hkeys(\"mapkey01\") is " + jedis.hkeys("mapkey01")); System.out.println("query for jedis.hvals(\"mapkey01\") is " + jedis.hvals("mapkey01")); } public static void listTest(Jedis jedis) { jedis.lpush("keylist01", "a"); jedis.lpush("keylist01", "b"); jedis.lpush("keylist01", "c"); System.out.println("keylist01 is " + jedis.lrange("keylist01", 0, -1)); } public static void setTest(Jedis jedis) { jedis.sadd("keyset01", "01"); jedis.sadd("keyset01", "02"); jedis.sadd("keyset01", "03"); jedis.sadd("keyset01", "04"); jedis.sadd("keyset01", "05"); System.out.println("query for jedis.smembers(\"keyset01\") is " + jedis.smembers("keyset01")); System.out.println("query for jedis.sismember(\"keyset01\", \"06\") is " + jedis.sismember("keyset01", "06")); System.out.println("query for jedis.scard(\"keyset01\") is " + jedis.scard("keyset01")); } public static void sortTest(Jedis jedis) { jedis.rpush("keylist02", "16"); jedis.lpush("keylist02", "8"); jedis.lpush("keylist02", "4"); jedis.lpush("keylist02", "2"); jedis.lpush("keylist02", "1"); System.out.println("keylist02 is " + jedis.lrange("keylist02", 0, -1)); jedis.sort("keylist02"); System.out.println("after sort keylist02 is " + jedis.lrange("keylist02", 0, -1)); } public static void main(String[] args) { Jedis jedis = getConnection("localhost", 6379); cleanAll(jedis); stringTest(jedis); mapTest(jedis); listTest(jedis); sortTest(jedis); setTest(jedis); jedis.close(); } }