mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
Updating readme file
This commit is contained in:
parent
4fb2220017
commit
19c9c98728
54
readme.md
54
readme.md
@ -4,7 +4,7 @@ MapStruct is a Java [annotation processor](http://docs.oracle.com/javase/6/docs/
|
|||||||
|
|
||||||
All you have to do is to define a mapper interfaces, annotate it with the `@Mapper` annotation and add the required mapping methods. During compilation, MapStruct will generate an implementation for the mapper interface. This implementation uses plain Java method invocations, i.e. no reflection or similar.
|
All you have to do is to define a mapper interfaces, annotate it with the `@Mapper` annotation and add the required mapping methods. During compilation, MapStruct will generate an implementation for the mapper interface. This implementation uses plain Java method invocations, i.e. no reflection or similar.
|
||||||
|
|
||||||
## Hello World
|
# Hello World
|
||||||
|
|
||||||
The following shows a simple example for using MapStruct. First, let's define an object (e.g. a JPA entity) and an accompanying data transfer object (DTO):
|
The following shows a simple example for using MapStruct. First, let's define an object (e.g. a JPA entity) and an accompanying data transfer object (DTO):
|
||||||
|
|
||||||
@ -64,37 +64,33 @@ public void shouldMapCarToDto() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Sometimes not only the names of two corresponding attributes differ, but also their types. This can be addressed by defining a custom type converter:
|
# Advanced mappings
|
||||||
|
|
||||||
public class IntToStringConverter implements Converter<Integer, String> {
|
## Reverse mappings
|
||||||
|
|
||||||
@Override
|
Often bi-directional mappings are required, e.g. from entity to DTO and from DTO to entity. For this purpose, simply declare a method with the required parameter and return type on the mapping interface which also declares the forward mapping method:
|
||||||
public String from(Integer source) {
|
|
||||||
return source != null ? source.toString() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
```java
|
||||||
public Integer to(String target) {
|
@Mapper
|
||||||
return target != null ? Integer.valueOf( target ) : null;
|
public interface CarMapper {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
To make use of a converter, specify its type within the `@Mapping` annotation:
|
|
||||||
|
|
||||||
@Mapper
|
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
|
||||||
public interface CarMapper {
|
|
||||||
|
|
||||||
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
|
@Mapping(source = "numberOfSeats", target = "seatCount")
|
||||||
|
CarDto carToCarDto(Car car);
|
||||||
|
|
||||||
|
Car carDtoToCar(CarDto carDto); (1)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
@Mappings({
|
1. The `carDtoToCar()` method is the reverse mapping method for `carToCarDto()`. Note that the attribute mappings only have to be specified at one of the two methods and will be applied to the corresponding reverse mapping method as well.
|
||||||
@Mapping(source = "numberOfSeats", target = "seatCount"),
|
|
||||||
@Mapping(source = "yearOfManufacture", target = "manufacturingYear", converter = IntToStringConverter.class)
|
## Mapping referenced objects and collections
|
||||||
})
|
|
||||||
CarDto carToCarDto(Car car);
|
Typically an object has not only primitive attributes but also references other objects. E.g. the `Car` class could contain a reference to a `Driver` object, while the `CarDto` class references a `DriverDto` object.
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
Car carDtoToCar(CarDto carDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
# Using MapStruct
|
# Using MapStruct
|
||||||
|
|
||||||
MapStruct is a Java annotation processor based on [JSR 269](jcp.org/en/jsr/detail?id=269) and as such can be used within command line builds (javac, Ant, Maven etc.) as well as from within your IDE.
|
MapStruct is a Java annotation processor based on [JSR 269](jcp.org/en/jsr/detail?id=269) and as such can be used within command line builds (javac, Ant, Maven etc.) as well as from within your IDE.
|
||||||
@ -107,9 +103,7 @@ For Maven based projects add the following to your POM file in order to use MapS
|
|||||||
<org.mapstruct.version>[current MapStruct version]</org.mapstruct.version>
|
<org.mapstruct.version>[current MapStruct version]</org.mapstruct.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
...
|
||||||
<!-- ... -->
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mapstruct</groupId>
|
<groupId>org.mapstruct</groupId>
|
||||||
@ -117,9 +111,7 @@ For Maven based projects add the following to your POM file in order to use MapS
|
|||||||
<version>${org.mapstruct.version}</version>
|
<version>${org.mapstruct.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
...
|
||||||
<!-- ... -->
|
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user