java 使用 Mapstruct 将多个源字段映射到相同类型的目标字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37705417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map multiple source fields to same type target fields with Mapstruct
提问by giannoug
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
. Due to "business" "logic", I need to constrain notificationPeriod
and schedulePeriod
to a Lecture
's start
and end
field values. Here is what I've come up to, using another class:
使用 MapStruct,我创建了一个映射XmlSchedule
到SchedulePayload
. 由于“业务”,“逻辑”,我需要限制notificationPeriod
和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);
}
Is there any way this can be achieved in another way (i.e. another mapper, decorators, etc.)? How can I pass multiple values (xmlSchedule, lecture) to a mapper?
有没有办法以另一种方式实现这一点(即另一个映射器、装饰器等)?如何将多个值(xmlSchedule、讲座)传递给映射器?
回答by agudian
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 = ..)
或者您可以使用装饰器(使用 MapStruct 提供的机制,或者您的依赖注入框架(如果使用))。