目录
映射对象
package example.model;
import lombok.Data;
@Data
public class Source {
private Integer gender;
}
package example.model;
import lombok.Data;
@Data
public class Target {
private String gender;
}
Spring中的bean
package example.component;
import org.springframework.stereotype.Component;
@Component
public class SomeComponent {
public void doSomething() {
}
}
开始写Mapper
创建Mapper接口
创建Mapper接口,但是建议不要急于引入mapstruct注解,想加也可以,反正等下都要回来改
package example.mapper;
import example.model.Source;
import example.model.Target;
public interface DecoratorMapper {
Target sourceToTarget(Source source);
}
创建Mapper的修饰器
这是一个抽象类,实现Mapper接口,并重写需要使用Spring中bean的方法,要特别注意的是修饰器里面装配的Mapper属性必须指明bean名称为delegate,在修饰器中实现调用其他bean等复杂逻辑即可
package example.mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import example.component.SomeComponent;
import example.model.Source;
import example.model.Target;
public abstract class DecoratorMapperDecorator implements DecoratorMapper {
@Autowired
private SomeComponent someComponent;
@Autowired
@Qualifier("delegate") //必须叫这个名字,mapstruct默认的不能改
private DecoratorMapper decoratorMapper;
@Override
public Target sourceToTarget(Source source) {
Target target = decoratorMapper.sourceToTarget(source);
someComponent.doSomething();
return target;
}
}
在Mapper接口指定修饰器
现在回来修改Mapper接口,对修饰器进行指定
package example.mapper;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import example.model.Source;
import example.model.Target;
@Mapper(componentModel = "spring")
@DecoratedWith(DecoratorMapperDecorator.class)
public interface DecoratorMapper {
Target sourceToTarget(Source source);
}
编译产物
默认实现
mapstruct默认的实现,唯一的不同就是多了个Qualifier命名
package example.mapper;
import example.model.Source;
import example.model.Target;
import javax.annotation.Generated;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-01-29T17:11:04+0800",
comments = "version: 1.5.3.Final, compiler: Eclipse JDT (IDE) 1.4.300.v20221108-0856, environment: Java 17.0.3 (Eclipse Adoptium)"
)
@Component
@Qualifier("delegate")
public class DecoratorMapperImpl_ implements DecoratorMapper {
@Override
public Target sourceToTarget(Source source) {
if ( source == null ) {
return null;
}
Target target = new Target();
if ( source.getGender() != null ) {
target.setGender( String.valueOf( source.getGender() ) );
}
return target;
}
}
修饰器实现
修饰器是个抽象类,所以mapstruct会生成一个对应的实现,并且作为主实现类注入到spring
package example.mapper;
import javax.annotation.Generated;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-01-29T17:11:04+0800",
comments = "version: 1.5.3.Final, compiler: Eclipse JDT (IDE) 1.4.300.v20221108-0856, environment: Java 17.0.3 (Eclipse Adoptium)"
)
@Component
@Primary
public class DecoratorMapperImpl extends DecoratorMapperDecorator {
}
调用逻辑
在spring中使用Mapper进行对象转换时,调用逻辑如下:
- DecoratorMapperImpl被注入到调用者类
- DecoratorMapperImpl执行其父类DecoratorMapperDecorator中的方法
- 在DecoratorMapperDecorator中,DecoratorMapperImpl_这个默认实现类被以delegate为名称注入
- DecoratorMapperDecorator中的逻辑方法就可以调用DecoratorMapperImpl_以mapstruct正常的方式对对象进行处理,然后在对对象进行细节修饰并返回