项目实体规范

已完成的文章

项目中用到的实体类

Form

接口传入表单实体类:xxxForm

1
2
3
4
5
@Data
@ApiModel("描述信息-xx表单")
public class xxxForm {
// 一般都是表字段充当属性
}

Query

查询条件实体类:xxxQuery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Data
public abstract class BasePageQuery implements Serializable {

private static final long serialVersionUID = 1L;

/**
* 当前页码
*/
@ApiModelProperty("当前页码")
private Long pageNum = 1L;

/**
* 每页条数
*/
@ApiModelProperty("每页条数")
private Long pageSize = 10L;

/**
* 排序字段
*/
@ApiModelProperty(value = "排序字段")
private List<SortInfo> sortInfos;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class xxxQuery extends BasePageQuery {

/**
* 最小添加时间
*/
private Date minAddTime;

/**
* 最大添加时间
*/
private Date maxAddTime;
}

Entity

数据对象(Po 持久对象):对应表名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("表名称")
public class xxx implements Serializable {

private static final long serialVersionUID = 1L;

/**
* 主键
*/
@TableId(value = "xxx_id", type = IdType.AUTO)
private Long id;

// ...

/**
* 创建人的用户id
*/
private Long createBy;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新人
*/
private Long updateBy;
/**
* 更新时间
*/
private Date updateTime;
/**
* 删除标志:0.未删除;1.已删除
*/
private Integer delFlag;
}

Bo

业务对象:xxxData

1
2
3
4
5
6
7
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class xxxData {
// 一般包含多个Po对象等其他属性
}

Dto

数据传输对象:xxxDto

1
2
3
public abstract class BaseDTO implements Serializable {
private static final long serialVersionUID = 1L;
}
1
2
3
4
5
6
7
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class xxxDTO extends BaseDTO {
// 一般都是表字段充当属性
}

Vo

视图对象:xxxVo

1
2
public abstract class BaseVO {
}
1
2
3
4
5
@Data
@ApiModel("描述信息-视图对象")
public class xxxVO extends BaseVO {
// 一般都是表中去除敏感字段后的字段充当属性
}

Do

DO主要有两个版本:

一个是阿里巴巴的开发手册中的定义:

DO( Data Object)这个等同于上面的 PO

另一个是在DDD(Domain-Driven Design)领域驱动设计中

DO(Domain Object)这个等同于上面的 BO

PageEntity

分页实体集合:PageEntity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("分页实体对象")
public class PageEntity<T> implements Serializable {
/**
* 总条数
*/
@ApiModelProperty("总条数")
private long total;

/**
* 数据集合
*/
@ApiModelProperty("数据集合")
private List<T> data;
}

PageResponse

分页响应对象:PageResponse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("分页响应对象")
public class PageResponse<T> extends PageEntity<T> {

/**
* 响应码
*/
@ApiModelProperty("返回码")
private String code;

/**
* 响应消息
*/
@ApiModelProperty("返回消息")
private String msg;

/**
* 成功操作并返回数据
* @param pageEntity
* @param <T>
* @return
*/
public static <T> PageResponse<T> success(PageEntity<T> pageEntity) {
PageResponse<T> pageResponse = new PageResponse<>();
pageResponse.setCode(ResponseCode.SUCCESS.getCode());
pageResponse.setMsg(ResponseCode.SUCCESS.getMsg());
pageResponse.setData(pageEntity.getData());
pageResponse.setTotal(pageEntity.getTotal());
return pageResponse;
}

}

Response

实体和集合接口响应消息体:Response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("响应对象")
public class Response<T> implements Serializable {

/**
* 响应码
*/
@ApiModelProperty("响应码")
private String code;

/**
* 响应消息
*/
@ApiModelProperty("响应消息")
private String msg;

/**
* 返回数据
*/
@ApiModelProperty("返回数据")
private T data;

/**
* 成功操作
*
* @param <T>
* @return
*/
public static <T> Response<T> success() {
return success(null);
}

/**
* 成功操作并返回数据
*
* @param data
* @param <T>
* @return
*/
public static <T> Response<T> success(T data) {
return result(ResponseCode.SUCCESS, data);
}

/**
* 失败操作
*
* @return
*/
public static <T> Response<T> failed() {
return result(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMsg(), null);
}

/**
* 失败操作并返回原因
*
* @param msg
* @return
*/
public static <T> Response<T> failed(String msg) {
return result(ResponseCode.ERROR.getCode(), msg, null);
}

/**
* 失败操作并指定异常原因
*
* @param responseCode
* @return
*/
public static <T> Response<T> failed(ErrorCode responseCode) {
return result(responseCode.getCode(), responseCode.getMsg(), null);
}

/**
* 失败操作并指定异常原因
* @param code
* @param msg
* @param <T>
* @return
*/
public static <T> Response<T> failed(String code, String msg) {
return result(code, msg, null);
}


/**
* 根据条件判断返回成功消息还是失败消息
* 一般用于更新、新增、删除
*
* @param state
* @param <T>
* @return
*/
public static <T> Response<T> judge(boolean state) {
if (state) {
return success();
} else {
return failed();
}
}

private static <T> Response<T> result(ErrorCode resultCode, T data) {
return result(resultCode.getCode(), resultCode.getMsg(), data);
}

private static <T> Response<T> result(String code, String msg, T data) {
return new Response<>(code, msg, data);
}

}

其他

ErrorCode

错误码接口:ErrorCode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface ErrorCode {

/**
* 获取错误码
* @return
*/
String getCode();

/**
* 获取错误描述
* @return
*/
String getMsg();
}

BusinessException

业务异常:BusinessException

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Getter
public class BusinessException extends RuntimeException {

private ErrorCode errorCode;

public BusinessException(ErrorCode errorCode) {
super(errorCode.getMsg());
this.errorCode = errorCode;
}

public BusinessException(String message) {
super(message);

}

public BusinessException(String message, Throwable cause){
super(message, cause);
}

public BusinessException(Throwable cause){
super(cause);
}
}

Constants

常量字典:DictionaryConstants

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
public interface DictionaryConstants {
/**
* 空字符串.
*/
String BLANK = "";

/**
* 真.
*/
String TRUE = "true";

/**
* 假.
*/
String FALSE = "false";

/**
* 16进制.
*/
int HEX = 0x10;

/**
* 零.
*/
int ZERO = 0;

/**
* 一.
*/
int ONE = 1;

/**
* 二.
*/
int TWO = 2;

/**
* 三.
*/
int THREE = 3;

/**
* 四.
*/
int FOUR = 4;

/**
* 五.
*/
int FIVE = 5;

/**
* 六.
*/
int SIX = 6;

/**
* 七.
*/
int SEVEN = 7;

/**
* 八.
*/
int EIGHT = 8;

/**
* 九.
*/
int NINE = 9;

/**
* 十.
*/
int TEN = 10;

/**
* -1.
*/
int NEGATIVE_ONE = -1;

/**
* 空格.
*/
String WHITE_SPACE = " ";

/**
* 乘.
*/
char MULTIPLY = '*';

/**
* 下划线.
*/
char UNDERLINE = '_';

/**
* 双引号.
*/
char DOUBLE_QUOTE = '"';

/**
* 引号.
*/
char QUOTE = '\'';

/**
* 且.
*/
char AND = '&';

/**
* 或.
*/
char OR = '|';

/**
* 逗号.
*/
char COMMA = ',';

/**
* 句点.
*/
char DOT = '.';

/**
* 减.
*/
char MINUS = '-';

/**
* 转义符.
*/
char ESCAPE = '\\';

/**
* 除.
*/
char DIVIDE = '/';

/**
* 加.
*/
char PLUS = '+';

/**
* 等于.
*/
char EQUAL = '=';

/**
* 问号.
*/
char QUESTION = '?';

/**
* 大于.
*/
char GT = '>';

/**
* 小于.
*/
char LT = '<';
}

GlobalExceptionHandler

全局系统异常处理:GlobalExceptionHandler

1
2
3
4
5
6
7
8
9
10
11
12
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
public <T> Response<T> handleException(Exception e) {
log.error(e.getMessage(), e);
return Response.failed(e.getLocalizedMessage());
}

}
------ 本文结束,感谢您的阅读 ------
请我喝杯咖啡~
支付宝打赏
微信打赏