[英]Map multiple source fields to same type target fields with Mapstruct
Consider the following POJOs: 考虑以下POJO:
public class SchedulePayload {
public String name;
public String scheduler;
public PeriodPayload notificationPeriod;
public PeriodPayload schedulePeriod;
}
private class Lecture {
public ZonedDateTime start;
public ZonedDateTime end;
}
public class XmlSchedule {
public String scheduleName;
public String schedulerName;
public DateTime notificationFrom;
public DateTime notificationTo;
public DateTime scheduleFrom;
public DateTime scheduleTo;
}
public class PeriodPayload {
public DateTime start;
public DateTime finish;
}
Using MapStruct, I created a mapper that maps XmlSchedule
to a SchedulePayload
. 使用MapStruct,我创建了一个映射器,将
XmlSchedule
映射到SchedulePayload
。 Due to "business" "logic", I need to constrain notificationPeriod
and schedulePeriod
to a Lecture
's start
and end
field values. 由于“业务”“逻辑”,我需要将
notificationPeriod
和schedulePeriod
约束到Lecture
的start
和end
字段值。 Here is what I've come up to, using another class: 以下是我使用另一个课程:
@Mapper(imports = { NotificationPeriodHelper.class })
public interface ISchedulePayloadMapper
{
@Mappings({
@Mapping(target = "name", source = "scheduleName"),
@Mapping(target = "scheduler", source = "schedulerName"),
@Mapping(target = "notificationPeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, notificationFrom, notificationTo))"),
@Mapping(target = "schedulePeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, scheduleFrom, scheduleTo))")
})
SchedulePayload map(XmlSchedule xmlSchedule, Lecture lecture);
}
Is there any way this can be achieved in another way (ie another mapper, decorators, etc.)? 有没有办法可以用另一种方式实现(即另一个映射器,装饰器等)? How can I pass multiple values (xmlSchedule, lecture) to a mapper?
如何将多个值(xmlSchedule,讲座)传递给映射器?
What you can do is create an @AfterMapping
method to populate those parts manually: 你可以做的是创建一个
@AfterMapping
方法来手动填充这些部分:
@Mapper
public abstract class SchedulePayloadMapper
{
@Mappings({
@Mapping(target = "name", source = "scheduleName"),
@Mapping(target = "scheduler", source = "schedulerName"),
@Mapping(target = "notificationPeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, notificationFrom, notificationTo))"),
@Mapping(target = "schedulePeriod", expression = "java(NotificationPeriodHelper.getConstrainedPeriod(xmlSchedule, scheduleFrom, scheduleTo))")
})
public abstract SchedulePayload map(XmlSchedule xmlSchedule, Lecture lecture);
@AfterMapping
protected void addPeriods(@MappingTarget SchedulePayload result, XmlSchedule xmlSchedule, Lecture lecture) {
result.setNotificationPeriod(..);
result.setSchedulePeriod(..);
}
}
Alternatively, you can place the @AfterMapping
method in another class that is referenced in @Mapper(uses = ..)
or you can use a Decorator (using the mechanisms MapStruct provides, or of your dependency injection framework if you use one). 或者,您可以将
@AfterMapping
方法放在@Mapper(uses = ..)
引用的另一个类中,或者您可以使用Decorator(使用MapStruct提供的机制,或者使用依赖注入框架,如果您使用它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.