对于Domain,Form,Dto的一些理解

前言

目前公司的项目使用前后端分离的方式进行开发,后端负责接口的开发,接口返回json数据格式给前端。后端有三处地方用到实体类,分别是domain,form,dto。

domain,form和dto

domain

例示文件 ProjectReport.java

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.znco.zndata.data.domain.cpa;
import java.util.Date;
import com.znco.framework.cache.CacheLevel;
import com.znco.framework.cache.annotations.Cache;
import com.znco.framework.cache.annotations.KeyProperty;
@Cache(level = CacheLevel.JVM_TO_REDIS)
public class ProjectReport {
/**
* 项目报告id
*/
@KeyProperty
private Long id;
/**
* 关联项目id
*/
private Long projectId;
/**
* 关联业务id
*/
private Long busiId;
/**
* 业务类型
*/
private String busiType;
/**
* 报告年度
*/
private String reportYear;
/**
* 项目报告名称
*/
private String name;
/**
* 项目经理
*/
private Long manager;
/**
* 报告签章
*/
private String signature;
/**
* 项目主办人
*/
private Long reporter;
/**
* 状态
*/
private Byte status;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
/**
* 创建者,关联用户id
*/
private String creatorId;
/**
* 公司id
*/
private Long companyId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getProjectId() {
return projectId;
}
public void setProjectId(Long projectId) {
this.projectId = projectId;
}
public Long getBusiId() {
return busiId;
}
public void setBusiId(Long busiId) {
this.busiId = busiId;
}
public String getBusiType() {
return busiType;
}
public void setBusiType(String busiType) {
this.busiType = busiType;
}
public String getReportYear() {
return reportYear;
}
public void setReportYear(String reportYear) {
this.reportYear = reportYear;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getManager() {
return manager;
}
public void setManager(Long manager) {
this.manager = manager;
}
public Long getReporter() {
return reporter;
}
public void setReporter(Long reporter) {
this.reporter = reporter;
}
public Byte getStatus() {
return status;
}
public void setStatus(Byte status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getCreatorId() {
return creatorId;
}
public void setCreatorId(String creatorId) {
this.creatorId = creatorId;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
}

domain是与数据库一一对应的一个实体类,本项目用的是mybatis数据库框架,在xml文件中会引用这个实体类。

ProjectReportMapper.xml 文件

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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.znco.zndata.data.persistence.cpa.ProjectReportMapper">
<resultMap id="BaseResultMap" type="com.znco.zndata.data.domain.cpa.ProjectReport">
<id column="id" property="id" jdbcType="BIGINT" />
<result column="project_id" property="projectId" jdbcType="BIGINT" />
<result column="busi_id" property="busiId" jdbcType="BIGINT" />
<result column="busi_type" property="busiType" jdbcType="VARCHAR" />
<result column="report_year" property="reportYear" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="manager" property="manager" jdbcType="BIGINT" />
<result column="reporter" property="reporter" jdbcType="BIGINT" />
<result column="signature" property="signature" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="TINYINT" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
<result column="creator_id" property="creatorId" jdbcType="VARCHAR" />
<result column="company_id" property="companyId" jdbcType="BIGINT" />
</resultMap>
<sql id="Base_Column_List">
id, project_id, busi_id, busi_type, report_year, name, manager,
reporter, signature, status,
create_time, update_time, creator_id,
company_id
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap"
parameterType="com.znco.zndata.data.domain.cpa.ProjectReport">
select
<include refid="Base_Column_List" />
from cpa_report
where id = #{id,jdbcType=BIGINT}
</select>
<select id="selectByPrimaryKeySelective" resultMap="BaseResultMap"
parameterType="com.znco.zndata.data.domain.cpa.ProjectReport">
select
<include refid="Base_Column_List" />
from cpa_report
<where>
company_id = #{companyId,jdbcType=BIGINT}
<if test="name != null and name != ''">
AND name = #{name, jdbcType=VARCHAR}
</if>
<if test="manager != null">
AND manager = #{manager, jdbcType=BIGINT}
</if>
<if test="reporter !=null">
AND reporter = #{reporter, jdbcType=BIGINT}
</if>
<if test="status != null">
AND status = #{status, jdbcType=TINYINT}
</if>
<if test="projectId != null">
AND project_id = #{projectId}
</if>
<if test="busiId != null">
AND busi_id = #{busiId}
</if>
<if test="reportYear != null and reportYear !=''">
AND report_year = #{reportYear}
</if>
<if test="busiType != null and busiType != ''">
AND busi_type = #{busiType}
</if>
<if test="signature != null and signature != ''">
AND signature = #{signature}
</if>
</where>
</select>
<select id="selectByPagination" resultMap="BaseResultMap"
parameterType="map">
select
<include refid="Base_Column_List" />
from cpa_report
<where>
company_id = #{companyId,jdbcType=BIGINT}
<if test="name != null and name != ''">
AND name = #{name, jdbcType=VARCHAR}
</if>
<if test="manager != null">
AND manager = #{manager, jdbcType=BIGINT}
</if>
<if test="reporter !=null">
AND reporter = #{reporter, jdbcType=BIGINT}
</if>
<if test="status != null">
AND status = #{status, jdbcType=TINYINT}
</if>
<if test="projectId != null">
AND project_id = #{projectId}
</if>
<if test="busiId != null">
AND busi_id = #{busiId}
</if>
<if test="reportYear != null and reportYear !=''">
AND report_year = #{reportYear}
</if>
<if test="busiType != null and busiType != ''">
AND busi_type = #{busiType}
</if>
<if test="signature != null and signature != ''">
AND signature = #{signature}
</if>
</where>
</select>
<delete id="deleteByPrimaryKey" parameterType="com.znco.zndata.data.domain.cpa.ProjectReport">
delete from
cpa_report where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" useGeneratedKeys="true" keyProperty="id"
parameterType="com.znco.zndata.data.domain.cpa.ProjectReport">
insert into cpa_report (
id, project_id, busi_id, busi_type,
report_year, name, manager, reporter, signature, status, create_time, creator_id,
company_id
)values (
#{id,jdbcType=BIGINT},
#{projectId,jdbcType=BIGINT}, #{busiId, jdbcType=BIGINT}, #{busiType, jdbcType=VARCHAR},
#{reportYear, jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{manager,jdbcType=BIGINT},
#{reporter,jdbcType=BIGINT}, #{signature, jdbcType=VARCHAR},
#{status,jdbcType=TINYINT}, sysdate(), #{creatorId,jdbcType=VARCHAR},
#{companyId,jdbcType=BIGINT}
)
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.znco.zndata.data.domain.cpa.ProjectReport">
update cpa_report
<set>
<if test="projectId != null">
project_id = #{projectId,jdbcType=BIGINT},
</if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="manager != null">
manager = #{manager,jdbcType=BIGINT},
</if>
<if test="reporter != null">
reporter = #{reporter,jdbcType=BIGINT},
</if>
<if test="status != null">
status = #{status,jdbcType=TINYINT},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
<if test="companyId != null">
company_id = #{companyId,jdbcType=BIGINT},
</if>
<if test="signature != null and signature != ''">
signature = #{signature,jdbcType=BIGINT},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>

如果数据库表字段有变化,那么就需要修改该表对应的domain文件和xml文件。

form

form是前端提交的表单所对应的实体类,其文件格式和domain一样,都是属性+get和set方法。

dto

dto是后端返回给前端数据所对应的实体类,其文件格式也是属性+get和set方法

三者的关系

前端通过按构建好的form表单模型提交数据给后端,后端从form文件中取值出来赋值给domain文件,构建一个能被数据库所识别的domain模型,通过domain来查询数据库,得到查询结果。后端再将得到的查询结果赋值给dto文件,将dto作为模型返回给前端,前端就可以从dto中获取数据来渲染页面。这样无论是修改数据库,还是提交的表单数据,还是页面展示内容,只要修改对应的实体类就可以,相互不受影响。