MapStruct

本文涉及的产品
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
简介: 对象转换神器——MapStruct

前言

版本说明

mapstruct = 1.3.1.Final

相关链接

实战演练

pom 依赖

<?xmlversion="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>top.simba1949</groupId><artifactId>map-struct-1.3.1.Final</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct --><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>1.3.1.Final</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency></dependencies><build><plugins><!--https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><annotationProcessorPaths><!--lombok 和 MapStruct 一起使用需要配置--><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></path><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>1.3.1.Final</version></path></annotationProcessorPaths></configuration></plugin></plugins></build></project>

属性相同属性名相同

推荐使用 spring-beans 包中 BeanUtils.copyProperties(source, target);

DTO

packageproperty.same.name.same;
importlombok.Data;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 10:26*/@DatapublicclassPropertySameAndNameSameDto {
privateIntegerid;
privateBooleanenable;
privateBigDecimalprice;
privateStringdesc;
privateDateproduce;
privateLocalDateTimebirthday;
}

VO

packageproperty.same.name.same;
importlombok.Data;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 10:25*/@DatapublicclassPropertySameAndNameSameVo {
privateIntegerid;
privateBooleanenable;
privateBigDecimalprice;
privateStringdesc;
privateDateproduce;
privateLocalDateTimebirthday;
}

自定义映射器

packageproperty.same.name.same;
importorg.mapstruct.Mapper;
importorg.mapstruct.factory.Mappers;
/*** @Mapper :自定义映射器* 属性相同,属性名相同* @author Anthony* @date 2020/9/11 10:26*/@MapperpublicinterfacePropertySameAndNameSameMapper {
PropertySameAndNameSameMapperINSTANCE=Mappers.getMapper(PropertySameAndNameSameMapper.class);
/*** 对象转换* 当属性与目标实体对应项具有相同的名称时,它将隐式映射。* @param propertySameAndNameSameDto* @return*/PropertySameAndNameSameVodto2Vo(PropertySameAndNameSameDtopropertySameAndNameSameDto);
}

单元测试

packageproperty.same.name.same;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 10:27*/publicclassPropertySameAndNameSameApplication {
publicstaticvoidmain(String[] args) {
PropertySameAndNameSameDtodto=newPropertySameAndNameSameDto();
dto.setId(1);
dto.setEnable(true);
dto.setDesc("desc");
dto.setPrice(BigDecimal.ONE);
dto.setProduce(newDate());
dto.setBirthday(LocalDateTime.now());
PropertySameAndNameSameVovo=PropertySameAndNameSameMapper.INSTANCE.dto2Vo(dto);
System.out.println(vo);
    }
}

属性相同属性名不同

DTO

packageproperty.same.name.diff;
importlombok.Data;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 13:54*/@DatapublicclassPropertySameAndNameDiffDto {
privateIntegeridDto;
privateBooleanenableDto;
privateBigDecimalpriceDto;
privateStringdescDto;
privateDateproduceDto;
privateLocalDateTimebirthdayDto;
}

VO

packageproperty.same.name.diff;
importlombok.Data;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 13:55*/@DatapublicclassPropertySameAndNameDiffVo {
privateIntegeridVo;
privateBooleanenableVo;
privateBigDecimalpriceVo;
privateStringdescVo;
privateDateproduceVo;
privateLocalDateTimebirthdayVo;
}

自定义映射器

packageproperty.same.name.diff;
importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.factory.Mappers;
/*** @Mapper :自定义映射器* 属性相同,属性名不相同* @author Anthony* @date 2020/9/11 13:55*/@MapperpublicinterfacePropertySameAndNameDiffMapper {
PropertySameAndNameDiffMapperINSTANCE=Mappers.getMapper(PropertySameAndNameDiffMapper.class);
/*** 对象转换* 当属性与目标实体对应项具有相同的名称时,它将隐式映射。* 当属性与目标实体对应项名称不相同时,需要使用 @Mapping 显示映射* @param propertySameAndNameDiffDto* @return*/@Mapping(source="idDto", target="idVo")
@Mapping(source="enableDto", target="enableVo")
@Mapping(source="priceDto", target="priceVo")
@Mapping(source="descDto", target="descVo")
@Mapping(source="produceDto", target="produceVo")
@Mapping(source="birthdayDto", target="birthdayVo")
PropertySameAndNameDiffVodto2Vo(PropertySameAndNameDiffDtopropertySameAndNameDiffDto);
}

