它会在启动时会自动生成同名复制,生成一个临时文件,避免bean拷贝时使用反射带来的性能损害,且字段检查也是在编译时完成的,基本上就是运行时无性能损耗
1.导入依赖
<!--这里我用的1.5.3.Final-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>
)
原因 :这个一般是编译器自检问题,应该是lombok在mapstruct之前加载导致的
线上的:线上一般没问题,一般是用maven编译的,只要你本地使用maven编译没问题,那线上就没问题,不会使用编译器的
解决方法: Setting -->Build,Execution,Deployment -->Compiler 加上参数: -Djps.track.ap.dependencies=false
java: Internal error in the mapping processor: java.lang.NullPointerExceptio
@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, unmappedTargetPolicy = ReportingPolicy.IGNORE, builder = @Builder(disableBuilder = true))
public interface UserMapper {
Demo2 toDemo2(Demo demo);
}
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private UserConver userConver;
@RequestMapping("/t1")
@Sensitive
public void test() {
Demo demo = new Demo(1,2,"s1",0);
Demo2 demo2 = userConver.toDemo2(demo);
}
}
@Mapper(componentModel = "default", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, unmappedTargetPolicy = ReportingPolicy.IGNORE, builder = @Builder(disableBuilder = true))
public interface DemoConverter {
DemoConverter INSTANCE = Mappers.getMapper(DemoConverter.class);
//单个bean转换(结合注解大全使用)
Demo req2Entity(DemoReq req);
//注意:要使用集合转换必须先声明单个bean转换,即上面这个方法
List<Demo >req2Entity(List<DemoReq> list);
}
public void agree(DemoReqreq) {
Demo entity = DemoConverter .INSTANCE.req2Entity(req);
}