MapStruct文档(三)——Spring注入

通常我们的项目都是基于spring的,对象的注入就必不可少。

在pom文件的bulid->plugins->plugin(maven-compiler-plugin)->configuration下添加相应配置。mapstruct配置


...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.4.1.Final</version>
            </path>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.8</version>
            </path>
        </annotationProcessorPaths>
        <showWarnings>true</showWarnings>
        <compilerArgs>
			<compilerArg>
    			-Amapstruct.verbose=true
			</compilerArg>
            <compilerArg>
				<!--   以spring注入的方式访问mapper-->
                -Amapstruct.defaultComponentModel=spring
            </compilerArg>
			<compilerArg>
				<!--   注入方式 默认是字段注入 设置为constructor是构造器注入-->
                -Amapstruct.defaultInjectionStrategy=field
            </compilerArg>
        </compilerArgs>
    </configuration>
</plugin>
...
配置showWarning和mapstruct.verbose=true会在编译时打印相关转换信息。

@Mapper注解上的componentModel(基于spring通常这里不需要覆盖默认配置)和injectionStrategy(InjectionStrategy.CONSTRUCTOR可以设置基于构造器注入覆盖默认配置的字段注入)会覆盖默认配置中的设置。


@Mapper
public interface TestTwoMapper {

    TestBO testToBO(TestPO testPO);
}


@Component
public class TestTwoMapperImpl implements TestTwoMapper {

    @Override
    public TestBO testToBO(TestPO testPO) {
        if ( testPO == null ) {
            return null;
        }

        TestBO testBO = new TestBO();

        testBO.setId( testPO.getId() );
        testBO.setName( testPO.getName() );
        testBO.setPrice( testPO.getPrice() );
        testBO.setCreteTime( testPO.getCreteTime() );

        return testBO;
    }
}
 
@Mapper(injectionStrategy = InjectionStrategy.CONSTRUCTOR, uses = {TestTwoMapper.class})
public interface TestMapper {

    List<TestBO> testToBOS(List<TestPO> testPOS);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    private final TestTwoMapper testTwoMapper;

	// 以构造器方式注入了TestTwoMapper
    @Autowired
    public TestMapperImpl(TestTwoMapper testTwoMapper) {

        this.testTwoMapper = testTwoMapper;
    }

    @Override
    public List<TestBO> testToBOS(List<TestPO> testPOS) {
        if ( testPOS == null ) {
            return null;
        }

        List<TestBO> list = new ArrayList<TestBO>( testPOS.size() );
        for ( TestPO testPO : testPOS ) {
            list.add( testTwoMapper.testToBO( testPO ) );
        }

        return list;
    }
}
 
...
 
// 注入mapeer
@Resource
private TestMapper testMapper;


 
List<TestPO> list = new ArrayList<>();
for (int i = 0; i < 2; i++) {
    TestPO testPO = new TestPO();
    testPO.setId(1L);
    testPO.setName("haru");
    testPO.setPrice(new BigDecimal("12.02"));
    testPO.setCreteTime(new Date(System.currentTimeMillis()));
    list.add(testPO);
}

List<TestBO> testBOS = testMapper.testToBOS(list);
System.out.println(testBOS);
 
...

不设置@Mapper中的InjectionStrategy.CONSTRUCTOR覆盖默认配置时,就还是字段注入。


@Component
public class TestMapperImpl implements TestMapper {

	// 字段注入
    @Autowired
    private TestTwoMapper testTwoMapper;

    @Override
    public List<TestBO> testToBOS(List<TestPO> testPOS) {
        if ( testPOS == null ) {
            return null;
        }

        List<TestBO> list = new ArrayList<TestBO>( testPOS.size() );
        for ( TestPO testPO : testPOS ) {
            list.add( testTwoMapper.testToBO( testPO ) );
        }

        return list;
    }
}

 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值