通过java操作redis

前提是redis服务器已启动
package redis;

import redis.clients.jedis.Jedis;


import java.util.HashMap;


/**
* @auto dh
* @create 2020-04-05-19:23
*/
public class Redis002 {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.220.129", 6380);
//获取所有的key
System.out.println(jedis.keys("*"));
//通过hset赋值
jedis.hset("custom", "name", "zhangsan");
//通过hget获取值
System.out.println(jedis.hget("custom", "name"));
HashMap<String, String> hm = new HashMap();
hm.put("name", "乔村小学");
hm.put("year", "2013");
//通过hmset赋值
jedis.hmset("school", hm);
//通过hmget获取值
System.out.println(jedis.hmget("school", "name", "year"));
//通过hkeys来获取school里的所有key
System.out.println(jedis.hkeys("school"));
//通过hvals来获取school里的所有vals
System.out.println(jedis.hvals("school"));
//通过hexists来判断school里的key为name是否存在
System.out.println(jedis.hexists("school", "name"));
//通过hgetAll来获取school里的所有键值对
System.out.println(jedis.hgetAll("school"));
//通过hdel来删除school里的key为name的键值对
jedis.hdel("school", "name");
System.out.println(jedis.hgetAll("school"));
//通过hincrBy来增加key为year的value值
jedis.hincrBy("school", "year", 20);
System.out.println(jedis.hget("school", "year"));
jedis.hincrBy("school", "year", 30);
System.out.println(jedis.hget("school", "year"));
//通过hlen来获取school里的键值对个数
System.out.println(jedis.hlen("school"));
}
}
原文地址:https://www.cnblogs.com/kukai/p/12639029.html