Java bean

Posted by Wh0ami-hy on February 13, 2023

1. Java bean

Java bean 满足一些规范的Java类

It is a regular Java class, except it follows certain conventions:

All properties are private (use getters/setters):属性私有
A public no-argument constructor:无参构造
Implements Serializable:实现Serializable接口

public class Employee implements Serializable{

   @Id
   private int id;
   private String name;   
   private int salary;  

   public Employee() {}

   public Employee(String name, int salary) {
      this.name = name;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName( String name ) {
      this.name = name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}

2. serialVersionUID

@Data  
public class BaseEntity implements Serializable {  
    private static final long serialVersionUID = 1L;  
  
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")  
    @Schema(description = "创建时间")  
    private LocalDateTime createTime;  
  
    @Schema(description = "创建人")  
    private String createBy;  
  
  
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")  
    @Schema(description = "修改时间")  
    private LocalDateTime updateTime;  
  
    @Schema(description = "修改人")  
    private String updateBy;  
  
    @Schema(description = "备注")  
    private String remark;  
  
    @Schema(description = "删除标志")  
    private String delFlag;  
}

serialVersionUID = 1L 的作用

序列化和反序列化必须保证serialVersionUID的一致性

在类中显式声明serialVersionUID = 1L 后,在序列化后,反序列化前,更改了类的属性或者方法,只要显式的指定了serialVersionUID的值, 那么在反序列化的过程中不会报错,只是反序列化出来的类中,我们反序列化前添加的属性值读取不到

3. 其他称呼

Java Bean、bean、pojo、实体类


本站总访问量