Java操作Redis存储HashMap对象类型数据

背景描述

 关于JAVA去操作Redis时,如何存储一个对象的数据,这是大家非常关心的问题,虽然官方提供了存储String,List,Set等等类型,但并不满足我们现在实际应用。存储一个对象是非常常见的。经过网上查找资料以及参考其他网友的代码和实践操作后,整理出Redis如何存储一个Java的HashMap demo代码,如果有错误的地方大家可以留言指出。另外关于如果存储一个java对象可以参考上篇文章【Java操作Redis存储对象类型数据】https://blog.csdn.net/Chris_111X/article/details/85053544

参考资料

参考地址:

https://blog.csdn.net/bang_come/article/details/79098866

https://www.cnblogs.com/JKayFeng/p/5911544.html

原理

先将对象存储到HaspMap中,然后将HashMap对象进行序列化,取出数据时再对HashMap进行反序列化。

 

Redis版本

Redis3.0

https://mvnrepository.com/artifact/redis.clients/jedis

Bean代码

package com.obj.bean;

import java.io.Serializable;

public class Person implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public String userName;
    public String password;
    public int age;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    

}
 

测试代码

package com.java.client;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.obj.bean.Person;
import redis.clients.jedis.Jedis;
public class RedisHashMapKey {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // TODO Auto-generated method stub
        Jedis redis = new Jedis("10.190.130.143", 6379);
        redis.auth("sa123456");
        Person p1 = new Person();
        p1.setUserName("user1");
        p1.setPassword("password1");
        p1.setAge(10);
        Person p2 = new Person();
        p2.setUserName("user2");
        p2.setPassword("password2");
        p2.setAge(12);
        Person p3 = new Person();
        p3.setUserName("user3");
        p3.setPassword("password3");
        p3.setAge(13);
        HashMap<String, Person> map = new HashMap<String, Person>();
        map.put("p1", p1);
        map.put("p2", p2);
        map.put("p3", p3);
        //转成输出字节流
        ByteArrayOutputStream bai = new ByteArrayOutputStream();
        ObjectOutputStream obi = new ObjectOutputStream(bai);
        obi.writeObject(map);
        byte[] byt = bai.toByteArray();
        // 将map存入redis中
        redis.set("p".getBytes(), byt);
        //获取map
        byte[] personByte = redis.get("p".getBytes());
        ObjectInputStream oii = null;
        ByteArrayInputStream bis = null;
        //转换成输入字节流
        bis = new ByteArrayInputStream(personByte);
        oii = new ObjectInputStream(bis);
        Map<String, Person> result = (Map<String, Person>) oii.readObject();
        for (Entry<String, Person> entry : result.entrySet()) {
            System.out.println("key= " + entry.getKey());
            Person obj = (Person) entry.getValue();
            System.out.println("getUserName: " + obj.getUserName());
            System.out.println("getPassword= " + obj.getPassword());
            System.out.println("getAge= " + obj.getAge());

        }

    }
}


运行结果

查看Redis

 

 

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值