如何使用spring injection,mapstruct mapper中的存储库类?

mitkmikd  于 2023-06-21  发布在  Spring
关注(0)|答案(3)|浏览(124)
@Data
public class FilesDTO {

    private int issue;
    private String uniqueStr;
    private StorageDomain xml;
    private StorageDomain pdf;
    private StorageDomain stop;
}
@Data
public class BackHalfDomain {
    private int articleId;
    private String uniqueStrr;
    private long xmlContentId;
    private long pdfContentId;
    private long stopId;
    private int issueNumber;
}

使用存储库类,我必须从BackHalfDomain中的ID获取StorageDomain对象。因此,我必须将StorageDomain对象Map到相应的字段。比如StorgeDomain sd = repo.findById(id).get();,并在FilesDTO的xml字段中设置这个sd对象,等等。这是我的绘图仪

@Mapper(componentModel = "spring")
public interface FilesDTOMapper {

    public static final FilesDTOMapper fileDTOMapper = Mappers.getMapper(FilesDTOMapper.class);

    @Mapping(target = "issue", source = "domain.issueNumber")
    @Mapping(target = "DOI", source = "domain.doi")
    public FilesDTO map(BackHalfDomain domain);

}

我用过uses,但不成功。我用过@Mapping(target="xyz", expression="java(repo.findById(id))")",但我得到的是NullPointerException Spring注入不工作。有人能解决这个问题吗?我刚开始用mapstruct

lokaqttq

lokaqttq1#

从mapstruct 1.2开始,您可以使用@AfterMapping和@Context的组合。

@Mapper(componentModel="spring")
public interface FilesDTOMapper { 

   @Mapping(target = "xyz", ignore = true)
   @Mapping(target = "issue", source = "domain.issueNumber")
   @Mapping(target = "DOI", source = "domain.doi")
   FilesDTO map( BackHalfDomain domain, @Context MyRepo repo);

   @AfterMapping
   default void map( @MappingTarget FilesDTO target, BackHalfDomain domain, @Context MyRepo repo) {
        target.setXYZ(repo.findById(domain.getId()));
   }
 }

在1.1中,您需要将Map器转换为抽象类

@Mapper(unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE,
        componentModel = "spring",
        uses = {})
public abstract class FilesDTOMapper {

    @Autowired
    protected MyRepo repo;

    @Mapping(target = "issue", source = "domain.issueNumber")
    @Mapping(target = "DOI", source = "domain.doi")
    @Mapping(target="xyz", expression="java(repo.findById(domain.getId())))")
    public FilesDTO map(BackHalfDomain domain);

}
ar5n3qh5

ar5n3qh52#

我也遇到了同样的问题。解决方案是使用this answer中建议的Decorator。按照您的代码,解决方案将类似于以下内容。
首先,我们必须在Mapper中指定Decorator:

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
@Primary
@DecoratedWith(FilesDTOMapperDecorator.class)
public interface FilesDTOMapper {
      // basic mapping here
      public FilesDTO map(BackHalfDomain domain);
}

然后,实现Decorator:

public abstract class FilesDTOMapperDecorator implements FilesDTOMapper {

    @Autowired
    @Qualifier("delegate")
    private FilesDTOMapper delegate;

    @Autowired
    private SomeRepository someRepository;

    @Override
    public FilesDTO map(BackHalfDomain domain) {
        // Delegate basic mapping
        FilesDTO filesDTO = delegate.map(domain);

        // Use the repository as needed to set additional mapping
        filesDTO.setSomeValue(repo.findById(id).getSomeValue());

        return filesDTO;
    }
}
whhtz7ly

whhtz7ly3#

您需要像bean一样将Map器注入到服务中。

相关问题