什么mapSturct
mapSturct
是一个生成类型安全, 高性能且无依赖的 JavaBean 映射代码的注解处理器(annotation processor)。通过动态生成代码实现,使代码易于调试和美观,
引入POM
<properties> <org.mapstruct.version>1.2.0.CR1org.mapstruct.version> properties> <dependencies> <dependency> <groupId>org.mapstructgroupId> <artifactId>mapstruct-jdk8artifactId> <version>${org.mapstruct.version}version> dependency> <dependency> <groupId>org.mapstructgroupId> <artifactId>mapstruct-processorartifactId> <version>${org.mapstruct.version}version> <scope>providedscope> dependency> dependencies>
与lombok冲突解决
clean install就可以看到对应的实现类
<build> <plugins> <plugin> <groupId>org.apache.maven.pluginsgroupId> <artifactId>maven-compiler-pluginartifactId> <version>3.7.0version> <configuration> <source>${java.version}source> <target>${java.version}target> <annotationProcessorPaths> <path> <groupId>org.mapstructgroupId> <artifactId>mapstruct-processorartifactId> <version>${org.mapstruct.version}version> path> <path> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>${lombok.version}version> path> annotationProcessorPaths> configuration> plugin> plugins> build>
属性相同不自动生成映射
生成了Impl但是属性相同的没有自动生成set方法,手动添加get set方法不使用lombok就没有问题
个人解决方式:
1.尝试升级了lombok版本 我是从从1.14.8 提高到1.18.2就行了
2.保证没有依赖lombok冲突 比如当前项目引入了lombok 其他项目又引入了 版本不一致
与Spring整合
//此注解在自动生成实现类会打上@Comment注解 我们只需要让Spring扫描就行了 @Mapper(componentModel = "spring") public interface MemberGradeConvert { public MemberGradeEntity editReqDtOToPo(EditReqDtO editReqDtO); }
简单使用
以上面为例自动在targer目录生成实现类
@Component public class MemberGradeConvertImpl implements MemberGradeConvert { public MemberGradeConvertImpl() { } public MemberGradeEntity editReqDtOToPo(EditReqDtO editReqDtO) { if (editReqDtO == null) { return null; } else { MemberGradeEntity memberGradeEntity = new MemberGradeEntity(); memberGradeEntity.setId(editReqDtO.getId()); memberGradeEntity.setName(editReqDtO.getName()); memberGradeEntity.setPoints(editReqDtO.getPoints()); memberGradeEntity.setDiscountRate(editReqDtO.getDiscountRate()); memberGradeEntity.setStatus(editReqDtO.getStatus()); memberGradeEntity.setMerchantId(editReqDtO.getMerchantId()); return memberGradeEntity; } } }
多个组成一个VO
@Mapper(componentModel = "spring") public interface CouponConvert { @Mapping(source = "couponCode.id", target = "couponCodeId") @Mapping(source = "couponCode.used", target = "used") @Mapping(source = "couponCode.usedDate", target = "usedDate") @Mapping(source = "couponCode.receiveDate", target = "receiveDate") @Mapping(source = "couponRo.enable", target = "enable") @Mapping( target = "expireDate",expression = "java(CouponConvert.getMaxDate(couponCode.getUsageEndDate(), couponRo==null?null:couponRo.getUsageEndDate()))") public FindUserCouponsResDto couponCodeAndCouponRoToFindUserCouponsResDto(CouponCode couponCode, CouponRo couponRo); static Timestamp getMaxDate(Timestamp date1, Timestamp date2){ if(date1==null){ return date2; } if(date2==null){ return date1; } return date1.getTime()>date2.getTime()?date1:date2; } }
自动生成实现
@Component public class CouponConvertImpl implements CouponConvert { @Override public FindUserCouponsResDto couponCodeAndCouponRoToFindUserCouponsResDto(CouponCode couponCode, CouponRo couponRo) { if ( couponCode == null && couponRo == null ) { return null; } FindUserCouponsResDto findUserCouponsResDto = new FindUserCouponsResDto(); if ( couponCode != null ) { findUserCouponsResDto.setCouponCodeId( couponCode.getId() ); findUserCouponsResDto.setReceiveDate( couponCode.getReceiveDate() ); findUserCouponsResDto.setUsedDate( couponCode.getUsedDate() ); findUserCouponsResDto.setUsed( couponCode.isUsed() ); } if ( couponRo != null ) { if ( couponRo.getEnable() != null ) { findUserCouponsResDto.setEnable( couponRo.getEnable() ); } } findUserCouponsResDto.setExpireDate( CouponConvert.getMaxDate(couponCode.getUsageEndDate(), couponRo==null?null:couponRo.getUsageEndDate()) ); return findUserCouponsResDto; } }
简单的条件处理
@Mapping(source = "couponCode.id", target = "couponCodeId") @Mapping(source = "couponCode.used", target = "used") @Mapping(source = "couponCode.usedDate", target = "usedDate") @Mapping(source = "couponCode.receiveDate", target = "receiveDate") @Mapping(source = "couponRo.enable", target = "enable") @Mapping(target = "amount", expression = "java(couponCode.getRanAmount()!=null?couponCode.getRanAmount():couponRo.getAmount())") @Mapping(target = "expireDate", expression = "java(couponRo.getUsageCountDownDays()!=null?couponCode.getUsageEndDate():couponRo.getUsageEndDate())") public FindUserCouponCodeByIdResDto couponCodeRoAndCouponRoToFindUserCouponCodeByIdResDto(CouponCodeRo couponCode, CouponRo couponRo);
转换后
@Override public FindUserCouponCodeByIdResDto couponCodeRoAndCouponRoToFindUserCouponCodeByIdResDto(CouponCodeRo couponCode, CouponRo couponRo) { if ( couponCode == null && couponRo == null ) { return null; } FindUserCouponCodeByIdResDto findUserCouponCodeByIdResDto = new FindUserCouponCodeByIdResDto(); if ( couponCode != null ) { findUserCouponCodeByIdResDto.setCouponCodeId( couponCode.getId() ); findUserCouponCodeByIdResDto.setReceiveDate( couponCode.getReceiveDate() ); findUserCouponCodeByIdResDto.setUsedDate( couponCode.getUsedDate() ); findUserCouponCodeByIdResDto.setUsed( couponCode.isUsed() ); } if ( couponRo != null ) { findUserCouponCodeByIdResDto.setEnable( couponRo.getEnable() ); } findUserCouponCodeByIdResDto.setAmount( couponCode.getRanAmount()!=null?couponCode.getRanAmount():couponRo.getAmount() ); findUserCouponCodeByIdResDto.setExpireDate( couponRo.getUsageCountDownDays()!=null?couponCode.getUsageEndDate():couponRo.getUsageEndDate() ); return findUserCouponCodeByIdResDto; } }
复杂的表达式
@Mapper(componentModel = "spring") public interface CouponConvert { @Mapping(source = "couponCode.id", target = "couponCodeId") @Mapping(source = "couponCode.used", target = "used") @Mapping(source = "couponCode.usedDate", target = "usedDate") @Mapping(source = "couponCode.receiveDate", target = "receiveDate") @Mapping(source = "couponRo.enable", target = "enable") @Mapping( target = "expireDate",expression = "java(CouponConvert.getMaxDate(couponCode.getUsageEndDate(), couponRo==null?null:couponRo.getUsageEndDate()))") public FindUserCouponsResDto couponCodeAndCouponRoToFindUserCouponsResDto(CouponCode couponCode, CouponRo couponRo); static Timestamp getMaxDate(Timestamp date1, Timestamp date2){ if(date1==null){ return date2; } if(date2==null){ return date1; } return date1.getTime()>date2.getTime()?date1:date2; } }
转换后
@Component public class CouponConvertImpl implements CouponConvert { @Override public FindUserCouponsResDto couponCodeAndCouponRoToFindUserCouponsResDto(CouponCode couponCode, CouponRo couponRo) { if ( couponCode == null && couponRo == null ) { return null; } FindUserCouponsResDto findUserCouponsResDto = new FindUserCouponsResDto(); if ( couponCode != null ) { findUserCouponsResDto.setCouponCodeId( couponCode.getId() ); findUserCouponsResDto.setReceiveDate( couponCode.getReceiveDate() ); findUserCouponsResDto.setUsedDate( couponCode.getUsedDate() ); findUserCouponsResDto.setUsed( couponCode.isUsed() ); } if ( couponRo != null ) { if ( couponRo.getEnable() != null ) { findUserCouponsResDto.setEnable( couponRo.getEnable() ); } } findUserCouponsResDto.setExpireDate( CouponConvert.getMaxDate(couponCode.getUsageEndDate(), couponRo==null?null:couponRo.getUsageEndDate()) ); return findUserCouponsResDto; } }
一对多
@Mapping(target = "batchSoaGiveOutCouponItems",expression ="java(validateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(validateSendCouponReqVo.getValidateSendCouponItemReqVos()))") public BatchSoaGiveOutCouponRequestVo validateSendCouponReqVoToBatchSoaGiveOutCouponRequestVo(ValidateSendCouponReqVo validateSendCouponReqVo); public ListvalidateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(List validateSendCouponItemReqVos); @Mapping(target = "couponReceiveChannel",ignore = true)//忽略set public BatchSoaGiveOutCouponRequestVo.BatchSoaGiveOutCouponItem validateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(ValidateSendCouponReqVo.ValidateSendCouponItemReqVo validateSendCouponItemReqVos);
转换
@Component public class CouponConvertImpl implements CouponConvert { @Override public BatchSoaGiveOutCouponRequestVo validateSendCouponReqVoToBatchSoaGiveOutCouponRequestVo(ValidateSendCouponReqVo validateSendCouponReqVo) { if ( validateSendCouponReqVo == null ) { return null; } BatchSoaGiveOutCouponRequestVo batchSoaGiveOutCouponRequestVo = new BatchSoaGiveOutCouponRequestVo(); batchSoaGiveOutCouponRequestVo.setBatchSoaGiveOutCouponItems( validateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(validateSendCouponReqVo.getValidateSendCouponItemReqVos()) ); return batchSoaGiveOutCouponRequestVo; } @Override public ListvalidateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(List validateSendCouponItemReqVos) { if ( validateSendCouponItemReqVos == null ) { return null; } List list = new ArrayList ( validateSendCouponItemReqVos.size() ); for ( ValidateSendCouponItemReqVo validateSendCouponItemReqVo : validateSendCouponItemReqVos ) { list.add( validateSendCouponItemReqVoToBatchSoaGiveOutCouponItem( validateSendCouponItemReqVo ) ); } return list; } @Override public BatchSoaGiveOutCouponItem validateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(ValidateSendCouponItemReqVo validateSendCouponItemReqVos) { if ( validateSendCouponItemReqVos == null ) { return null; } BatchSoaGiveOutCouponItem batchSoaGiveOutCouponItem = new BatchSoaGiveOutCouponItem(); batchSoaGiveOutCouponItem.setUserId( validateSendCouponItemReqVos.getUserId() ); batchSoaGiveOutCouponItem.setUserMobile( validateSendCouponItemReqVos.getUserMobile() ); batchSoaGiveOutCouponItem.setCouponId( validateSendCouponItemReqVos.getCouponId() ); batchSoaGiveOutCouponItem.setMemberId( validateSendCouponItemReqVos.getMemberId() ); batchSoaGiveOutCouponItem.setQuantity( validateSendCouponItemReqVos.getQuantity() ); return batchSoaGiveOutCouponItem; } }
忽略属性
@Mapping(target = "couponReceiveChannel",ignore = true) public BatchSoaGiveOutCouponRequestVo.BatchSoaGiveOutCouponItem validateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(ValidateSendCouponReqVo.ValidateSendCouponItemReqVo validateSendCouponItemReqVos);
转换后
@Override public BatchSoaGiveOutCouponItem validateSendCouponItemReqVoToBatchSoaGiveOutCouponItem(ValidateSendCouponItemReqVo validateSendCouponItemReqVos) { if ( validateSendCouponItemReqVos == null ) { return null; } BatchSoaGiveOutCouponItem batchSoaGiveOutCouponItem = new BatchSoaGiveOutCouponItem(); batchSoaGiveOutCouponItem.setUserId( validateSendCouponItemReqVos.getUserId() ); batchSoaGiveOutCouponItem.setUserMobile( validateSendCouponItemReqVos.getUserMobile() ); batchSoaGiveOutCouponItem.setCouponId( validateSendCouponItemReqVos.getCouponId() ); batchSoaGiveOutCouponItem.setMemberId( validateSendCouponItemReqVos.getMemberId() ); batchSoaGiveOutCouponItem.setQuantity( validateSendCouponItemReqVos.getQuantity() ); return batchSoaGiveOutCouponItem; }
一个对象set到另外一个对象
public MaotaiBookingPo setMaotaiBookingPo(@MappingTarget MaotaiBookingPo maotaiBookingPo, AppointmentReqDto appointmentReqDto);
自动生成后: @Override public MaotaiBookingPo setMaotaiBookingPo(MaotaiBookingPo maotaiBookingPo, AppointmentReqDto appointmentReqDto) { if ( appointmentReqDto == null ) { return null; } maotaiBookingPo.setId( appointmentReqDto.getId() ); maotaiBookingPo.setUserId( appointmentReqDto.getUserId() ); maotaiBookingPo.setMobile( appointmentReqDto.getMobile() ); maotaiBookingPo.setName( appointmentReqDto.getName() ); maotaiBookingPo.setIdCard( appointmentReqDto.getIdCard() ); maotaiBookingPo.setReceiveAddr( appointmentReqDto.getReceiveAddr() ); maotaiBookingPo.setLon( appointmentReqDto.getLon() ); maotaiBookingPo.setLat( appointmentReqDto.getLat() ); maotaiBookingPo.setProvinceId( appointmentReqDto.getProvinceId() ); maotaiBookingPo.setProvinceName( appointmentReqDto.getProvinceName() ); maotaiBookingPo.setCityId( appointmentReqDto.getCityId() ); maotaiBookingPo.setCityName( appointmentReqDto.getCityName() ); maotaiBookingPo.setDistrictId( appointmentReqDto.getDistrictId() ); maotaiBookingPo.setDistrictName( appointmentReqDto.getDistrictName() ); maotaiBookingPo.setActivityId( appointmentReqDto.getActivityId() ); return maotaiBookingPo; }