Java 使用org.modelmapper.modelmapper进行对象映射

Java 使用org.modelmapper.modelmapper进行对象映射,java,modelmapper,Java,Modelmapper,我有两个目标: @Setter @Getter public class Agent { public int userID; public String name; public boolean isVoiceRecorded; public boolean isScreenRecorded; public boolean isOnCall; public LocalDateTime startEventDateTime; } public cl

我有两个目标:

@Setter
@Getter
public class Agent {
    public int userID;
    public String name;
    public boolean isVoiceRecorded;
    public boolean isScreenRecorded;
    public boolean isOnCall;
    public LocalDateTime startEventDateTime;
}
public class AgentLine {
    public int userID;
    public String name;
    public boolean isVoiceRecorded;
    public boolean isScreenRecorded;
    public boolean isOnCall;
    public String startEventDateTime;
}
我想在代理行和代理之间映射。由于Localdatetime转换,我无法使用默认映射。 我定义了:

    @Bean
    ModelMapper getModelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        Converter<AgentLine, Agent> orderConverter = new Converter<AgentLine, Agent>() {
            @Override
            public Agent convert(MappingContext<AgentLine, Agent> mappingContext) {
                AgentLine s = mappingContext.getSource();
                Agent d = mappingContext.getDestination();
/*                d.userID = s.userID;
                d.name = s.name;*/
                d.startEventDateTime = LocalDateTime.parse(s.startEventDateTime, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
                return d;
            }
        };
        modelMapper.addConverter(orderConverter);
        return modelMapper;
    }
它可以工作,但我不想在convert方法中指定所有代理属性,我想指定startEventDateTime转换,默认情况下将映射其余属性

此外,我还试图定义:

PropertyMap<AgentLine, Agent> orderMap = new PropertyMap<AgentLine, Agent>() {
                @Override
                protected void configure() {
                    map().setName(source.name);
                }
            };
modelMapper.addMappings(orderMap);
PropertyMap orderMap=new PropertyMap(){
@凌驾
受保护的void configure(){
map().setName(source.name);
}
};
addMappings(orderMap);
但是,在映射中,您无法处理日期转换。 如果我为映射器PropertyMap和转换器定义属性映射,则会忽略属性映射


我不想在convert方法中指定所有代理属性,我想指定startEventDateTime转换,默认情况下将映射其余属性。

不要使用
转换器来映射复杂对象。为此,您应该使用
TypeMap
。使用
Converter
进行自定义转换(对于您的案例
String
LocalDateTime

ModelMapper ModelMapper=newmodelmapper();
Converter dateTimeConverter=ctx->ctx.getSource()==null?null:LocalDateTime.parse(ctx.getSource(),DateTimeFormatter.ISO_LOCAL_DATE_TIME);
modelMapper.typeMap(AgentLine.class,Agent.class)
.addMappings(mapper->mapper.using(dateTimeConverter).map(AgentLine::getStartEventDateTime,Agent::setStartEventDateTime));

顺便说一句,字段应该有
专用的
修改器,而不是
公用的
。还可以为
AgentLine
类使用
@Getter
@Setter
注释。。这会有帮助的
PropertyMap<AgentLine, Agent> orderMap = new PropertyMap<AgentLine, Agent>() {
                @Override
                protected void configure() {
                    map().setName(source.name);
                }
            };
modelMapper.addMappings(orderMap);
ModelMapper modelMapper = new ModelMapper();    
Converter<String, LocalDateTime> dateTimeConverter = ctx -> ctx.getSource() == null ? null : LocalDateTime.parse(ctx.getSource(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
modelMapper.typeMap(AgentLine.class, Agent.class)
        .addMappings(mapper -> mapper.using(dateTimeConverter).map(AgentLine::getStartEventDateTime, Agent::setStartEventDateTime));