单元测试

packageproperty.same.name.diff;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 13:54*/publicclassPropertySameAndNameDiffApplication {
publicstaticvoidmain(String[] args) {
PropertySameAndNameDiffDtodto=newPropertySameAndNameDiffDto();
dto.setIdDto(1);
dto.setEnableDto(true);
dto.setDescDto("desc");
dto.setPriceDto(BigDecimal.TEN);
dto.setProduceDto(newDate());
dto.setBirthdayDto(LocalDateTime.now());
PropertySameAndNameDiffVovo=PropertySameAndNameDiffMapper.INSTANCE.dto2Vo(dto);
System.out.println(vo);
    }
}

属性不同

DTO

packageproperty.diff;
importlombok.Data;
/*** @author Anthony* @date 2020/9/11 14:10*/@DatapublicclassPropertyDiffDto {
privateStringidDto;
privateStringenableDto;
privateStringpriceDto;
privateIntegerdescDto;
privateStringproduceDto;
privateStringbirthdayDto;
}

VO

packageproperty.diff;
importlombok.Data;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 14:10*/@DatapublicclassPropertyDiffVo {
privateIntegeridVo;
privateBooleanenableVo;
privateBigDecimalpriceVo;
privateStringdescVo;
privateDateproduceVo;
privateLocalDateTimebirthdayVo;
}

自定义映射器

packageproperty.diff;
importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.factory.Mappers;
/*** @Mapper :自定义映射器* 属性不相同* @author Anthony* @date 2020/9/11 14:10*/@MapperpublicinterfacePropertyDiffMapper {
PropertyDiffMapperINSTANCE=Mappers.getMapper(PropertyDiffMapper.class);
/*** 对象转换* 属性相同:当属性与目标实体对应项具有相同的名称时,它将隐式映射。* 属性相同:当属性与目标实体对应项名称不相同时,需要使用 @Mapping 显示映射** 属性不同:*  有特殊要求的,比如日期,@Mapping 添加属性 dateFormat,不满足要求的可以自定义解析*  (假设 enable 1=true,0=false):按照自定义解析方式解析*  无特殊要求的,按照以下规则解析**  Boolean.parseBoolean(..)*  Integer.parseInt(..)**  String.valueOf(..)**  (new SimpleDateFormat("yyyy-MM-dd")).parse(..) : dateFormat = "yyyy-MM-dd"*  LocalDateTime.parse(.., DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) :dateFormat = "yyyy-MM-dd HH:mm:ss"**  new BigDecimal(..)** @param propertyDiffDto* @return*/@Mapping(source="idDto", target="idVo")
// 将 source 定义成 表达式@Mapping(expression="java(enableDto2Vo(propertyDiffDto.getEnableDto()))", target="enableVo")
@Mapping(source="priceDto", target="priceVo")
@Mapping(source="descDto", target="descVo")
// 需要添加日期格式化@Mapping(source="produceDto", target="produceVo", dateFormat="yyyy-MM-dd")
@Mapping(source="birthdayDto", target="birthdayVo", dateFormat="yyyy-MM-dd HH:mm:ss")
PropertyDiffVodto2Vo(PropertyDiffDtopropertyDiffDto);
/*** 自定义 enableDto2Vo 解析* @param enableStr* @return*/defaultbooleanenableDto2Vo(StringenableStr){
if ("0".equals(enableStr)){
returnfalse;
        }elseif ("1".equals(enableStr)){
returntrue;
        }else {
thrownewRuntimeException("无法解析:"+enableStr);
        }
    }
}

单元测试

