具有完全相同字段的对象之间的 Mapstruct
Mapstruct between objects having exactly same fields
我一直在使用 mapstruct 映射 类 的对象,它们略有不同。
现在,我有一个用例,其中两个 类 完全 相同。
类 之一是 BO(资格),另一个是具有完全相同字段的 DTO(资格记录)。
如何使用 @Mapper
在这两种类型之间进行转换?
到目前为止,我正在做
@Mapping(source = "qualificationId", target = "qualificationId")
QualificationRecord getQualificationRecordFromQualification(final Qualification qualification);
并且它能够生成映射器,设置所有字段。
但是,source = "qualificationId", target = "qualificationId"
似乎是多余的,我不得不添加它只是因为没有可用的无参数 @Mapping()
注释。
有没有办法让 Mapper 复制所有的字段,而不写一行多余的内容?
只需在接口中定义映射方法,这样就会将所有字段从一个对象复制到另一个对象:
/**
* Mapper. Automatically implemented by mapstruct.
*
*/
@Mapper
public interface SomeObjMapper {
/**
* instance.
*/
final SomeObjMapper INSTANCE = Mappers.getMapper(SomeObjMapper.class);
/**
* Mapper method to map entity to domain. Automatically implemented by mapstruct.
*
* @param entity
* given entity.
* @return Returns the domain object.
*/
SomeObj entityToDomain(SomeObjEntity entity);
/**
* Mapper method to map domain object to entity. Automatically implemented by mapstruct.
*
* @param domain
* given domain object.
* @return Returns the entity.
*/
SomeObjEntity domainToEntity(SomeObj domain);
}
我一直在使用 mapstruct 映射 类 的对象,它们略有不同。
现在,我有一个用例,其中两个 类 完全 相同。 类 之一是 BO(资格),另一个是具有完全相同字段的 DTO(资格记录)。
如何使用 @Mapper
在这两种类型之间进行转换?
到目前为止,我正在做
@Mapping(source = "qualificationId", target = "qualificationId")
QualificationRecord getQualificationRecordFromQualification(final Qualification qualification);
并且它能够生成映射器,设置所有字段。
但是,source = "qualificationId", target = "qualificationId"
似乎是多余的,我不得不添加它只是因为没有可用的无参数 @Mapping()
注释。
有没有办法让 Mapper 复制所有的字段,而不写一行多余的内容?
只需在接口中定义映射方法,这样就会将所有字段从一个对象复制到另一个对象:
/**
* Mapper. Automatically implemented by mapstruct.
*
*/
@Mapper
public interface SomeObjMapper {
/**
* instance.
*/
final SomeObjMapper INSTANCE = Mappers.getMapper(SomeObjMapper.class);
/**
* Mapper method to map entity to domain. Automatically implemented by mapstruct.
*
* @param entity
* given entity.
* @return Returns the domain object.
*/
SomeObj entityToDomain(SomeObjEntity entity);
/**
* Mapper method to map domain object to entity. Automatically implemented by mapstruct.
*
* @param domain
* given domain object.
* @return Returns the entity.
*/
SomeObjEntity domainToEntity(SomeObj domain);
}