java对象转换
对象转换: 对象的分层涉及到各个层级之间的对象转换(Entity2DTO , DTO2VO, VO2DTO,DTO2Entity等),传统的采用set/get 方法硬编码实现写的代码比较多;或者采用Bean的copy处理性能受影响
新的处理方式:采用工具在可以在编译器动态生成Java实现类,同时可以集成spring的生态体系,纯粹的是面向接口实现方式
实现方式:
-
项目中需要添加依赖配置
<org.mapstruct.version>
1.3
.
0
.Final</org.mapstruct.version>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
-
定义接口类设置映射关系
import
com.sinaif.core.dto.UserDto;
import
com.sinaif.core.entity.UserEntity;
import
org.mapstruct.Mapper;
import
org.mapstruct.Mapping;
import
org.mapstruct.Mappings;
import
java.util.List;
/**
* @Author allen_chen
* @Description 用户POJO对象转换接口
* @Date 2019/7/29 11:53
* @Param
* @return
**/
@Mapper
(componentModel =
"spring"
)
public
interface
UserTransfer {
/**
* @Author allen_chen
* @Description eentity转化为dto
* @Date 2019/7/29 11:53
* @Param [userEntity]
* @return com.example.demo.common.base.dto.UserDto
**/
@Mappings
({
@Mapping
(source =
"phone"
,target =
"cellphone"
),
@Mapping
(source =
"id"
,target =
"userId"
)
})
public
UserDto entity2Dto(UserEntity userEntity);
/**
* @Author allen_chen
* @Description dto转化为entity
* @Date 2019/7/29 11:53
* @Param [dto]
* @return com.example.demo.common.base.entity.UserEntity
**/
@Mappings
({
@Mapping
(source =
"cellphone"
,target =
"phone"
)
})
public
UserEntity dto2Entity(UserDto dto);
@Mappings
({
@Mapping
(source =
"cellphone"
,target =
"phone"
)
})
public
List<UserEntity> listDto2ListEntity(List<UserDto> dtoList);
@Mappings
({
@Mapping
(source =
"phone"
,target =
"cellphone"
)
})
public
List<UserDto> ListEntity2ListDto( List<UserEntity> entityList);
}
-
具体调用方式直接通过spring注入完成调用
@Autowired
UserTransfer userTransfer;
@Test
public
void
testUserEntity2DtoTransfer(){
UserEntity entity =
new
UserEntity();
entity.setName(
"allen"
);
entity.setNickName(
"allen.chen"
);
entity.setCreTime(
new
Date());
entity.setId(50L);
entity.setPhone(
"135060309"
);
UserDto dto = userTransfer.entity2Dto(entity);
Assert.assertNotNull(dto);
Assert.assertEquals(
"allen"
,dto.getName());
}
@Test
public
void
testUserDto2EntityTransfer(){
UserDto dto =
new
UserDto();
dto.setName(
"allen"
);
dto.setNickName(
"allen.chen"
);
UserEntity entity = userTransfer.dto2Entity(dto);
Assert.assertNotNull(entity);
Assert.assertEquals(
"allen"
,entity.getName());
}
- 注意事项:
1) 如果项目中有用到swagger的需要排除下依赖:
<exclusions> <exclusion> <artifactId>mapstruct</artifactId> <groupId>org.mapstruct</groupId> </exclusion> </exclusions> |
2)更多使用eclipse IDEA 工具需要参考官网地址:http://mapstruct.org/documentation/ide-support/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?