使用Mapstruct将多个源字段映射到相同类型的目标字段

使用Mapstruct将多个源字段映射到相同类型的目标字段

本文介绍了使用Mapstruct将多个源字段映射到相同类型的目标字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下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的映射器.由于业务"逻辑",我需要将notificationPeriodschedulePeriod约束为Lecturestartend字段值.这是我使用另一个类得出的结论:

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:

@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,演讲)传递给映射器?

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?

推荐答案

您可以做的是创建一个@AfterMapping方法来手动填充这些部分:

What you can do is create an @AfterMapping method to populate those parts manually:

@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(..);
    }
}

或者,您可以将@AfterMapping方法放在@Mapper(uses = ..)中引用的另一个类中,或者可以使用Decorator(使用MapStruct提供的机制,或者使用依赖项注入框架的机制).

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).

这篇关于使用Mapstruct将多个源字段映射到相同类型的目标字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:11