java – MapStruct泛型Map和map组合的子对象列表

我有父类:User.java和2个类:FacebookUser.java和TwitterUser.java它们是返回的实体,取决于使用DiscriminatorColumn的数据库中的类型列,我想写出正确的映射器来映射用户可能是实例的FacebookUser或TwitterUser.我有以下映射器似乎无法正常工作,只映射用户父级而不是子级:

@Mapper
public interface UserMapper {
    public static UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    User map(UserDTO userDTO);

    @InheritInverseConfiguration
    UserDTO map(User user);

    List<UserDTO> map(List<User> users);

    FacebookUser map(FacebookUserDTO userDTO);

    @InheritInverseConfiguration
    FacebookUserDTO map(FacebookUser user);

    TwitterUser map(TwitterUserDTO userDTO);

    @InheritInverseConfiguration
    TwitterUserDTO map(TwitterUser user);

}

然后我用:

UserDTO userDto = UserMapper.INSTANCE.map(user);

要映射的类:

@Entity
@Table(name = "users")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 10)
@DiscriminatorValue(value = "Local")
public class User {
    @Column
    private String firstName;
    @Column
    private String lastName;
    ///... setters and getters
}

@Entity
@DiscriminatorValue(value = "Facebook")
public class FacebookUser extends User {
    @Column
    private String userId;
    ///... setters and getters
}

@Entity
@DiscriminatorValue(value = "Twitter")
public class TwitterUser extends User {
    @Column
    private String screenName; 
    ///... setters and getters
}

DTO:

public class UserDTO {
    private String firstName;
    private String lastName;
    ///... setters and getters
}

public class FacebookUserDTO extends UserDTO {
    private String userId;
    ///... setters and getters
}

public class TwitterUserDTO extends UserDTO {
    private String screenName; 
    ///... setters and getters
}

此外,如果我有与Facebook用户和Twitter用户或基本用户混合的用户列表:

假设我有以下用户:

User user = new User ("firstName","lastName");
User fbUser = new FacebookUser ("firstName","lastName","userId");
User twUser = new TwitterUser ("firstName","lastName","screenName");

List<User> users = new ArrayList<>();
users.add(user);
users.add(fbUser);
users.add(twUser);

//Then: 

List<UserDTO> dtos = UserMapper.INSTANCE.map(users);

我只获得firstName和lastName,但不是screenName或userId.

对此有何解决方案?

解决方法:

目前它似乎还没有作为mapstruct的功能:Support for Type-Refinement mapping (or Downcast Mapping)

我在谷歌小组中提出了这个问题:https://groups.google.com/forum/?fromgroups#!topic/mapstruct-users/PqB-g1SBTPg

并发现我需要在界面内部使用默认方法进行手动映射(对于java 8).

还有另一个问题,即映射父类几乎不适用,所以我写了一个空类,即父类的子类:

所以代码变得如下:

@Mapper
public interface UserMapper {
    public static UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    LocalUser map(LocalUserDTO userDTO);

    @InheritInverseConfiguration
    LocalUserDTO map(LocalUser user);


    List<UserDTO> map(List<User> users);

    FacebookUser map(FacebookUserDTO userDTO);

    @InheritInverseConfiguration
    FacebookUserDTO map(FacebookUser user);

    TwitterUser map(TwitterUserDTO userDTO);

    @InheritInverseConfiguration
    TwitterUserDTO map(TwitterUser user);

    default UserDTO map(User user) {

        if (user instanceof FacebookUser) {
            return this.map((FacebookUser) user);
        } else if (user instanceof TwitterUser) {
            return this.map((TwitterUser) user);
        } else {
            return this.map((LocalUser) user);
        }
    }

    @InheritInverseConfiguration
    default User map(UserDTO userDTO) {
        if (userDTO instanceof FacebookUserDTO) {
            return this.map((FacebookUserDTO) userDTO);
        } else if (userDTO instanceof TwitterUserDTO) {
            return this.map((TwitterUserDTO) userDTO);
        } else {
            return this.map((LocalUserDTO) userDTO);
        }
    }

}
上一篇:java – MapStruct:丰富映射注释以定义自定义映射器


下一篇:如何使用MapStruct应用两个参数映射对象的函数?