通常我们的项目都是基于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;
}
}