🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 一些共性的属性进行封装 --- ## 为什么要进行封装 上一节数据库表的创建和说明我们已经知道了每个数据库表有一些共性的字段(创建热,创建时间,修改人,修改时间,版本,状态)。如果对这些共性的字段还在每个类写上一遍,那这样的代码就太难看了。那么应该怎么办呢?我们可以将其封装成一个基础类,然后其他类有这些属性的类就继承他们就行了。 比如上面的可以封装如下: ` ~~~ 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访问无权限 ![](https://img.kancloud.cn/c0/95/c0950fe3a489ded3bccd78d566f49c2f_491x178.png) 例子2访问成功 ![](https://img.kancloud.cn/bb/8e/bb8e4d295d517f9883f90f9b1787650d_633x682.png) 也就是 **所有的返回结果都必须以** { 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) 在这个依赖中还有很多我们需要用到的实体类,大家可以把他当工具来用,也可以好好的研读一下它的源码。增进自己的功力。 其他封装的实体类我们会在后面结合代码给大家一一地讲述。 # 好了,前置的知识点我们就讲述到这里。后面我们将会继续进行代码的编写。