# 一些共性的屬性進行封裝
---
## 為什么要進行封裝
上一節數據庫表的創建和說明我們已經知道了每個數據庫表有一些共性的字段(創建熱,創建時間,修改人,修改時間,版本,狀態)。如果對這些共性的字段還在每個類寫上一遍,那這樣的代碼就太難看了。那么應該怎么辦呢?我們可以將其封裝成一個基礎類,然后其他類有這些屬性的類就繼承他們就行了。
比如上面的可以封裝如下:
`
~~~
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* 基礎實體
*
* @author : yyfly / developer@yyfly.com
* @date : 2018-08-08
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 正常
*/
public static final int NORMAL = 0;
/**
* 禁用
*/
public static final int DISABLE = 1;
/**
* 刪除
*/
public static final int DELETED = 9999;
/**
* 審核
*/
public static final int AUDIT = 3;
/**
* 唯一ID
*/
@Id
@GenericGenerator(name = "twitter-id", strategy = "com.yyfly.common.entity.TwitterIdGenerator")
@GeneratedValue(generator = "twitter-id")
private String id;
/**
* 創建人
*/
@CreatedBy
@Column(updatable = false)
private String createBy;
/**
* 創建時間
*/
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
@Column(updatable = false)
private Date createDate;
/**
* 最后一次修改人
*/
@LastModifiedBy
private String updateBy;
/**
* 最后一次修改時間
*/
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
private Date updateDate;
/**
* 版本
*/
@Version
private Long version;
/**
* 實體狀態
*/
private int status;
~~~
`
## 前后端分離的情況下,我們需要返回給前端的也得是標準的分離的返回結果。
比如
例子1訪問無權限

例子2訪問成功

也就是
**所有的返回結果都必須以**
{
code: 狀態碼
message: 返回提示信息
data: 返回的具體數據
}
那么對于這樣標準的返回結果,我們每次返回都要寫這樣的類,那可真是蛋疼。我們就可以封裝成一個RespoonseData類進行返回。
下面就是這樣的一個類
`
~~~
import com.yyfly.common.http.HTTP;
import com.yyfly.common.http.HTTP.Status;
import java.io.Serializable;
public class ResponseData implements Serializable {
private static final long serialVersionUID = -6936648847780505144L;
public Integer code;
public String message;
public Object data;
public String toString() {
return "ResponseData{code=" + this.code + ", message='" + this.message + '\'' + ", data=" + this.data + '}';
}
public static ResponseData success() {
return success("request succeeded");
}
public static ResponseData success(Object data) {
return success("request succeeded", data);
}
public static ResponseData success(String message, Object data) {
return success(HTTP.SC_OK, message, data);
}
public static ResponseData success(Integer code, String message, Object data) {
return build(code, message, data);
}
public static ResponseData error(Integer code) {
return error(code, "request fail");
}
public static ResponseData error(Integer code, String message) {
return error(code, message, (Object)null);
}
public static ResponseData error(Integer code, String message, Object data) {
return build(code, message, data);
}
public static ResponseData build(Status status) {
return build(status, (Object)null);
}
public static ResponseData build(Status status, Object data) {
return build(status.value(), status.getReasonPhrase(), data);
}
public static ResponseData build(Integer code, String message, Object data) {
return new ResponseData(code, message, data);
}
public static ResponseData.ResponseDataBuilder builder() {
return new ResponseData.ResponseDataBuilder();
}
public Integer getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
public Object getData() {
return this.data;
}
public void setCode(Integer code) {
this.code = code;
}
public void setMessage(String message) {
this.message = message;
}
public void setData(Object data) {
this.data = data;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof ResponseData)) {
return false;
} else {
ResponseData other = (ResponseData)o;
if (!other.canEqual(this)) {
return false;
} else {
label47: {
Object this$code = this.getCode();
Object other$code = other.getCode();
if (this$code == null) {
if (other$code == null) {
break label47;
}
} else if (this$code.equals(other$code)) {
break label47;
}
return false;
}
Object this$message = this.getMessage();
Object other$message = other.getMessage();
if (this$message == null) {
if (other$message != null) {
return false;
}
} else if (!this$message.equals(other$message)) {
return false;
}
Object this$data = this.getData();
Object other$data = other.getData();
if (this$data == null) {
if (other$data != null) {
return false;
}
} else if (!this$data.equals(other$data)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof ResponseData;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $code = this.getCode();
int result = result * 59 + ($code == null ? 43 : $code.hashCode());
Object $message = this.getMessage();
result = result * 59 + ($message == null ? 43 : $message.hashCode());
Object $data = this.getData();
result = result * 59 + ($data == null ? 43 : $data.hashCode());
return result;
}
public ResponseData() {
}
public ResponseData(Integer code, String message, Object data) {
this.code = code;
this.message = message;
this.data = data;
}
public static class ResponseDataBuilder {
private Integer code;
private String message;
private Object data;
ResponseDataBuilder() {
}
public ResponseData.ResponseDataBuilder code(Integer code) {
this.code = code;
return this;
}
public ResponseData.ResponseDataBuilder message(String message) {
this.message = message;
return this;
}
public ResponseData.ResponseDataBuilder data(Object data) {
this.data = data;
return this;
}
public ResponseData build() {
return new ResponseData(this.code, this.message, this.data);
}
public String toString() {
return "ResponseData.ResponseDataBuilder(code=" + this.code + ", message=" + this.message + ", data=" + this.data + ")";
}
}
}
~~~
`
具體用的時候我們只需要調用就可以了。
比如說我們要返回一個成功的結果。
我們可以通過這樣
`return ResponseData.success(userService.toDTOs(users));`
## 利用車輪
### 如果每次新寫一個項目都得要自己在寫這些類,那豈不是會爆肝。這里我們可以直接將類封裝好后,打包成jar提交到maven的中央倉庫。這樣我們就可以直接使用它了。這里我提供一個已經封裝好的這些類的maven倉庫地址。大家可以將依賴導入自己的pom.xml.
[https://mvnrepository.com/artifact/com.yyfly/common/1.0.6](https://mvnrepository.com/artifact/com.yyfly/common/1.0.6)
在這個依賴中還有很多我們需要用到的實體類,大家可以把他當工具來用,也可以好好的研讀一下它的源碼。增進自己的功力。
其他封裝的實體類我們會在后面結合代碼給大家一一地講述。
# 好了,前置的知識點我們就講述到這里。后面我們將會繼續進行代碼的編寫。