java类复制mapstruct(convertor转换器)

07-03 1187阅读

背景:

类拷贝或属性拷贝,采用BeanUtil会用反射,性能不佳

java类复制mapstruct(convertor转换器)
(图片来源网络,侵删)
BeanUtil.copyProperties(eventsInputDto, satisfyRiskDTO)

常见转换场景:

1、通过字段的值,条件判断赋值

三目运算判断赋值:

expression = "java(saveInfo.getXxxFlag() == null ? 0 : saveInfo.getXxxFlag())", target = "xxxFlagFlag"

@Mapping(expression = "java(saveInfo.getXxxFlag() == null ? 0 : saveInfo.getXxxFlag())", target = "xxxFlagFlag")
RespDTO toCareActivityApprovalDTO(saveInfo saveInfo);

 其他场景使用场景:

	//不同字段映射
    @Mapping(target = "memberNo", source = "memberVo.memberNoXxx")
	//来自其他参数赋值(多个参数实体,赋值到同一对象)
    @Mapping(target = "authMode", source = "wayEnum.code")
	//调用外部方法赋值
    @Mapping(target = "authUid", expression = "java(com.petrochina.gsms.membersync.batch.common.utils.MemberConverUtils.findAuid(memberVo,wayEnum))")
    //java自带的系统类
	@Mapping(target = "accountStartTime", expression = "java(java.time.LocalDateTime.now())")
    //日期转换
	@Mapping(target = "accountEndTime", expression = "java(cn.hutool.core.date.LocalDateTimeUtil.parse(\"9999-12-12 00:00:00\", cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMATTER))")
    PersAccountEntity toEntity(MemberVo memberVo, IndividualAcctCertificationWayEnum wayEnum);
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]