Redis存储list对象

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 block
		e.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());
          }

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