Redis保存对象的俩种方法详解
程序员文章站
2022-04-20 20:05:36
...
一、把对象转成Json
//第一种
Jedis redis=RedisPool.getJedis();
SysUser user=new SysUser();
user.setName("ygc");
user.setCode("123");
String userInfo = JSON.toJSONString(user,SerializerFeature.WriteMapNullValue);
redis.set("userInfo", userInfo);
String info=redis.get("userInfo");
SysUser user1=JSONObject.parseObject(info,SysUser.class);
System.out.println(user1.getName()+":第一种");
二、把对象转成byte[]
//第二种
redis.set("user".getBytes(),SerializeUtil.serialize(user1));
SysUser user2=(SysUser) SerializeUtil.unSerialize(redis.get("user".getBytes()));
System.err.println(user2.getName()+":第二种");
SerializeUtil 序列化工具
/**
* 序列化
*/
public static byte[] serialize(Object obj) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
byte[] byteArray = baos.toByteArray();
return byteArray;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 反序列化
* @param bytes
* @return
*/
public static Object unSerialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
// 反序列化为对象
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
完整版测试代码
package com.kero99.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kero99.pojo.SysUser;
import redis.clients.jedis.Jedis;
/**
* redis存对象
* @author ygc
*
*/
public class SerializeUtil {
/**
* 序列化
*/
public static byte[] serialize(Object obj) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
byte[] byteArray = baos.toByteArray();
return byteArray;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 反序列化
* @param bytes
* @return
*/
public static Object unSerialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
// 反序列化为对象
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Jedis redis=RedisPool.getJedis();
SysUser user=new SysUser();
user.setName("ygc");
user.setCode("123");
//第一种
String userInfo = JSON.toJSONString(user,SerializerFeature.WriteMapNullValue);
redis.set("userInfo", userInfo);
String info=redis.get("userInfo");
SysUser user1=JSONObject.parseObject(info,SysUser.class);
System.out.println(user1.getName()+":第一种");
//第二种
redis.set("user".getBytes(),SerializeUtil.serialize(user1));
SysUser user2=(SysUser) SerializeUtil.unSerialize(redis.get("user".getBytes()));
System.err.println(user2.getName()+":第二种");
}
}
上一篇: mybatis调用存储过程
下一篇: (两百八十二) 学习Simpleperf