【问题标题】:How to map multiple fields into one destination field using modelmapper如何使用 modelmapper 将多个字段映射到一个目标字段
【发布时间】:2017-11-15 00:49:26
【问题描述】:

如何将多个字段映射/合并为一个字段?比如将firstnamelastname 连接到目标fullname

public class ModelMapperConfigTest {

    @Test
    public void should_validate() {
        new ModelMapperConfig().modelMapper().validate();
    }

    @Data
    public static class Person {
        private String firstname;
        private String lastname;
    }

    @Data
    public static class PersonDto {
        private String firstname;
        private String lastname;
        private String fullname;
    }

    // Test data

    @Test
    public void should_map_multiple_fields_into_one() {

        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        // TODO: Configure modelmapper to merge the two fields

        // test that all fields are mapped
        modelMapper.validate();

        // test that the age is calculated
        Person person = new Person();
        person.setFirstname("Marilyn");
        person.setLastname("Monroe");
        PersonDto personDto = modelMapper.map(person, PersonDto.class);
        assertEquals(personDto.fullname, "Marilyn Monroe");
    }

    // This method should be used for mapping. In real, this could be a service call
    private String generateFullname(String firstname, String lastname) {
        return firstname + " " + lastname;
    }
}

【问题讨论】:

    标签: java modelmapper


    【解决方案1】:

    为此,您可以在PropertyMap 中使用Converter

    只需像这样配置您的映射器:

    @Test
    public void should_map_multiple_fields_into_one() {
    
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    
        modelMapper.createTypeMap(Person.class, PersonDto.class)
                .addMappings(
                        new PropertyMap<Person, PersonDto>() {
                            @Override
                            protected void configure() {
                                // define a converter that takes the whole "person"
                                using(ctx -> generateFullname(
                                        ((Person) ctx.getSource()).getFirstname(),
                                        ((Person) ctx.getSource()).getLastname())
                                )
                                // Map the compliete source here
                                .map(source, destination.getFullname());
                            }
                        });
    
        // test that all fields are mapped
        modelMapper.validate();
    
        // test that the age is calculated
        Person person = new Person();
        person.setFirstname("Marilyn");
        person.setLastname("Monroe");
        PersonDto personDto = modelMapper.map(person, PersonDto.class);
        assertEquals(personDto.fullname, "Marilyn Monroe");
    }
    
    // This method should be used for mapping. In real, this could be a service call
    private String generateFullname(String firstname, String lastname) {
        return firstname + " " + lastname;
    }
    

    【讨论】:

    • 这对我不起作用。除了我想连接两个字段的字段外,所有字段均为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    相关资源
    最近更新 更多