MapStruct Mapper作为Spring Framework Converter-可以惯用吗? [英] MapStruct Mapper as Spring Framework Converter - idiomatic use possible?

查看:93
本文介绍了MapStruct Mapper作为Spring Framework Converter-可以惯用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 MapStruct 映射器与Spring的

I'd like to combine MapStruct mappers with Spring's Conversion model. So I declare every Mapper interface as an extension of Spring's Converter:

@Mapper
public interface CarMapper extends Converter<Car, CarDto> {    
    @Override
    CarDto convert(Car car);    
}

然后我可以通过注入标准的 ConversionService 来使用mapper bean:

I can then use the mapper beans by injecting the standard ConversionService:

class CarWarehouse {
    @Autowired
    private ConversionService conversionService;

    ...

    public CarDto getCarInformation(Car car) {
        return conversionService.convert(car, CarDto.class);
    }
}

这很好用,但是我想知道是否有一种方法可以避免通过 uses 属性直接将某些Mappers注入到其他对象中.我想做的就是告诉Mapper 使用 ConversionService 来使用另一个Mapper.但是,由于 ConversionService convert 方法与MapStruct的映射方法的标准模式不匹配,因此代码生成插件无法识别在以下情况下可以使用该服务寻找一个子映射.基本上,我想做的就是写

This works nicely, but I'm wondering whether there's a way to avoid injecting some Mappers into others directly via the uses attribute. What I'd like to do is tell a Mapper to use the ConversionService for employing another mapper. However, since the ConversionService's convert method doesn't match MapStruct's standard pattern for a mapping method, the code generation plugin doesn't recognise that it can use the service when looking for a submapping. Basically, what I want to do is write

@Mapper(uses=ConversionService.class)
public interface ParentMapper extends Converter<Parent, ParentDto>

代替

@Mapper(uses={ChildMapper1.class, ChildMapper2.class, ChildMapper3.class})
public interface ParentMapper extends Converter<Parent, ParentDto>

有没有办法做到这一点?

Is there a way to achieve this?

修改

由于被问到,所以说我已经定义了如上所述的 CarMapper ,类型为 Car CarDto 的属性分别为 Wheel WheelDto 类型的 wheel .然后,我希望能够像这样定义另一个Mapper:

Since it's been asked, let's say I've got a CarMapper defined as above, with the types Car and CarDto having an attribute wheel of type Wheel and WheelDto, respectively. Then I'd like to be able to define another Mapper like this:

@Mapper
public interface WheelMapper extends Converter<Wheel, WheelDto> {    
    @Override
    WheelDto convert(Wheel wheel);    
}

现在,我必须显式添加此Mapper:

Right now, I'd have to add this Mapper explicitly:

@Mapper(uses = WheelMapper.class)
public interface CarMapper extends Converter<Car, CarDto>

然后将为生成的 CarMapperImpl 一个类型为 WheelMapper @Autowired 成员,该成员将被调用以映射属性车轮.

Which would then give the generated CarMapperImpl an @Autowired member of type WheelMapper which would be called in order to map the attribute wheel.

但是,我想要的是生成的代码看起来像这样:

However, what I'd like is that the generated code would look somewhat like this:

@Component
public class CarMapperImpl implements CarMapper {
    @Autowired
    private ConversionService conversionService;
    @Override
    public CarDto convert(Car car) {
        CarDto carDto = new CarDto();
        carDto.setWheel(conversionService.convert(car.getWheel(), WheelDto.class);
        return carDto;
    }
}

推荐答案

自从我问了这个问题已经一年多了,但是现在我们在MapStruct项目本身中提出了一个答案- MapStruct Spring Extensions 项目.

It's been more than a year since I asked this question, but now we've come up with an answer inside the MapStruct project itself - the MapStruct Spring Extensions project.

A CarMapper 示例作为项目内的示例提供.

A CarMapper example is provided as an example within the project.

这篇关于MapStruct Mapper作为Spring Framework Converter-可以惯用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