问题描述
在项目开发中,用到了mapStruct对List对象转换,对象里面参数有变化,用到了@Maping注解,发现里面该转换的参数没有转换,并且内容为null。
@Mapping(source = "jsid", target = "id")
@Mapping(source = "roleGroupId", target = "pid")
List<SystemRoleCO> toListSystemRoleCO(List<UserRoleDO> userRoleDOS);
原因分析:
点进实现类方法发现要转换的属性没有生成,经同事指点可能是要先有这个 对象对象的转换方法,奇奇怪怪的。
protected SystemRoleCO userRoleDOToSystemRoleCO(UserRoleDO userRoleDO) {
if ( userRoleDO == null ) {
return null;
}
SystemRoleCO systemRoleCO = new SystemRoleCO();
systemRoleCO.setJsmc( userRoleDO.getJsmc() );
systemRoleCO.setJsjb( userRoleDO.getJsjb() );
return systemRoleCO;
}
解决方案:
在定义List<对象>之前定义一个对象就可以转换成功了。
@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);
在项目开发中,使用MapStruct进行List<UserRoleDO>到List<SystemRoleCO>的转换时,发现@Mapping注解未生效,转换后的对象参数为null。问题源于MapStruct未生成对应的属性转换方法。解决方案是先定义单个对象的转换方法(UserRoleDO到SystemRoleCO),然后再定义List的转换方法,这样MapStruct就能正确处理列表转换了。
8035





