Redis存储list对象

来源:互联网 发布:磁条卡写卡软件 编辑:程序博客网 时间:2024/05/16 05:13
public class JdModel implements Serializable{private String bookID;    private String bookName;    private String bookPrice;    public String getBookID() {        return bookID;    }    public void setBookID(String bookID) {        this.bookID = bookID;    }    public String getBookName() {        return bookName;    }    public void setBookName(String bookName) {        this.bookName = bookName;    }    public String getBookPrice() {        return bookPrice;    }    public void setBookPrice(String bookPrice) {        this.bookPrice = bookPrice;    }}

public class SerializeUtil {public static byte[] serializeListObject(List<?> object) {        ObjectOutputStream oos = null;         ByteArrayOutputStream baos = null;         try {              // 序列化             baos = new ByteArrayOutputStream();             oos = new ObjectOutputStream(baos);             //oos.writeObject(object);             for(Object obj : object){               oos.writeObject(obj);               }                byte[] bytes = baos.toByteArray();              return bytes;        } catch (Exception e) {        }         return null;  }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 unserialize( byte[] bytes) {        ByteArrayInputStream bais = null;         try {              // 反序列化             bais = new ByteArrayInputStream(bytes);             ObjectInputStream ois = new ObjectInputStream(bais);              return ois.readObject();        } catch (Exception e) {        }         return null;  }         public static List<Object> deserialize(byte[] in) {         List<Object> list = new ArrayList();         ByteArrayInputStream bis = null;         ObjectInputStream is = null;         try {             if(in != null) {                 bis=new ByteArrayInputStream(in);                 is=new ObjectInputStream(bis);                 while (true) {                 Object obj=(Object) is.readObject();                                        if(obj == null){                         break;                     }else{                         list.add(obj);                     }                 }                 is.close();                 bis.close();             }         } catch (IOException e) {                 }  catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {                   }         return list;     }    }

jedis.set("jdModelList".getBytes(), SerializeUtil.serializeListObject(bookdatas));      byte[] in = jedis.get("jdModelList".getBytes());            List<Object> list = SerializeUtil.deserialize(in);           for(int i=0;i<list.size();i++){          JdModel model= (JdModel)list.get(i);          System.out.println(model.getBookName());          }

原创粉丝点击