<properties>
<java.version>11</java.version>
<mapstruct.version>1.2.0.Final</mapstruct.version>
</properties>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>
@Mapper
public interface ConvertTest {
ConvertTest INSTANCE = Mappers.getMapper(ConvertTest.class);
Son toSon(Father father);
}
?编译代码后,生成如下编译代码
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2022-03-09T16:42:08+0800",
comments = "version: 1.2.0.Final, compiler: javac, environment: Java 1.8.0_66 (Oracle Corporation)"
)
public class ConvertTestImpl implements ConvertTest {
@Override
public Son toSon(Father father) {
if ( father == null ) {
return null;
}
Son son = new Son();
son.setId( father.getId() );
son.setName( father.getName() );
return son;
}
}
@RestController
@RequestMapping(value = "demo")
public class Controller {
private ConvertTest convertTest = ConvertTest.INSTANCE;
@GetMapping(value = "test")
public Son test(){
Father father= new Father();
father.setId(1);
father.setSex("2");
father.setName("3");
return convertTest.toSon(father);
}
}
|