@Mapper
public interface EmployeeMapper {
//qualifiedByName take the
@Mapping(source = "employee", target = "fullName", qualifiedByName = "fullNameByFirstAndLastName")
EmployeeDto toDto(Employee employee);
//do the Inverse of the toDto method
@InheritInverseConfiguration(name = "toDto")
Employee fromDto(EmployeeDto dto);
//take the same source emp
@Named("fullNameByFirstAndLastName")
default String fullNameByFirstAndLastName(Employee employee) {
return employee.getFirstname() + "-" + employee.getLastname();
}
/**
* recommended
* we can change the interface to abstract class and the methods also
*/
}
@Data
@Builder
public class Employee {
private int id;
private String firstname;
private String lastname;
}
@Data
public class EmployeeDto {
private int id;
private String fullName;//FirstName+ ' ' +lastName
}
- 未知属性"雇员";在结果类型Employee.EmployeeBuilder中;null";?-这段代码不是编译的,我想在EmployeDtO中将名字与全名中的刊名连接起来
编辑:正如Filip在评论中提到的,这应该从1.5.0开始修正。最终版本之后,请参阅https://github.com/mapstruct/mapstruct/issues/2356.但是您仍然需要为firstname
和lastname
字段创建手动映射。
原因是您反向继承了字段fullName
的映射。反转后的CCD_ 4变为CCD_。
因为这毫无意义,因为由于您没有名为employee
的字段,mapstruct会给出错误。
在这种情况下,您不能使用@InheritInverseConfiguration
路由。相反,您需要自己编写反向映射。
这意味着
//qualifiedByName take the
@Mapping(source = "employee", target = "fullName", qualifiedByName = "fullNameByFirstAndLastName")
EmployeeDto toDto(Employee employee);
//do the Inverse of the toDto method
@InheritInverseConfiguration(name = "toDto")
Employee fromDto(EmployeeDto dto);
将变成类似的东西
//qualifiedByName take the
@Mapping(source = "employee", target = "fullName", qualifiedByName = "fullNameByFirstAndLastName")
EmployeeDto toDto(Employee employee);
//do the Inverse of the toDto method
@Mapping(source = "fullName", target = "firstname", qualifiedByName = "extractFirstNameFromFullName")
@Mapping(source = "fullName", target = "lastname", qualifiedByName = "extractLastNameFromFullName")
Employee fromDto(EmployeeDto dto);
// methods here to extract the first/last name from the fullname.