zoukankan      html  css  js  c++  java
  • 前端有个list,后台获取。及对象序列化存库

    前端有个list,需要以此存库时,一般情况下应该怎么做呢,我们会在前端循环这个list,把每一行的内容放进一个string【】中,然后后台处理,取出值,这样比较麻烦。

    现在我发现一个简单的做法。例如:

    public A{

      private String age;

      private String number;

      private List<B>  listB;

    }

    public B{

      private String name;

      private String sex;

    }

    前端如果有这个list的话,这需要吧list中的名字改为 name = "listB[index].name" 或者 name = "listB[index].sex",就会通过自动封装进listB中。

    2、对象的序列化,对象如果想序列化,对象必须实现Serialization接口。

    //将新得到的用户信息与当前用户信息序列化
    ByteArrayOutputStream baos;
    ObjectOutputStream out = null;
    baos = new ByteArrayOutputStream(); //
    try {
    out = new ObjectOutputStream(baos);
    out.writeObject(staff1);
    out.writeObject(staff2);
    } catch (IOException e) {
    throw new Exception();
    }finally{
    out.close();
    }
    //序列化的对象转为字节数组
    byte[] byteObject = baos.toByteArray();
    //将前后两次用户信息赋值
    staffChange.setAfter(byteObject); // staffChange对象中有一个 private byte[] after,属性

    反序列化得到对象,

    ByteArrayInputStream bais;

    ObjectInputStream in = null;
    Staff staff = null;
    try {

    bais = new ByteArrayInputStream(after);

    in = new ObjectInputStream(bais);

    in.readObject();//放进的是堆中,先进先出,存放的时候,先放的修改前的,所以先读出来一份

    staff=(Staff) in.readObject();  //先读出来一份后,接下来才是修改后的,然后获取到了

    } catch (IOException ex) {


    } catch (ClassNotFoundException ex) {

    }finally{
    try {
    in.close();
    } catch (IOException e) {
    }
    }
    return staff;

    反序列化存库,在个人信息变更中!!!

  • 相关阅读:
    nodejs向远程服务器发送post请求----融云Web SDK/客户端获取token
    Oauth2.0认证---授权码模式
    AngularJS---自定义指令
    Leetcode160-Intersection of Two Linked Lists-Easy
    Lintcode489-Convert Array List to Linked List-Easy
    Lintcode228-Middle of Linked List-Naive
    Lintcode174-Remove Nth Node From End of List-Easy
    Lintcode225-Find Node in Linked List-Naive
    Lintcode85-Insert Node in a Binary Search Tree-Easy
    Lintcode93-Balanced Binary Tree-Easy
  • 原文地址:https://www.cnblogs.com/h-wei/p/11017189.html
Copyright © 2011-2022 走看看