java中使用redis --- Hash的简单应用

1.java代码

public class RedisTest01 {

    public static void main(String[] args) {

        // connect redis server
        Jedis redis = new Jedis("127.0.0.1", 6379);

        Map<String, String> map = new HashMap();
        map.put("userName", "jack");
        map.put("password", "123");
        map.put("age", "12");
        // 将map存入redis中
        redis.hmset("myMap", map);

        // 取出redis中的map进行遍历
        Map<String, String> userMap = redis.hgetAll("myMap");
        for (Map.Entry<String, String> item : userMap.entrySet()) {
            System.out.println(item.getKey() + " : " + item.getValue());
        }
    }

}

2.输出

userName : jack
age : 12
password : 123

3.Hash存储类型最适合存储java中的Entity,用的比较多。。。

原文地址:https://www.cnblogs.com/zhangjianbing/p/8506528.html