一款 IDEA 插件帮你优雅转化 DTO、VO、BO、PO、DO
程序IT圈
共 5897字,需浏览 12分钟
·
2022-04-26 07:27
https://juejin.cn/post/6952848675924082718
https://juejin.cn/post/6844904046097072141
https://zhuanlan.zhihu.com/p/264675395
1. 定义方法出入参
2. 光标定位方法内,使用快捷键ALT+INSERT(WIN) 、 command + N(mac) ,或者右键鼠标选择Generate,弹出生成选项框后,选择genCopyMethod,代码就生成好了
@Data
public class UserVO {
private String name;
private Date entryDate;
private String userId;
private List 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 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 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、Spring BeanUtils (copyProperties)
2、Cglib BeanCopier (copyProperties)
3、Apache BeanUtils (copyProperties)
4、Apache PropertyUtils (copyProperties)
5、Dozer
6、mapstruct
7、JSON 序列化 再反序列化
1. mapstruct
@Mapper(componentModel = "spring",uses = {RoleVOMapper.class,RoomVOMapper.class})
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("尚德楼");
}
}
@Data
public class UserVO {
private String name;
private Date entryDate;
private String userId;
private List roleList;
private RoomVO room;
public static UserVO convertToUserVO(UserDTO item) {
if (item == null) {
return null;
}
UserVO result = new UserVO();
BeanUtils.copyProperties(item,result);
List 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;
}
}
来源:juejin.cn/post/7053264631262871583
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。 获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
评论