这个部分依旧分为两个步骤,一个是redis客户端,一个是java的客户端
一:Redis客户端
1.get与set
2.删除
3.增加
4.是否存在
hexists myhash2 age
5.获取长度
6.获取keys
7.获取所有的values
二:Java客户端
1.java程序
1 package top.it; 2 3 import org.junit.Test; 4 import redis.clients.jedis.Jedis; 5 6 import java.util.Iterator; 7 import java.util.Set; 8 9 public class JedisDemo3 { 10 @Test 11 public void test(){ 12 //设置ip与端口 13 Jedis jedis=new Jedis("192.168.140.121",6379); 14 15 //获取所有的key值 16 Set keys=jedis.hkeys("myhash"); 17 Iterator<String> it=keys.iterator() ; 18 while(it.hasNext()){ 19 String key = it.next(); 20 System.out.println("keys:"+key); 21 } 22 System.out.println("==============================="); 23 //增 24 jedis.hset("myhash3","username","tom"); 25 jedis.hset("myhash3","age","12"); 26 jedis.hset("myhash3","sex","male"); 27 jedis.hset("myhash3","address","Shanghai"); 28 System.out.println(jedis.hkeys("myhash3")); 29 System.out.println("==============================="); 30 //删 31 jedis.hdel("myhash3","address"); 32 //改 33 jedis.hincrBy("myhash3","age",3); 34 //查 35 System.out.println(jedis.hget("myhash3","username")); 36 System.out.println(jedis.hmget("myhash3","username","sex")); 37 System.out.println(jedis.hexists("myhash3","sex")); 38 System.out.println(jedis.hkeys("myhash3")); 39 System.out.println(jedis.hvals("myhash3")); 40 41 42 jedis.close(); 43 } 44 }
2.效果