一款 IDEA 插件帮你优雅转化 DTO、VO、BO、PO、DO
大家好,我是磊哥。
POJO 的定义是无规则简单的对象,在日常的代码分层中 pojo 会被分为VO、BO、 PO、 DTO
VO (view object/value object)表示层对象
1、前端展示的数据,在接口数据返回给前端的时候需要转成VO
2、个人理解使用场景,接口层服务中,将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
参考文档:
https://juejin.cn/post/6952848675924082718
https://juejin.cn/post/6844904046097072141
https://zhuanlan.zhihu.com/p/264675395
插件名称:Simple Object Copy
public class UserVO {
private String name;
private Date entryDate;
private String userId;
private List<RoleVO> roleList;
private RoomVO room;
public static UserVO convertToUserVO(UserDTO item) {
if (item == null) {
return null;
}
UserVO result = new UserVO();
result.setName(item.getName());
result.setEntryDate(item.getEntryDate());
result.setUserId(item.getUserId());
List<RoleDTO> roleList = item.getRoleList();
if (roleList == null) {
result.setRoleList(null);
} else {
result.setRoleList(roleList.stream().map(UserVO::convertToRoleVO).collect(Collectors.toList());
}
result.setRoom(convertToRoomVO(item.getRoom()));
return result;
}
public static RoomVO convertToRoomVO(RoomDTO item) {
if (item == null) {
return null;
}
RoomVO result = new RoomVO();
result.setRoomId(item.getRoomId());
result.setBuildingId(item.getBuildingId());
result.setRoomName();
result.setBuildingName();
return result;
}
public static RoleVO convertToRoleVO(RoleDTO item) {
if (item == null) {
return null;
}
RoleVO result = new RoleVO();
result.setRoleId(item.getRoleId());
result.setRoleName(item.getRoleName());
result.setCreateTime(item.getCreateTime());
return result;
}
}
@Data
public class UserDTO {
private String name;
private Date entryDate;
private String userId;
private List<RoleDTO> roleList;
private RoomDTO room;
}
@Data
public class RoleVO {
private String roleId;
private String roleName;
private LocalDateTime createTime;
}
@Data
public class RoleDTO {
private String roleId;
private String roleName;
private LocalDateTime createTime;
}
@Data
public class RoomVO {
private String roomId;
private String buildingId;
private String roomName;
private String buildingName;
}
@Data
public class RoomDTO {
private String roomId;
private String buildingId;
}
1. mapstruct
public interface UserMapper {
UserConverter INSTANCE = Mappers.getMapper(UserConverter.class);
UserVO toUserVO(UserDTO userDTO);
}
@Mapper(componentModel = "spring")
public interface RoleMapper {
RoleVO toRoleVO(RoleDTO roleDTO);
}
@Mapper(componentModel = "spring")
public interface RoomMapper {
RoomVO toRoomVO(RoomDTO roomDTO);
}
public class Main {
public static void main(String[] args) {
UserDTO user = ;
UserVO userVO = UserMapper.INSTANCE.toUserVO(user);
userVO.getRoomVO().setRoomName("大厅1");
userVO.getRoomVO().setBuildingName("尚德楼");
}
}
public class UserVO {
private String name;
private Date entryDate;
private String userId;
private List<RoleVO> roleList;
private RoomVO room;
public static UserVO convertToUserVO(UserDTO item) {
if (item == null) {
return null;
}
UserVO result = new UserVO();
BeanUtils.copyProperties(item,result);
List<RoleDTO> roleList = item.getRoleList();
if (roleList == null) {
result.setRoleList(null);
} else {
result.setRoleList(roleList.stream().map(UserVO::convertToRoleVO).collect(Collectors.toList());
}
result.setRoom(convertToRoomVO(item.getRoom()));
return result;
}
public static RoomVO convertToRoomVO(RoomDTO item) {
if (item == null) {
return null;
}
RoomVO result = new RoomVO();
BeanUtils.copyProperties(item,result);
result.setRoomName();
result.setBuildingName();
return result;
}
public static RoleVO convertToRoleVO(RoleDTO item) {
if (item == null) {
return null;
}
RoleVO result = new RoleVO();
BeanUtils.copyProperties(item,result);
return result;
}
}
近期技术热文
往期推荐