# VO & DTO 层
# VO(View Object)视图层
VO层用来返回数据给前端,如返回
员工的详情页相关数据
,这也是diboot的@Bind*无SQL关联数据绑定
的应用场景。
Diboot未对VO层做任何封装,如无特殊要求,一般建议VO直接继承自Entity,避免重复定义entity中的已有字段。
返回给前端,数据库中查询的数据一般需要做如下处理:
- 将字典的存储值转换为显示值,如将性别
F
转换为女
(使用@BindDict注解) - 将关联对象id值转换为名称的显示值,如将部门id
1001
转换为研发部
(使用@BindField/@BindFieldList注解) - 将关联的子对象的数据加进来一并返回,如员工信息接口中返回员工基础信息和
员工履历
记录信息(使用@BindEntity/@BindEntityList注解)
- 将字典的存储值转换为显示值,如将性别
VO示例代码(含@Bind*关联数据绑定注解):
public class EmployeeVO extends Employee {
private static final long serialVersionUID = 12345L;
// 字典绑定转换
@BindDict(type="GENDER", field = "gender")
private LabelValue genderLabel;
// 关联字段绑定转换
@BindField(entity = Department.class, field = "name", condition = "this.department_id = id")
private String departmentName;
// 关联对象绑定转换
@BindEntityList(entity = WorkExperience.class, condition = "this.id = employee_id")
private List<WorkExperience> workExperienceList;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# DTO(Data Transfer Object)数据传输层
DTO层一般用来接收前端提交过来的数据,用于后端映射转换为查询或者更新操作。如
新增员工的表单页提交过来的数据
。 DTO根据情况可继承或者不继承对应的Entity。
- DTO示例代码:
public class EmployeeFormDTO extends Employee {
// 一并提交关联的子对象数据
private List<WorkExperience> workExperienceList;
}
1
2
3
4
2
3
4
- 如果DTO对象用于后端查询,或者后端不使用DTO,直接使用Entity映射接收前端请求参数时,基于前端参数动态构建查询SQL,这是 @BindQuery 的适用场景。
使用示例如下(具体文档可参考
无SQL查询条件构建
):
public class Employee extends BaseEntity {
private static final long serialVersionUID = 8670003133709715087L;
@BindQuery(comparison = Comparison.LIKE, strategy = Strategy.IGNORE_EMPTY)
private String name;
}
1
2
3
4
5
6
2
3
4
5
6