# 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
It is an enhanced package of mapstruct and spring, which simplifies the use of mapstruct combined with spring. The implementation method refers to the mapstruct-spring-extensions project
Create Dto and 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;
}
```
## Use MapStruct Spring Plus
### Maven
For maven projects, use the following POM file
```xml
...
1.4.2.Final1.0.0.RELEASE
...
org.mapstructmapstruct${org.mapstruct.version}io.github.zhaordmapstruct-spring-plus-boot-starter${io.github.zhaord.version}
...
org.apache.maven.pluginsmaven-compiler-plugin3.8.11.81.8org.mapstructmapstruct-processor${org.mapstruct.version}io.github.zhaordmapstruct-spring-plus-processor${io.github.zhaord.version}
...
```
### Gradle
For Gradle projects, use the following configuration
``` 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
...
}
```
### Use Cases
Inject `IObjectMapper` in spring, call `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 {
}
}
```
## Wiki
* `@Mapping` Annotation attributes
`@AutoMapField` inherits from the annotation of `@Mapping`, all the attributes of `@Mapping` can be used in `@AutoMapField`