简体   繁体   English

如何从Java Stream中的接口调用MapStruct方法

[英]How to call MapStruct method from interface in java stream

I recently started using the MapStruct mapping tool in a project. 我最近开始在项目中使用MapStruct映射工具。 In the past, for mapping DTO -> Entity and vice versa I used custom mapper like: 过去,为了映射DTO->实体,反之亦然,我使用了自定义映射器,例如:

public static CustomerDto toDto(Customer customer) {

    return isNull(customer)
        ? null
        : CustomerDto.builder()
            .id(customer.getId())
            .name(customer.getName())
            .surname(customer.getSurname())
            .phoneNumber(customer.getPhoneNumber())
            .email(customer.getEmail())
            .customerStatus(customer.getCustomerStatus())
            .username(customer.getUsername())
            .NIP(customer.getNIP())
            .build();
  }

In case when I was trying to get one single Optional object after all I was able to map my entity to dto in the following way: 毕竟,当我尝试获取一个可选对象时,可以通过以下方式将我的实体映射到dto:

public Optional<CustomerDto> findOneById(final long id) {
    return customerRepository.findById(id).map(CustomerMapper::toDto);
  }

Currently, as I mentioned before I am using mapStruct and the problem is that my mapper it's, not class, it's the interface like: 当前,正如我之前提到的,我使用mapStruct,问题是我的映射器不是类,而是接口,例如:

@Mapper
public interface CommentMapper {

  @Mappings({
      @Mapping(target = "content", source = "entity.content"),
      @Mapping(target = "user", source = "entity.user")
  })
  CommentDto commentToCommentDto(Comment entity);

  @Mappings({
      @Mapping(target = "content", source = "dto.content"),
      @Mapping(target = "user", source = "dto.user")
  })
  Comment commentDtoToComment(CommentDto dto);

}

I want to know if it possible to use somehow this interface method in stream gentle to map my value without wrapping values like: 我想知道是否可以在流柔和的情况下以某种方式使用此接口方法来映射我的值而无需包装如下值:

public Optional<CommentDto> findCommentById(final long id) {

    Optional<Comment> commentById = commentRepository.findById(id);
    return Optional.ofNullable(commentMapper.commentToCommentDto(commentById.get()));
}

Thanks for any help. 谢谢你的帮助。

Access the mapper like: 像这样访问映射器:

private static final YourMapper MAPPER = Mappers.getMapper(YourMapper.class);

final Optional<YourEntity> optEntity = entityRepo.findById(id);
return optEntity.map(MAPPER::toDto).orElse(null);

Basically we do a similar thing with enumerations 基本上,我们用枚举做类似的事情

 @Mapping(target = "type", expression = "java(EnumerationType.valueOf(entity.getType()))")

you can define java expressions in your @Mapping annotation 您可以在@Mapping批注中定义Java表达式

@Mapping(target = "comment", expression = "java(commentMapper.commentToCommentDto(commentRepository.findById(entity.commentId).orElse(null)))"

Otherwise you should be able to make use of a 否则,您应该可以利用

class CommentMapper { ... }

which you automatically can refer with 您可以自动参考的

@Mapper(uses = {CommentMapper.class})

your implementation will detect the commentEntity and Dto and will automatically use the CommentMapper. 您的实现将检测到commentEntity和Dto,并将自动使用CommentMapper。

A MapStruct mapper is workling like: Shit in Shit out, so remember your entity needs the commentEntity so the dto can has the commentDto. MapStruct映射器的工作方式如下:退出,退出,因此请记住您的实体需要commentEntity,以便dto可以具有commentDto。

EDIT 编辑

2nd solution could be using: 第二个解决方案可能使用:

@BeforeMapping
default void beforeMappingToDTO(Entity source, @MappingTarget Dto target) {
    target.setComment(commentMapper.commentToCommentDto(commentRepository.findById(entity.commentId).orElse(null)));
}

@Spektakulatius answer solved a problem. @Spektakulatius答案解决了一个问题。

To reach a goal I made a few steps: First of all, I created an object of my mapper to use it in a java stream: 为了实现一个目标,我采取了一些步骤:首先,我创建了映射器的对象以在Java流中使用它:

private CommentMapper commentMapper = Mappers.getMapper(CommentMapper.class);

In the last step I used my object commentMapper like: 在最后一步中,我使用了我的对象commentMapper:

public Optional<CommentDto> findCommentById(final long id) {
    return commentRepository.findById(id).map(CommentMapper.MAPPER::commentToCommentDto);
}

After all that completely gave me a possibility to use my custom MapStruct mapper in stream. 毕竟,这完全使我可以在流中使用我的自定义MapStruct映射器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM