Redis保存对象的两种方式
一、序列化(实体需要实现Serializable接口)
- 序列化就是将一个对象转换为二进制的数据流。这样就可以进行传输,或者保存到文件中。如果一个类的对象要想实现序列化,就必须实现serializable接口。在此接口中没有任何的方法,此接口只是作为一个标识,表示本类的对象具备了序列化的能力而已。
- 反序列化:将二进制数据流转换成相应的对象
主要代码如下:
1 2 3 4 5 6 7 8
| Company company1 = new Company(); company1.setCustId(102034L); company1.setCustName(getSessionUser().getCustName()); String string = jedisClient.set("test123".getBytes(),SerializationUtil.serialize((company1));
byte[] bs = jedisClient.get("test123".getBytes()); Company company =(Company)SerializationUtil.deserialize(bs);
|
序列化工具类(两个)代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;
public class SerializationUtil {
public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { } return null; }
public static Object deserialize(byte[] bytes) { ByteArrayInputStream bais = null; try { bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) {
} return null; }
}
public class ObjectTranscoder { public static byte[] serialize(Object value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] rv=null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); os.writeObject(value); os.close(); bos.close(); rv = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { try { if(os!=null)os.close(); if(bos!=null)bos.close(); }catch (Exception e2) { e2.printStackTrace(); } } return rv; } public static Object deserialize(byte[] in) { Object rv=null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if(in != null) { bis=new ByteArrayInputStream(in); is=new ObjectInputStream(bis); rv=is.readObject(); is.close(); bis.close(); } } catch (Exception e) { e.printStackTrace(); }finally { try { if(is!=null)is.close(); if(bis!=null)bis.close(); } catch (Exception e2) { e2.printStackTrace(); } } return rv; } }
|
二、json字符串
1 2 3 4 5 6 7 8 9
| Company company1 = new Company(); company1.setCustId(102034L); company1.setCustName(getSessionUser().getCustName()); String string = jedisClient.set("test123", com.alibaba.fastjson.JSON.toJSONString(company1));
String returnValue = jedisClient.get("test123"); Company company = com.alibaba.fastjson.JSON.parseObject(returnValue,Company.class); System.out.println(company.getCustName());
|
用redis的字符串存储java对象的话,后面查询的数据是没法进行分页的,如果要分页可以考虑用redis的list存储