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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:41:49  来源:igfitidea点击:

Map multiple source fields to same type target fields with Mapstruct

javamappingmapstruct

提问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 XmlScheduleto a SchedulePayload. Due to "business" "logic", I need to constrain notificationPeriodand schedulePeriodto a Lecture's startand endfield values. Here is what I've come up to, using another class:

使用 MapStruct,我创建了一个映射XmlScheduleSchedulePayload. 由于“业务”,“逻辑”,我需要限制notificationPeriodschedulePeriodLecturestartend字段值。这是我想到的,使用另一个类:

@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 @AfterMappingmethod 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 @AfterMappingmethod 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 提供的机制,或者您的依赖注入框架(如果使用))。