java - 推土机升级到mapstruct

标签 java dozer mapstruct

我正在使用dozer在我的应用程序中进行 bean 到 bean 映射...由于它的漏洞,我想升级到 mapstruct

在 dozer 中,我们有一个用于 bean 到 bean 映射的映射器函数,它为所有不同的实例进行映射。

在 MapStruct 中,我无法使用相同的一种方法来实现。

import java.util.Map;
import org.dozer.DozerBeanMapper;
import org.dozer.MappingException;

/**
 * Factory for creating DTO objects based on the object identifier that
 * is passed to the object creation method.
 */
public class RetrieveChangeSetObjectDtoFactory {
    private Map<String, String> objectIdentifiers;  
    private DozerBeanMapper beanMapper;

    public Object createDto(String objectIdentifier, Persistence srcObject) throws MappingException, ClassNotFoundException {
        String className = objectIdentifiers.get(objectIdentifier);

        if (className == null) {
            return null;
        }

        return beanMapper.map(srcObject, Class.forName(className));
    }

    /**
     * Setter for the object identifiers map, maps the domain objects to their associated DTO classes.
     * 
     * @param objectIdentifiers object identifiers map
     */
    public void setObjectIdentifiers(Map<String, String> objectIdentifiers) {
        this.objectIdentifiers = objectIdentifiers;
    }

    /**
     * Setter for the bean mapper.
     * 
     * @param beanMapper bean mapper (dozer)
     */
    public void setBeanMapper(DozerBeanMapper beanMapper) {
        this.beanMapper = beanMapper;
    }
}

beanmapper.map 正在为我映射对象...通过 hashmap spring bean 加载映射对象

想要有相同的一种方法来映射存储在 hashmap 中的所有对象

enter image description here

这是我的 Spring Dozer bean

最佳答案

MapStruct 和 Dozer 之间的一个重要区别是 MapStruct 是一个注释处理器工具,这意味着它生成代码。您必须创建接口(interface)/映射来生成您需要的映射代码。

MapStruct 没有执行通用映射的单个入口点。但是,如果您愿意,您可以自己实现类似的东西。

您需要一个所有映射器都将实现的基本接口(interface)

public interface BaseMapper<S, T> {

    T toDto(S source);

    S toEntity(T target);
}

然后,您需要以稍微不同的方式实现 RetrieveChangeSetObjectDtoFactory

public class RetrieveChangeSetObjectDtoFactory {

    private Map<Class<?>, Map<Class<?>, BaseMapper<?, ?>>> entityDtoMappers = new HashMap<>();

    public <S, T> Object createDto(Class<S> entityClass, Class<T> dtoClass, S source) {
        if (source == null) {
            return null;
        }

        return getMapper(entityClass, dtoClass).toDto(source);
    }

    public <S, T> Object createSource(Class<S> entityClass, Class<T> dtoClass, T dto) {
        if (dto == null) {
            return null;
        }

        return getMapper(entityClass, dtoClass).toEntity(dto);
    }

    @SuppressWarnings("unchecked")
    protected <S, T> BaseMapper<S, T> getMapper(Class<S> entityClass, Class<T> dtoClass) {
        // appropriate checks
        return (BaseMapper<S, T>) entityDtoMappers.get(entityClass).get(dtoClass);
    }

    public <S, T> void registerMapper(Class<S> entityClass, Class<T> dtoClass, BaseMapper<S, T> mapper) {
        entityDtoMappers.computeIfAbsent(entityClass, key -> new HashMap<>()).put(dtoClass, mapper);
    }
}

但是,我建议只注入(inject)您需要的映射器,而不是做一些通用的事情。

关于java - 推土机升级到mapstruct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772340/

相关文章:

java - 获取要在服务任务中使用的嵌入表单变量的值

java - Tinkerpop:创建顶点后设置标签

java - Dozer 映射字段类型 ="one-way"未按预期工作

java - MapStruct:将嵌套对象属性映射到属性

java - 使用java Mapstruct的模糊映射方法

java - 如何在 Spring Boot 中使用 Mapstruct 映射 parent 和 child ?

java - 如何配置 objectMapper 在失败时不创建新实例?

java - Mockito:WAITING与参数匹配的调用

logging - 如何关闭 log4j2 中的 Dozer 日志条目?

java - 一个与Spring boot devtools相关的dozer map异常