Understanding POJO, VO, BO, DTO, DO and Using Simple Object Copy Plugin for Java Object Mapping
This article explains the differences between POJO, VO, BO, PO, DTO and DO in Java, demonstrates how the Simple Object Copy IDEA plugin can generate one‑click conversion methods, compares it with other mapping tools, and provides sample code for complex object transformations.
POJO (Plain Old Java Object) is a simple, rule‑less object that can be categorized into VO, BO, PO, DTO, and DO depending on its usage in different layers.
VO (View Object/Value Object) is used for data returned to the front‑end; it is typically created by converting a DTO in the service layer.
BO (Business Object) represents internal business data, may aggregate multiple objects, and is transformed from DTO before processing.
PO (Persistent Object) stores data retrieved from the database and does not contain business logic.
DTO (Data Transfer Object) is used for transferring data between services or layers, helping to decouple components.
DO (Domain Object) has two meanings: Alibaba’s Data Object (equivalent to PO) and DDD’s Domain Object (equivalent to BO).
The article introduces the Simple Object Copy IDEA plugin, which can generate conversion methods automatically. After defining the source and target classes, pressing ALT+INSERT (Windows) or command+N (macOS) and selecting genCopyMethod creates the mapping code.
Example of a generated conversion method:
@Data
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;
}
// other conversion methods ...
}The article also compares Simple Object Copy with other mapping tools such as Spring BeanUtils, Cglib BeanCopier, Apache BeanUtils, Dozer, MapStruct, and JSON serialization, highlighting issues like code intrusion, handling of mismatched fields, and performance overhead.
For MapStruct, a manual mapper interface is required:
@Mapper(componentModel = "spring", uses = {RoleVOMapper.class, RoomVOMapper.class})
public interface UserMapper {
UserVO toUserVO(UserDTO userDTO);
}
@Mapper(componentModel = "spring")
public interface RoleMapper {
RoleVO toRoleVO(RoleDTO roleDTO);
}
@Mapper(componentModel = "spring")
public interface RoomMapper {
RoomVO toRoomVO(RoomDTO roomDTO);
}Using BeanUtils involves reflection and can be slower; it also requires explicit handling of missing fields, which may lead to null values in the front‑end.
The article concludes with a recommendation of a Java Virtual Machine book and provides download instructions for the Simple Object Copy plugin via the IntelliJ IDEA Marketplace.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.