# mapstruct-spring-plus **Repository Path**: zhaord/mapstruct-spring-plus ## Basic Information - **Project Name**: mapstruct-spring-plus - **Description**: No description available - **Primary Language**: Java - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 3 - **Created**: 2021-04-21 - **Last Updated**: 2024-03-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mapstruct-spring-plus 是mapstruct和spring的增强包,简化mapstruct结合spring的使用方式,实现方式参考了mapstruct-spring-extensions项目 创建Dto和Entity ``` java @Data @AutoMap(targetType = Car.class) public class CarDto { private String make; @AutoMapField(target = "carType") private String type; } @Data public class Car { private String make; private String carType; } ``` ## 使用 MapStruct Spring Plus ### Maven 对于maven项目,使用如下的POM文件 ```xml ... 1.4.2.Final 1.0.0.RELEASE ... org.mapstruct mapstruct ${org.mapstruct.version} io.github.zhaord mapstruct-spring-plus-boot-starter ${io.github.zhaord.version} ... org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 org.mapstruct mapstruct-processor ${org.mapstruct.version} io.github.zhaord mapstruct-spring-plus-processor ${io.github.zhaord.version} ... ``` ### Gradle 对于Gradle项目,使用如下配置 ``` gradle dependencies { ... compile 'org.mapstruct:mapstruct:1.4.2.Final' compile 'io.github.zhaord:mapstruct-spring-plus-boot-starter:1.0.0.RELEASE' annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' testAnnotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' // if you are using mapstruct in test code annotationProcessor 'io.github.zhaord:mapstruct-spring-plus-processor:1.0.0.RELEASE' testAnnotationProcessor 'io.github.zhaord:mapstruct-spring-plus-processor:1.0.0.RELEASE' // if you are using mapstruct in test code ... } ``` ### 使用案例 sprign中注入 `IObjectMapper`, 调用 `objectMapper.map(dto, Car.class)` ``` java @ExtendWith(SpringExtension.class) @ContextConfiguration( classes = {AutoMapTests.AutoMapTestConfiguration.class}) public class AutoMapTests { @Autowired private IObjectMapper mapper; @Test public void testDtoToEntity() { var dto = new CarDto(); dto.setMake("M1"); dto.setType("OTHER"); Car entity = mapper.map(dto, Car.class); assertThat(entity).isNotNull(); assertThat(entity.getMake()).isEqualTo("M1"); assertThat(entity.getCarType()).isEqualTo("OTHER"); } @ComponentScan("io.github.zhaord.mapstruct.plus") @Configuration @Component static class AutoMapTestConfiguration { } } ``` ## 问题 * `@Mapping` 注解属性 `@AutoMapField` 继承自`@Mapping` 注解,素有`@Mapping`属性都可以在`@AutoMapField`中使用