当我们在代码中遇到实体类之间相关转换的时候,最常用的应该就是BeanUtils.copyProperties();
方法了吧,但是这个方法只能转换同名,同类型的属性,如果名称不同,属性不同,则不会转换成功,例如,现在有如下两个实体类:
@Data
@AllArgsConstructor
class SourceEntity{
private Integer id;
private Integer parentId;
private String name;
private LocalDateTime createDate;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class TargetEntity{
private Integer id;
private String parentId;
private String realName;
private Date updateDate;
}
使用BeanUtils进行实体类转换
public class TestService {
public static void main(String[] args) {
SourceEntity sourceEntity = new SourceEntity(1, 0, "ralph", LocalDateTime.now());
TargetEntity targetEntity = new TargetEntity();
BeanUtils.copyProperties(sourceEntity,targetEntity);
System.out.println(targetEntity);
}
}
转换结果:
TargetEntity(id=1, parentId=null, realName=null, updateDate=null)
这个结果应该都在大家意料之中了,那如果想要让两个实体类属性全部转换,是不是需要一个一个进行set
public static void main(String[] args) {
SourceEntity sourceEntity = new SourceEntity(1, 0, "ralph", LocalDateTime.now());
TargetEntity targetEntity = new TargetEntity();
BeanUtils.copyProperties(sourceEntity,targetEntity);
targetEntity.setParentId(sourceEntity.getParentId().toString());
targetEntity.setRealName(sourceEntity.getName());
targetEntity.setUpdateDate(DateUtil.parse(sourceEntity.getCreateDate().toString()));
System.out.println(targetEntity);
}
结果:
TargetEntity(id=1, parentId=0, realName=ralph, updateDate=2023-02-11 19:39:10)
显而易见,能过这种方式达到了我们想要的效果,但是这样的方式过于繁琐,而且如果想要复用,必须得抽一个公共方法出来,如果需要转换的类比较多那每一个类都需要抽一个方法。
接下来就该由本文的主角-Mapstruct全场
<dependency>
<groupId>org.mapstructgroupId>
<artifactId>mapstructartifactId>
<version>1.3.1.Finalversion>
dependency>
<dependency>
<groupId>org.mapstructgroupId>
<artifactId>mapstruct-processorartifactId>
<version>1.3.1.Finalversion>
dependency>
package com.ralph.business.article.utils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* 实体类转换接口,即使字段名称,类型不一致,也可实现转换
*/
@Mapper(componentModel = "spring")
public interface EntityConvert {
/**
* 获取该类自动生成的实现类的实例
* 接口中的属性都是 public static final 的 方法都是public abstract的
*/
EntityConvert INSTANCES = Mappers.getMapper(EntityConvert.class);
@Mappings({
@Mapping(target = "parentId", expression = "java(source.getParentId().toString())"),
@Mapping(source = "name", target = "realName"),
@Mapping(source = "createDate", target = "updateDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
})
TargetEntity toTargetEntity(SourceEntity source);
}
public class TestService {
public static void main(String[] args) {
SourceEntity sourceEntity = new SourceEntity(1, 0, "ralph", LocalDateTime.now());
TargetEntity targetEntity = EntityConvert.INSTANCES.toTargetEntity(sourceEntity);
System.out.println(targetEntity);
}
}
TargetEntity(id=1, parentId=0, realName=ralph, updateDate=Sun Feb 12 03:48:10 CST 2023)
可以看到,同样完成了类型转换,而且代码更加简洁,如果有多个实体类需要转换,只需要在接口中增加对应的方法即可,通过@Mapping
注解标注名称或类型不同的属性,相同的可以不加,会自动转换
当写好了一个转换方法,想要将转换关系反过来时,只需要加一个注解即可实现@InheritInverseConfiguration
@Mappings({
@Mapping(target = "parentId", expression = "java(source.getParentId().toString())"),
@Mapping(source = "name", target = "realName"),
@Mapping(source = "createDate", target = "updateDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
})
TargetEntity toTargetEntity(SourceEntity source);
@InheritInverseConfiguration
SourceEntity toTargetEntity(TargetEntity source);
这样SourceEntity toTargetEntity(TargetEntity source);
方法就可以将TargetEntity转换为SourceEntity而不需要重新写@Mapping注解
即将多个对象的值合并赋值给一个对象
@Mappings({
@Mapping(target = "parentId", expression = "java(source.getParentId().toString())"),
@Mapping(source = "article.name", target = "realName"),
@Mapping(source = "source.createDate", target = "updateDate", dateFormat = "yyyy-MM-dd HH:mm:ss")
})
TargetEntity mergeEntity(SourceEntity source, BusinessArticle article);
当源对象的属性没有值时,可以指定一个默认值
@Mapping(source = "name", target = "realName", defaultValue = "defaultName")
TargetEntity defaultValue(SourceEntity source);
如果SourceEntity的name为空,则会默认赋值为defaultName
如果想在其他地方通过@Autoware方式注入转换类,需要将转换类的@Mapper的componentModel属性设置为spring,即@Mapper(componentModel = "spring")
当我们想根据自已的想法进行映射时,可以自定义映射规则
@Named("sexFormat")
class CusMapper{
@Named("sexFormat")
String toSex(Integer sex){
if (0 == sex){
return "男";
}else {
return "女";
}
}
}
转换类上将自定义映射导入
@Mapper(componentModel = “spring”,uses = {CusMapper.class})
在转换方法上使用自定义映射规则
@Mapping(source = "sexInt", target = "sex", qualifiedByName = "sexFormat") TargetEntity sexMapper(SourceEntity source);
java: Internal error in the mapping processor: java.lang.NullPointerException
…
解决方法:
按以下方法在修改idea的设置
Setting -->Build,Execution,Deployment -->Compiler -->User-local build
加上参数:
-Djps.track.ap.dependencies=false
java: No property named “name” exists in source parameter(s). Did you mean “id”?
根据报错信息得知是没有找到name这个属性,但是我们的实体类是有name这个属性的,在查询后得知,是因为lombok插件导致的
解决方法:
1、将lombok的依赖放在mapstruct前面
2、修改lombok与mapstruct的版本,使两者版本适配
3、在pom文件中增加binding插件,将mapstruct与lombok进行绑定,具体内容如下
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.5.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstructgroupId>
<artifactId>mapstruct-processorartifactId>
<version>1.3.1.Finalversion>
path>
<path>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.20version>
path>
<path>
<groupId>org.projectlombokgroupId>
<artifactId>lombok-mapstruct-bindingartifactId>
<version>0.2.0version>
path>
annotationProcessorPaths>
configuration>
plugin>
plugins>
build>
欢迎前往博客主页查看更多内容
如果觉得不错,期待您的点赞、收藏、评论、关注
如有错误欢迎指正!
Gitee地址:https://gitee.com/ralphchen/ralph-cloud