关于mapStruct对List对象转换遇到的问题

问题描述

在项目开发中,用到了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);
经验分享 程序员 微信小程序 职场和发展