packageproperty.diff;
/*** @author Anthony* @date 2020/9/11 14:10*/publicclassPropertyDiffApplication {
publicstaticvoidmain(String[] args) {
PropertyDiffDtodto=newPropertyDiffDto();
dto.setIdDto("1");
// 除了 true 其他都是 false:假设1=true,0=falsedto.setEnableDto("0");
dto.setPriceDto("6.66");
dto.setDescDto(1);
dto.setProduceDto("2020-09-11");
dto.setBirthdayDto("2020-09-11 14:00:00");
PropertyDiffVovo=PropertyDiffMapper.INSTANCE.dto2Vo(dto);
System.out.println(vo);
    }
}

多个对象转为一个对象数据

DtoFirst

packagemulti.object;
importlombok.Data;
importjava.math.BigDecimal;
/*** @author Anthony* @date 2020/9/11 15:30*/@DatapublicclassMultiObject2OneDtoFirst {
privateStringidDto;
privateIntegerageDto;
privateBigDecimalpriceDto;
}

DtoSecond

packagemulti.object;
importlombok.Data;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 15:30*/@DatapublicclassMultiObject2OneDtoSecond {
privateDatebirthdayDto;
privateLocalDateTimelocalDateTimeDto;
privateMultiObject2OneDtoDepthdepthDto;
}

DtoSecond.depthDto

packagemulti.object;
importlombok.Data;
/*** @author Anthony* @date 2020/9/11 16:28*/@DatapublicclassMultiObject2OneDtoDepth {
privateBooleanenableDto;
}

VO

packagemulti.object;
importlombok.Data;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 15:32*/@DatapublicclassMultiObject2OneVo {
privateStringidVo;
privateIntegerageVo;
privateBigDecimalpriceVo;
privateDatebirthdayVo;
privateLocalDateTimelocalDateTimeVo;
privateBooleanenableVo;
}

自定义映射器

packagemulti.object;
importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.factory.Mappers;
/*** @author Anthony* @date 2020/9/11 15:30*/@MapperpublicinterfaceMultiObject2OneMapper {
MultiObject2OneMapperINSTANCE=Mappers.getMapper(MultiObject2OneMapper.class);
@Mapping(source="first.idDto", target="idVo")
@Mapping(source="first.ageDto", target="ageVo")
@Mapping(source="first.priceDto", target="priceVo")
@Mapping(source="second.birthdayDto", target="birthdayVo")
@Mapping(source="second.localDateTimeDto", target="localDateTimeVo")
@Mapping(source="second.depthDto.enableDto", target="enableVo")
MultiObject2OneVodto2Vo(MultiObject2OneDtoFirstfirst, MultiObject2OneDtoSecondsecond);
}

单元测试

packagemulti.object;
importjava.math.BigDecimal;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 15:29*/publicclassMultiObject2OneApplication {
publicstaticvoidmain(String[] args) {
MultiObject2OneDtoFirstfirst=newMultiObject2OneDtoFirst();
first.setIdDto("id");
first.setAgeDto(18);
first.setPriceDto(BigDecimal.TEN);
MultiObject2OneDtoDepthdepth=newMultiObject2OneDtoDepth();
depth.setEnableDto(true);
MultiObject2OneDtoSecondsecond=newMultiObject2OneDtoSecond();
second.setBirthdayDto(newDate());
second.setLocalDateTimeDto(LocalDateTime.now());
second.setDepthDto(depth);
MultiObject2OneVovo=MultiObject2OneMapper.INSTANCE.dto2Vo(first, second);
System.out.println(vo);
    }
}

自定义转换器

DTO

packagediy.transform;
importlombok.Data;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 16:43*/@DatapublicclassDiyTransformDto {
privateStringintDto;
privateStringbooleanDto;
privateStringdateDto;
privateStringlocalDateTimeDto;
privateStringuserIntDto;
privateStringuserStringDto;
privateStringenumDto;
privateDatedate2LocalDateTimeDto;
privateLocalDateTimelocalDateTime2DateDto;
}

Vo

DiyTransformUserVo

packagediy.transform;
importlombok.Data;
/*** @author Anthony* @date 2020/9/11 16:48*/@DatapublicclassDiyTransformUserVo {
privateIntegeruserIntVo;
privateStringuserStringVo;
}

DiyTransformVo

