考虑以下 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;
}
使用 MapStruct,我创建了一个映射XmlSchedule
到SchedulePayload
. 由于“业务”“逻辑” ,我需要限制anotificationPeriod
和字段值。这是我想出的,使用另一个类:schedulePeriod
Lecture
start
end
@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);
}
有什么方法可以通过另一种方式实现(即另一个映射器、装饰器等)?如何将多个值(xmlSchedule、lecture)传递给映射器?