简体   繁体   English

推土机-在从一个列表映射到另一个列表时,是否在列表中转换了对象?

[英]Dozer - Having objects converted in a list while mapping from list to list?

We are using Dozer to map entities to dto objects. 我们正在使用推土机将实体映射到dto对象。

We struggle with the following: Suppose we have an A entity with one-to-many relationship to B entities. 我们在以下方面作斗争:假设我们有一个A实体与B实体具有一对多关系。 When mapping we would like to convert the field produktId (eg 1234) in B entity to a modified value in B Dto (eg 00001234). 映射时,我们希望将B实体中的字段produktId(例如1234)转换为B Dto中的已修改值(例如00001234)。

Is it possible to have objects converted in a list while mapping from list to list? 从列表到列表的映射时,是否可以在列表中转换对象?

class AEntity {

 List<BEntity> bEntities;
}

class BEntity {
 Long produktId;
}

class ADto {
 List<BDto> bDtos;
}

class BDto {
 String produktId;
}

As André suggests, a custom converter seems appropriate here. 正如André所建议的那样,自定义转换器在这里似乎很合适。 With the API mapping something like this should work with Dozer 5.5.1: 使用API映射时 ,Dozer 5.5.1应该可以使用以下方法:

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.dozer.loader.api.BeanMappingBuilder;
import org.dozer.loader.api.FieldsMappingOptions;

public class MappingExample {

    private Mapper mapper;

    public ADto map(AEntity aEntity) {
        return getMapper().map(aEntity, ADto.class);
    }

    private Mapper getMapper() {
        if (mapper == null) {

            BeanMappingBuilder mappingBuilder = new BeanMappingBuilder() {
                @Override
                protected void configure() {
                    // Or just annotate getbEntities() in AEntity 
                    // with @Mapping("bDtos")
                    mapping(AEntity.class, ADto.class)
                            .fields("bEntities", "bDtos");

                    // Specify custom conversion for the Long field
                    mapping(BEntity.class, BDto.class)
                      .fields("produktId", "produktId",
                        FieldsMappingOptions.customConverter(
                                LongToStringConverter.class));
                }
            };

            // Pass the custom mappings to Dozer
            DozerBeanMapper beanMapper = new DozerBeanMapper();
            beanMapper.addMapping(mappingBuilder);
            mapper = beanMapper;
        }

        return mapper;
    }
}

The converter might look something like this: 转换器可能看起来像这样:

import org.dozer.CustomConverter;

public class LongToStringConverter implements CustomConverter {
    @Override
    public Object convert(Object existingDestFieldValue, Object srcFieldValue,
                          Class<?> destinationClass, Class<?> sourceClass) {
        if (srcFieldValue != null && srcFieldValue instanceof Long
                && String.class.equals(destinationClass)) {
            return String.format("%04d", (Long)srcFieldValue);
        }

        return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM