- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用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 中的所有对象
这是我的 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/
我有一个类似于以下两个类的场景: public class Person { private String name; private Set familyMembers; /
如何将单个字段映射到 Dozer 中的 List/Collection? class SrcFoo { private String id; private List bars; }
我正在通过 Dozer 映射器映射两个 DTO 对象。我有兴趣从列表中选择一个值并将其映射到目标文件中的单个字段。 是否可以像这样使用映射: someList[0] someVariab
我正在使用推土机来映射对象。如何忽略(排除)使用推土机注释的字段? 类似于: class A { @IgnoreField public String so
我已经被这个麻烦困扰了两天了,但我无法解决。我有: public class ClientBo{ ... List person; ... } 和 public class ClientV
为什么dozer无法将Number类型的Source Class变量映射到相同类型的Destination Class变量? 出现以下异常: java.lang.IllegalArgumentExce
如何以编程方式为推土机设置自定义转换器?以下代码不起作用: 自定义转换器实现: class ConverterImpl extends DozerConverter { ConverterImpl()
我需要编译一个针对特定架构或更好的程序,使用通用 AMD64 指令编译,忽略更新的 AMD cpu(推土机和打桩机)。 具体来说,我需要绝对忽略任何 FMA 或 XOP 指令(我的理解是这些通常在 O
我想将DTO(均为字符串数据类型)映射到VO(包含String、int、boolean、Date) 学生DTO private StudentDetailDTO student; 学生详细信息DTO:
我有一个列表列表,我正在尝试使用 Dozer 和自定义转换器将其映射到二维数组 [][]。 public class Field { List items; public void a
有没有办法通过它的 xml 映射文件来配置 dozer 以将 -1 的原始 int 字段值转换为空对象引用? 遗留对象模型默认值为 -1,因此零可以是一个有效的选择。 我们要映射到的较新的对象模型假定
我正在尝试在我当前运行 Spring 和 Hibernate 的 Web 应用程序上实现 Dozer 作为桥接模式的帮助,但我认为我做错了什么,因为尝试多次使用 DozerBeanMapper 实例会
我是一名优秀的程序员,十分优秀!