- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
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
定义方法出入参
光标定位方法内,使用快捷键ALT+INSERT(WIN) 、 command + N(mac) ,或者右键鼠标选择Generate,弹出生成选项框后,选择genCopyMethod,代码就生成好了
需要手写的代码
一键生成后的展示
复杂对象转化展示(一键生成后的展示)
复杂对象转化源码示例:
@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;
}
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.无入侵市面上有很多类似的工具类,比较常用的有
1、Spring BeanUtils (copyProperties)
2、Cglib BeanCopier (copyProperties)
3、Apache BeanUtils (copyProperties)
4、Apache PropertyUtils (copyProperties)
5、Dozer
6、mapstruct
7、JSON 序列化 再反序列化
这些工具,不仅要引入相应的依赖jar包,而且对代码有入侵,要调用对应得api方法才能进行转化,一旦遇到类型不一致,字段名稍有变动,就需要另写java代码补全字段,整体代码非常丑陋。
举例:
同样的代码,,不仅要引入依赖、写如下转化mapper,还要,在对应地方调用对应api(代码入侵验证),然而Simple Object Copy 只需要一键生成。
RoomDTO中不存在的roomName、buildingName还要mapstruct另写方法,很容易忽略。源实体中不存在的属性,没有提示,小心前端总是问为啥都是null。
在Simple Object Copy 插件代码生成后,不存在的字段也生成了空方法,直接编译提示补充,不容易忽略
需要手写的代码:
@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("尚德楼");
}
}
1. BeanUtils
性能稍差。
不支持复杂对象还是要写大量代码,代码字段不清晰不易理解,别人接手难。RoomDTO中不存在的roomName、buildingName还要BeanUtils另写方法,很容易忽略。源实体中不存在的属性,没有提示,小心前端总是问为啥都是null。
需要手写的代码
@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();
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;
}
}
2.性能优势
相比上面的工具类,不是使用反射、就是是用代理、序列化操作。相比于纯正的set方法去转化,差距不是一个量级。此次不赘述。
3.灵活性、兼容性
跟上述工具类相比插件有很大优势,不再赘述,下面我们比较一下,我之前常用的idea插件generateO2O
在此推荐其他一个我常用插件:generateAllSetter,搭配食用更佳,
4. 如何下载?
打开idea plugins,切market place 搜索:Simple Object Copy
推荐3个原创springboot+Vue项目,有完整视频讲解与文档和源码:
关注我,学Java
您如何优雅编码同一tableView中的两种类型的单元格? 显然我可以这样: NSDictionary *cellInfo = [_userInformation objectAtIndex:inde
假设我正在编写一个仅包含标题或主要包含标题的库,并且具有以下代码: using my_type = int; namespace detail { inline void foo() { my
我正在使用复选框和输入进行一系列启用/禁用选择,我想知道我是否可以使用循环、变量或复合语句来简单地处理这个js?感觉就像是使用大量代码来实现相对简单的功能。 这是我正在做的事情的一个 fiddle :
我正在尝试为来自维基百科的 API 响应编写一个解析器。它真的很困惑,我已经求助于旧的 RegEx 来清理大部分东西。然而,我坚持这一点。考虑一个字符串: var a ="[[December 1]
我正在通过一个 channel 接收多个消息,并在对其进行迭代之后,我想保留最后一个元素以供进一步使用。我的第一个(可能很糟糕!)方法是声明一些变量,然后在每个循环中分配它。 let last = 0
我正在编写一个 PHP Web 应用程序,它将在不久的将来在生产环境下运行,而不是使用非用户友好的 die() , 我想我会想出一个 Class处理错误消息。 基本上,我的思考过程是这样的: 如果 W
我们有 elb 负载平衡 2 台运行 tomcat 作为应用程序服务器的 WAS 机器。要实现AWS环境下的不间断部署,我们应该, 选择部署目标 WAS。 让它停止来自 elb 的交易。(elb 暂停
何为pythonic? pythonic如果翻译成中文的话就是很python。很+名词结构的用法在中国不少,比如:很娘,很国足,很CCTV等等。 我的理解为,很+名词表达了一种特殊和强调的意味。
认为已经有对此的答案,但找不到。我一直在以某种方式解析方法选项,并想检查并确保它是最优雅/最简洁的方式。 这是我通常做的: def some_method *args options = args
我正在清理我的一个旧项目。它必须做的一件事是——给定笛卡尔网格系统和网格上的两个正方形,找到所有正方形的列表,连接这两个正方形中心的线将通过这些正方形。 这里的特殊情况是所有起点和终点都被限制在正方形
如何使系统 ( SystemB1 ) 访问另一个系统 ( SystemA::sub ) 的字段,就好像它是自己的字段一样? SystemA是一个拥有自己领域的实用系统 Sub* sub . Syste
我有一个包含约 8.000.000 条记录的 MySQL 数据库。因为我需要处理所有这些,所以我使用 BlockingQueue 作为生产者从数据库读取数据并将 1000 条记录放入队列中。 Cons
我正在让我的 HTTP 服务器正常关闭。我从帖子中获取了提示 here ,到目前为止,我的代码是这样设置的: func start() { //...... //START HTTP/
示例脚本只是“wc -m”命令的包装器,简单的符号计数器。我尝试只用“teststrings” slice 元素提供输入。并在输出监听器 goroutine 接收每个字符串的符号数。寻找一种让“wc”
我想干净/优雅地关闭 Internet Explorer。 taskkill 会关闭它,但是当重新打开它时,它会询问您是否要重新打开上一个 session 。 最佳答案 尝试 CloseMainWin
Haskell 的简洁和优雅给我留下了深刻的印象。但我在 .Net 公司工作,所以当我可以使用 F# 时我会使用它——我可能是全国数百个使用它的人中唯一的一个。 ADO.NET 或 F# 是否提供像
如果我们不想在我们的类中实现 init 方法,并且记住 NSObject 中的 init 只返回一个没有初始化的对象实例,如果我们已经得到了,我不明白调用 init 的意义带有分配的实例。我已经尝试过
我们的组织中有许多初级 Delphi 开发人员,作为向他们教授 Delphi 过程的一部分,我希望他们能够看到“干净”、编写良好、设计良好的 Delphi 代码。 我要寻找的一些标准包括: 优秀的类(
我有一个 3D 图像扫描(形状:335x306x306,总元素:31368060),我想用相同大小的 3D bool 掩码来掩盖它以返回相同大小的蒙版图像。 当我简单地用掩码索引数组时: masked
如何使适配器类适本地支持 const 和非 const 底层数据? 具体例子 RigidBody是描述对象物理属性的类。 这是其非常简化的版本(1D):- class RigidBody{ f
我是一名优秀的程序员,十分优秀!