1、什么是DTO、VO、BO、PO、DO、POJO
POJO的定义是无规则简单的对象,在日常的代码分层中pojo会被分为VO、BO、 PO、 DTO
VO (view object/value object)表示层对象
1、前端展示的数据,在接口数据返回给前端的时候需要转成VO2、使用场景,接口层服务中,将DTO转成VO,返回给前台
B0(bussines object)业务层对象
1、主要在服务内部使用的业务对象2、可以包含多个对象,可以用于对象的聚合操作3、使用场景在服务层服务中,由DTO转成BO然后进行业务处理后,转成DTO返回到接口层
PO(persistent object)持久对象
1、出现位置为数据库数据,用来存储数据库提取的数据2、只存储数据,不包含数据操作3、使用场景在数据库层中,获取的数据库数据存储到PO中,然后转为DTO返回到服务层中
DTO(Data Transfer Object)数据传输对象
1、在服务间的调用中,传输的数据对象2、DTO是可以存在于各层服务中(接口、服务、数据库等等)服务间的交互使用DTO来解耦DO(domain object)领域实体对象
DO 现在主要有两个版本:
①阿里巴巴的开发手册中的定义,DO( Data Object)这个等同于上面的PO②DDD(Domain-Driven Design)领域驱动设计中,DO(Domain Object)这个等同于上面的BO
2、插件如何完成转化
插件名称:Simple Object Copy1、定义方法出入参2、光标定位方法内,使用快捷键ALT+INSERT(WIN) 、 command + N(mac) ,或者右键鼠标选择Generate,弹出生成选项框后,选择genCopyMethod,代码就生成好了
复杂对象转化展示
代码展示:
@Datapublic%20class%20UserVO%20{private%20String%20name;private%20Date%20entryDate;private%20String%20userId;private%20List%20roleList;private%20RoomVO%20room;public%20static%20UserVO%20convertToUserVO(UserDTO%20item)%20{%20%20%20%20if%20(item%20==%20null)%20{%20%20%20%20%20%20%20%20return%20null;%20%20%20%20}%20%20%20%20UserVO%20result%20=%20new%20UserVO();%20%20%20%20result.setName(item.getName());%20%20%20%20result.setEntryDate(item.getEntryDate());%20%20%20%20result.setUserId(item.getUserId());%20%20%20%20List<RoleDTO>%20roleList%20=%20item.getRoleList();%20%20%20%20if%20(roleList%20==%20null)%20{%20%20%20%20%20%20%20%20result.setRoleList(null);%20%20%20%20}%20else%20{%20result.setRoleList(roleList.stream().map(UserVO::convertToRoleVO).collect(Collectors.toList());%20%20%20%20}%20%20%20%20result.setRoom(convertToRoomVO(item.getRoom()));%20%20%20%20return%20result;}public%20static%20RoomVO%20convertToRoomVO(RoomDTO%20item)%20{%20%20%20%20if%20(item%20==%20null)%20{%20%20%20%20%20%20%20%20return%20null;%20%20%20%20}%20%20%20%20RoomVO%20result%20=%20new%20RoomVO();%20%20%20%20result.setRoomId(item.getRoomId());%20%20%20%20result.setBuildingId(item.getBuildingId());%20%20%20%20result.setRoomName();%20%20%20%20result.setBuildingName();%20%20%20%20return%20result;}public%20static%20RoleVO%20convertToRoleVO(RoleDTO%20item)%20{%20%20%20%20if%20(item%20==%20null)%20{%20%20%20%20%20%20%20%20return%20null;%20%20%20%20}%20%20%20%20RoleVO%20result%20=%20new%20RoleVO();%20%20%20%20result.setRoleId(item.getRoleId());%20%20%20%20result.setRoleName(item.getRoleName());%20%20%20%20result.setCreateTime(item.getCreateTime());%20%20%20%20return%20result;}}@Datapublic%20class%20UserDTO%20{private%20String%20name;private%20Date%20entryDate;private%20String%20userId;private%20List%20roleList;private%20RoomDTO%20room;}@Datapublic%20class%20RoleVO%20{private%20String%20roleId;private%20String%20roleName;private%20LocalDateTime%20createTime;}@Datapublic%20class%20RoleDTO%20{private%20String%20roleId;private%20String%20roleName;private%20LocalDateTime%20createTime;}@Datapublic%20class%20RoomVO%20{private%20String%20roomId;private%20String%20buildingId;private%20String%20roomName;private%20String%20buildingName;}@Datapublic%20class%20RoomDTO%20{private%20String%20roomId;private%20String%20buildingId;}
3、其他转化方式
1、无入侵
市面上有很多类似的工具类,比较常用的有
- Spring%20BeanUtils%20(copyProperties)
- Cglib%20%20BeanCopier%20(copyProperties)
- Apache%20BeanUtils%20(copyProperties)
- Apache%20PropertyUtils%20(copyProperties)
- Dozer
- mapstruct
- JSON%20序列化%20再反序列化
这些工具,不仅要引入相应的依赖jar包,而且对代码有入侵,要调用对应得api方法才能进行转化,一旦遇到类型不一致,字段名稍有变动,就需要另写java代码补全字段,整体代码非常丑陋。
2、性能优势
相比上面的工具类,不是使用反射、就是是用代理、序列化操作。相比于纯正的set方法去转化,差距不是一个量级。此次不赘述。
3、灵活性、兼容性
跟上述工具类相比插件有很大优势,不再赘述,下面比较一下常用的idea插件generateO2O
| 插件 | 特点 |
|---|---|
| Simple%20Object%20Copy | 依据返回值为主,根据字段名去匹配,不会导致返回值漏属性 |
| 支持对象包含对象、对象包含list、set集合的转化 | |
| generateO2O | 以入参为主匹配字段,存在漏属性的情况 |
| 不支持子对象的转化、不支持list泛型不同的转化 |
在此推荐一个其他常用插件:generateAllSetter,搭配使用更佳
4、如何下载
打开idea%20plugins,切market%20place%20搜索:Simple%20Object%20Copy
5、插件优点
1、可以节省一个个字段的设置的开发时间2、避免了漏字段设置,ps:前端同学总是来问为啥字段总是null。3、而且通过出入参的设计思想去开发,规范了代码,在有特殊请求转化的时候也比较方便。 :::danger 注意:该插件需要付费,6元(人民币)每年,当然学生、教育机构、公益免费。 :::
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。









评论