java—是否可以使用orika mapper为某些类创建一个配置?

zengzsys  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(352)

我尝试配置orika mapper。我有5个实体和5个dto。我的配置是正确的,但是我找不到关于如何为不同类配置Map器的任何信息。那么,是可以为某些类创建一个配置,还是应该为每对类创建一个配置呢?

@Configuration 
public class MapperConfig implements OrikaMapperFactoryConfigurer {

@Bean
DatatypeFactory datatypeFactory() throws DatatypeConfigurationException {
    return DatatypeFactory.newInstance();
}

@Bean
DefaultMapperFactory.MapperFactoryBuilder<?, ?> orikaMapperFactoryBuilder() {
    DefaultMapperFactory.Builder orikaMapperFactoryBuilder = new DefaultMapperFactory.Builder();
    return orikaMapperFactoryBuilder;
}

@Bean
public MapperFactory orikaMapperFactory(DefaultMapperFactory.MapperFactoryBuilder<?, ?> orikaMapperFactoryBuilder) {
    MapperFactory orikaMapperFactory = orikaMapperFactoryBuilder.build();
    this.configure(orikaMapperFactory);
    return orikaMapperFactory;
}

public void configure(MapperFactory orikaMapperFactory) {
    orikaMapperFactory.classMap(Author.class, AuthorDto.class)
            .byDefault()
            .register();
}

@Bean
public MapperFacade orikaMapperFacade(MapperFactory orikaMapperFactory) {
    MapperFacade orikaMapperFacade = orikaMapperFactory.getMapperFacade();
    return orikaMapperFacade;
}

}
fsi0uk1n

fsi0uk1n1#

可以,您需要在configure方法中为其他两对类注册Map器
例子:

public void configure(MapperFactory orikaMapperFactory) {
      orikaMapperFactory.classMap(Author.class, AuthorDto.class)
      .byDefault()
      .register();
      orikaMapperFactory.classMap(A.class, B.class)
      .byDefault()
      .register();
}

相关问题