Problem Description
During project development, object conversion was used mapStruct
, List
and the parameters in the object changed. @Maping
Annotations were used, and it was found that the converted parameters were not converted, and the content was null.
@Mapping(source = "jsid", target = "id")
@Mapping(source = "roleGroupId", target = "pid")
List<SystemRoleCO> toListSystemRoleCO(List<UserRoleDO> userRoleDOS);
Cause Analysis:
Clicking on the implementation class method found that the attribute to be converted was not generated. A colleague pointed out that it may be necessary to have a conversion method for this object first, which is strange.
protected SystemRoleCO userRoleDOToSystemRoleCO(UserRoleDO userRoleDO) {
if ( userRoleDO == null ) {
return null;
}
SystemRoleCO systemRoleCO = new SystemRoleCO();
systemRoleCO.setJsmc( userRoleDO.getJsmc() );
systemRoleCO.setJsjb( userRoleDO.getJsjb() );
return systemRoleCO;
}
solution:
Define an object before definition List<对象>
and the conversion will be successful.
@Mapping(source = "jsid", target = "id")
@Mapping(source = "roleGroupId", target = "pid")
SystemRoleCO toListSystemRoleCO(UserRoleDO userRoleDOS);
@Mapping(source = "jsid", target = "id")
@Mapping(source = "roleGroupId", target = "pid")
List<SystemRoleCO> toListSystemRoleCO(List<UserRoleDO> userRoleDOS);