• JAVA中如何使用Mapstruct进行对象的复制
  • 发布于 1周前
  • 52 热度
    0 评论
在业务应用中,我们的代码结构往往是多层次的,不同层次之间经常涉及到对象的转化,虽然很简单,但实际上繁琐且容易出错。
反例 1
public class UserConverter {

    public static UserDTO toDTO(UserDO userDO) {
        UserDTO userDTO = new UserDTO();
        userDTO.setAge(userDO.getAge());
        // 问题 1: 自己赋值给自己
        userDTO.setName(userDTO.getName());
        return userDTO;
    }

    @Data
    public static class UserDO {
        private String name;
        private Integer age;
        // 问题 2: 新增字段未赋值
        private String address;
    }

    @Data
    public static class UserDTO {
        private String name;
        private Integer age;
    }
}
反例2:
public class UserBeanCopyConvert {
    public UserDTO toDTO(UserDO userDO) {
        UserDTO userDTO = new UserDTO();
        // 堆代码 duidaima.com
        // 用反射复制不同类型对象.
        // 1. 重构不友好, 当我要删除或修改 UserDO 的字段时, 无法得知该字段是否通过反射被其他字段依赖
        BeanUtils.copyProperties(userDO, userDTO);
        return userDTO;
    }
}
使用 Mapstruct
Mapstruct 使用编译期代码生成技术,根据注解, 入参,出参自动生成转化,代码,并且支持各种高级特性,比如:
1.未映射字段的处理策略,在编译期发现映射问题
2.复用工具,方便字段类型转化
3.生成 spring Component 注解,通过 spring 管理
等等其他特性
@Mapper(
    componentModel = "spring",
    unmappedSourcePolicy = ReportingPolicy.ERROR,
    unmappedTargetPolicy = ReportingPolicy.ERROR,
    // convert 逻辑依赖 DateUtil 做日期转化
    uses = DateUtil.class
)
public interface UserConvertor  {

    UserDTO toUserDTO(UserDO userDO);

    @Data
    class UserDO {
        private String name;
        private Integer age;
        //private String address;
        private Date birthDay;
    }

    @Data
    class UserDTO {
        private String name;
        private Integer age;
        private String birthDay;
    }

}

public class DateUtil {
    public static String format(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }
}
使用示例:
@RequiredArgsConstructor
@Component
public class UserService {
    private final UserDao userDao;
    private final UserCovertor userCovertor;

    public UserDTO getUser(String userId){
        UserDO userDO = userDao.getById(userId);
        return userCovertor.toUserDTO(userDO);
    }
}

编译期校验:

生成的代码:
@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-12-18T20:17:00+0800",
    comments = "version: 1.3.1.Final, compiler: javac, environment: Java 11.0.12 (GraalVM Community)"
)
@Component
public class UserConvertorImpl implements UserConvertor {

    @Override
    public UserDTO toUserDTO(UserDO userDO) {
        if ( userDO == null ) {
            return null;
        }

        UserDTO userDTO = new UserDTO();

        userDTO.setName( userDO.getName() );
        userDTO.setAge( userDO.getAge() );
        userDTO.setBirthDay( DateUtil.format( userDO.getBirthDay() ) );

        return userDTO;
    }
}

用户评论