96 lines
2.0 KiB
Java
96 lines
2.0 KiB
Java
package com.example.model;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.Date;
|
|
|
|
/**
|
|
* 基础实体类
|
|
* 包含所有实体通用的字段和方法
|
|
*/
|
|
public abstract class BaseEntity implements Serializable {
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private Long id;
|
|
private Date createdAt;
|
|
private Date updatedAt;
|
|
private Boolean active;
|
|
|
|
public BaseEntity() {
|
|
// 默认为激活状态
|
|
this.active = true;
|
|
|
|
// 默认设置创建和更新时间
|
|
Date now = new Date();
|
|
this.createdAt = now;
|
|
this.updatedAt = now;
|
|
}
|
|
|
|
/**
|
|
* 判断实体是否为新建(未持久化)状态
|
|
*/
|
|
public boolean isNew() {
|
|
return id == null;
|
|
}
|
|
|
|
/**
|
|
* 判断实体是否为激活状态
|
|
*/
|
|
public boolean isActive() {
|
|
return active == null ? false : active;
|
|
}
|
|
|
|
/**
|
|
* 激活实体
|
|
*/
|
|
public void activate() {
|
|
this.active = true;
|
|
this.updatedAt = new Date();
|
|
}
|
|
|
|
/**
|
|
* 停用实体
|
|
*/
|
|
public void deactivate() {
|
|
this.active = false;
|
|
this.updatedAt = new Date();
|
|
}
|
|
|
|
/**
|
|
* 更新实体的最后修改时间
|
|
*/
|
|
public void touch() {
|
|
this.updatedAt = new Date();
|
|
}
|
|
|
|
public Long getId() {
|
|
return id;
|
|
}
|
|
|
|
public void setId(Long id) {
|
|
this.id = id;
|
|
}
|
|
|
|
public Date getCreatedAt() {
|
|
return createdAt;
|
|
}
|
|
|
|
public void setCreatedAt(Date createdAt) {
|
|
this.createdAt = createdAt;
|
|
}
|
|
|
|
public Date getUpdatedAt() {
|
|
return updatedAt;
|
|
}
|
|
|
|
public void setUpdatedAt(Date updatedAt) {
|
|
this.updatedAt = updatedAt;
|
|
}
|
|
|
|
public Boolean getActive() {
|
|
return active;
|
|
}
|
|
|
|
public void setActive(Boolean active) {
|
|
this.active = active;
|
|
}
|
|
} |