packagediy.transform;
importlombok.Data;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 16:44*/@DatapublicclassDiyTransformVo {
privateIntegerintVo;
privateBooleanbooleanVo;
privateDatedateVo;
privateLocalDateTimelocalDateTimeVo;
privateDiyTransformUserVouserVo;
privateDiyTransformDeleteEnumenumVo;
privateLocalDateTimedate2LocalDateTimeVo;
privateDatelocalDateTime2DateVo;
}

自定义转换器

packagediy.transform.handler;
importjava.time.LocalDateTime;
importjava.time.ZoneId;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 18:01*/publicclassDateTransformHandler {
publicstaticDatelocalDateTime2Date(LocalDateTimelocalDateTime){
if (null==localDateTime){
thrownewRuntimeException("");
        }
returnDate.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
publicstaticLocalDateTimedate2LocalDateTime(Datedate){
if (null==date){
thrownewRuntimeException("");
        }
returndate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}

枚举类

packagediy.transform;
/*** @author Anthony* @date 2020/9/11 16:52*/publicenumDiyTransformDeleteEnum {
DELETE_NO(0, "未删除"),
DELETE_YES(1, "已删除")
    ;
privateIntegercode;
privateStringdesc;
publicstaticDiyTransformDeleteEnumgetDeleteEnumByCode(intcode){
for (DiyTransformDeleteEnumanEnum : DiyTransformDeleteEnum.values()) {
if (code==anEnum.code){
returnanEnum;
            }
        }
thrownewRuntimeException("获取不到对应的枚举类型");
    }
DiyTransformDeleteEnum(Integercode, Stringdesc) {
this.code=code;
this.desc=desc;
    }
publicIntegergetCode() {
returncode;
    }
publicvoidsetCode(Integercode) {
this.code=code;
    }
publicStringgetDesc() {
returndesc;
    }
publicvoidsetDesc(Stringdesc) {
this.desc=desc;
    }
@OverridepublicStringtoString() {
return"DiyTransformDeleteEnum{"+"code="+code+", desc='"+desc+'\''+'}';
    }
}

自定义映射器

packagediy.transform;
importdiy.transform.handler.DateTransformHandler;
importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.factory.Mappers;
/*** 第一种方式:* @Mapper(uses = {DateTransformHandler.class}) :使用自定义的转换器* @author Anthony* @date 2020/9/11 16:45*/@Mapper(uses= {DateTransformHandler.class})
publicinterfaceDiyTransformMapper {
DiyTransformMapperINSTANCE=Mappers.getMapper(DiyTransformMapper.class);
@Mapping(source="intDto", target="intVo")
@Mapping(source="booleanDto", target="booleanVo")
@Mapping(source="dateDto", target="dateVo", dateFormat="yyyy-MM-dd HH:mm:ss")
@Mapping(source="localDateTimeDto", target="localDateTimeVo", dateFormat="yyyy-MM-dd HH:mm:ss")
// 第二种方式:使用自定义的转换器@Mapping(expression="java(string2Enum(diyTransformDto.getEnumDto()))",target="enumVo")
@Mapping(source="userIntDto", target="userVo.userIntVo")
@Mapping(source="userStringDto", target="userVo.userStringVo")
@Mapping(source="date2LocalDateTimeDto", target="date2LocalDateTimeVo")
@Mapping(source="localDateTime2DateDto", target="localDateTime2DateVo")
DiyTransformVodto2Vo(DiyTransformDtodiyTransformDto);
/*** 转义枚举* @param code* @return*/defaultDiyTransformDeleteEnumstring2Enum(Stringcode){
if (null==code||code.length() ==0){
thrownewRuntimeException("解析枚举失败");
        }
IntegercodeInt=Integer.valueOf(code);
returnDiyTransformDeleteEnum.getDeleteEnumByCode(codeInt);
    }
}

单元测试

packagediy.transform;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 16:45*/publicclassDiyTransformApplication {
publicstaticvoidmain(String[] args) {
DiyTransformDtodto=newDiyTransformDto();
dto.setIntDto("9");
dto.setBooleanDto("true");
dto.setDateDto("2020-01-01 14:00:00");
dto.setLocalDateTimeDto("2020-02-02 14:00:00");
dto.setUserIntDto("7");
dto.setUserStringDto("username");
dto.setEnumDto("1");
dto.setLocalDateTime2DateDto(LocalDateTime.now());
dto.setDate2LocalDateTimeDto(newDate());
DiyTransformVovo=DiyTransformMapper.INSTANCE.dto2Vo(dto);
System.out.println(vo);
    }
}

反向映射-属性相同

DTO

packagemapping.reverse.property.same;
importlombok.Data;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 18:35*/@DatapublicclassMappingReversePropertySameDto {
privateIntegeridSame;
privateStringnameDto;
privateDatedateDto;
}

VO

packagemapping.reverse.property.same;
importlombok.Data;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 18:35*/@DatapublicclassMappingReversePropertySameVo {
privateIntegeridSame;
privateStringnameVo;
privateDatedateVo;
}

自定义映射器

packagemapping.reverse.property.same;
importorg.mapstruct.InheritInverseConfiguration;
importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.factory.Mappers;
/*** @author Anthony* @date 2020/9/11 18:35*/@MapperpublicinterfaceMappingReversePropertySameMapper {
MappingReversePropertySameMapperINSTANCE=Mappers.getMapper(MappingReversePropertySameMapper.class);
@Mapping(source="nameDto", target="nameVo")
@Mapping(source="dateDto", target="dateVo")
MappingReversePropertySameVodto2Vo(MappingReversePropertySameDtomappingReversePropertySameDto);
/*** 反向映射:添加注解 @InheritInverseConfiguration(name = "dto2Vo")* 如果多个方法符合条件,需要在 name 指定对应的方法名* @param mappingReversePropertySameVo* @return*/@InheritInverseConfiguration(name="dto2Vo")
MappingReversePropertySameDtovo2Dto(MappingReversePropertySameVomappingReversePropertySameVo);
}

单元测试

packagemapping.reverse.property.same;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 18:36*/publicclassMappingReversePropertySameApplication {
publicstaticvoidmain(String[] args) {
MappingReversePropertySameDtodto=newMappingReversePropertySameDto();
dto.setDateDto(newDate());
dto.setIdSame(1);
dto.setNameDto("name");
MappingReversePropertySameVovo=MappingReversePropertySameMapper.INSTANCE.dto2Vo(dto);
System.out.println(vo);
MappingReversePropertySameDtovo2Dto=MappingReversePropertySameMapper.INSTANCE.vo2Dto(vo);
System.out.println(vo2Dto);
    }
}

反向映射-属性不同

枚举

packagemapping.reverse.property.diff;
/*** @author Anthony* @date 2020/9/11 19:02*/publicenumMappingReversePropertyDiffEnum {
YES(1, "YES"),
NO(0, "NO"),
    ;
publicstaticMappingReversePropertyDiffEnumgetEnumByCode(Integercode){
for (MappingReversePropertyDiffEnumanEnum : MappingReversePropertyDiffEnum.values()) {
if (code==anEnum.code){
returnanEnum;
            }
        }
thrownewRuntimeException("");
    }
privateIntegercode;
privateStringdesc;
MappingReversePropertyDiffEnum(Integercode, Stringdesc) {
this.code=code;
this.desc=desc;
    }
publicIntegergetCode() {
returncode;
    }
publicvoidsetCode(Integercode) {
this.code=code;
    }
publicStringgetDesc() {
returndesc;
    }
publicvoidsetDesc(Stringdesc) {
this.desc=desc;
    }
}

DTO

packagemapping.reverse.property.diff;
importlombok.Data;
/*** @author Anthony* @date 2020/9/11 18:50*/@DatapublicclassMappingReversePropertyDiffDto {
privateStringidDto;
privateStringtoDateDto;
privateStringtoLocalDateTimeDto;
privateStringtoEnum;
}

VO

packagemapping.reverse.property.diff;
importlombok.Data;
importjava.time.LocalDateTime;
importjava.util.Date;
/*** @author Anthony* @date 2020/9/11 18:50*/@DatapublicclassMappingReversePropertyDiffVo {
privateIntegeridVo;
privateDatetoDateVo;
privateLocalDateTimetoLocalDateTimeVo;
privateMappingReversePropertyDiffEnumanEnum;
}

自定义映射器

packagemapping.reverse.property.diff;
importorg.mapstruct.InheritInverseConfiguration;
importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.factory.Mappers;
/*** @author Anthony* @date 2020/9/11 18:50*/@MapperpublicinterfaceMappingReversePropertyDiffMapper {
MappingReversePropertyDiffMapperINSTANCE=Mappers.getMapper(MappingReversePropertyDiffMapper.class);
@Mapping(source="idDto", target="idVo")
@Mapping(source="toDateDto", target="toDateVo", dateFormat="yyyy-MM-dd HH:mm:ss")
@Mapping(source="toLocalDateTimeDto", target="toLocalDateTimeVo", dateFormat="yyyy-MM-dd HH:mm:ss")
@Mapping(expression="java(toEnum(mappingReversePropertyDiffDto.getToEnum()))", target="anEnum")
MappingReversePropertyDiffVodto2Vo(MappingReversePropertyDiffDtomappingReversePropertyDiffDto);
/*** 反向映射:添加注解 @InheritInverseConfiguration(name = "dto2Vo")* 如果多个方法符合条件,需要在 name 指定对应的方法名* 如果不满足反向映射需要自定义方法反向映射* @param mappingReversePropertySameVo* @return*/@InheritInverseConfiguration(name="dto2Vo")
@Mapping(expression="java(enum2String(mappingReversePropertySameVo.getAnEnum()))", target="toEnum")
MappingReversePropertyDiffDtovo2Dto(MappingReversePropertyDiffVomappingReversePropertySameVo);
defaultMappingReversePropertyDiffEnumtoEnum(Stringcode){
if (null==code||code.length() <=0){
thrownewRuntimeException("");
        }
returnMappingReversePropertyDiffEnum.getEnumByCode(Integer.valueOf(code));
    }
/*** 自定义方法反向映射* @param anEnum* @return*/defaultStringenum2String(MappingReversePropertyDiffEnumanEnum){
returnString.valueOf(anEnum.getCode());
    }
}

单元测试

packagemapping.reverse.property.diff;
/*** @author Anthony* @date 2020/9/11 18:50*/publicclassMappingReversePropertyDiffApplication {
publicstaticvoidmain(String[] args) {
MappingReversePropertyDiffDtodto=newMappingReversePropertyDiffDto();
dto.setIdDto("1");
dto.setToDateDto("2020-01-01 14:00:00");
dto.setToLocalDateTimeDto("2020-12-31 14:00:00");
dto.setToEnum("0");
MappingReversePropertyDiffVovo=MappingReversePropertyDiffMapper.INSTANCE.dto2Vo(dto);
System.out.println(vo);
MappingReversePropertyDiffDtovo2Dto=MappingReversePropertyDiffMapper.INSTANCE.vo2Dto(vo);
System.out.println(vo2Dto);
    }
}





目录
相关文章
|
安全 IDE Java
MapStruct - 原理讲解
MapStruct - 原理讲解
1269 2
MapStruct - 原理讲解
|
Java Maven
MapStruct - Lombok & Maven 版本关系
MapStruct - Lombok & Maven 版本关系
1812 0
|
3月前
|
API
Swagger2 常用注解介绍
Swagger2 常用注解介绍
|
6月前
|
安全 IDE Java
使用MapStruct和Lombok简化代码
使用MapStruct和Lombok简化代码
188 2
|
7月前
|
Java 数据库连接 数据库
Spring整合Mybatis、Spring整合JUnit
Spring整合Mybatis、Spring整合JUnit
45 1
Spring整合Mybatis、Spring整合JUnit
|
6月前
|
XML 搜索推荐 Java
JAXB的常用注解讲解
JAXB的常用注解讲解
|
7月前
|
Java 数据库连接 mybatis
Spring整合MyBatis、整合JUnit
Spring整合MyBatis、整合JUnit
65 0
|
Java Spring 容器
mapstruct
mapstruct
65 0
|
安全 Java Maven
MapStruct使用
MapStruct使用