Updating readme file

This commit is contained in:
Gunnar Morling 2013-04-07 14:51:11 +03:00
parent 458718e346
commit 4a6b8c5c1c

View File

@ -2,15 +2,17 @@
MapStruct is a Java [annotation processor](http://docs.oracle.com/javase/6/docs/technotes/guides/apt/index.html) for the generation of type-safe bean mapping classes. MapStruct is a Java [annotation processor](http://docs.oracle.com/javase/6/docs/technotes/guides/apt/index.html) for the generation of type-safe bean mapping classes.
All you have to do is to define one more more mapper interfaces, annotate them with the `@Mapper` annotation and add the required mapping methods. During compilation, MapStruct will generate an implementation for each 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.
The following shows an example. First, an object (e.g. a JPA entity) and an accompanying data transfer object (DTO): ## 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):
```java
public class Car { public class Car {
private String make; private String make;
private int numberOfSeats; private int numberOfSeats;
private int yearOfManufacture;
//constructor, getters, setters etc. //constructor, getters, setters etc.
} }
@ -19,37 +21,38 @@ The following shows an example. First, an object (e.g. a JPA entity) and an acco
private String make; private String make;
private int seatCount; private int seatCount;
private int yearOfManufacture;
//constructor, getters, setters etc. //constructor, getters, setters etc.
} }
```
Both types are rather similar, only the seat count attributes have different names. The mapper interface thus looks like this: Both types are rather similar, only the seat count attributes have different names. A mapper interface could thus look like this:
@Mapper ```java
@Mapper (1)
public interface CarMapper { public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); (3)
@Mapping(source = "numberOfSeats", target = "seatCount"), @Mapping(source = "numberOfSeats", target = "seatCount")
CarDto carToCarDto(Car car); CarDto carToCarDto(Car car); (2)
Car carDtoToCar(CarDto carDto);
} }
```
The interface is straight-forward: The interface is straight-forward:
* Annotating it with `@Mapper` let's the MapStruct processor kick in during compilation 1. Annotating it with `@Mapper` let's the MapStruct processor kick in during compilation
* The `INSTANCE` member provides access to the mapper implementation for clients (a service loader based alternative coming soon) 1. The actual mapping method expects the source object as parameter and returns the target object. Its name can be freely chosen. Of course there can be multiple mapping methods in one interface. For attributes with different names in source and target object, the `@Mapping` annotation can be used to configure the names.
* For each mapping direction (entity to DTO and vice versa) there is a conversion method. For those attributes which have differing names and thus can't be mapped automatically, a mapping is configured using the `@Mapping` annotation on one of the methods. 1. An instance of the interface implementation can be retrieved from the `Mappers` class. By convention, the interface declares a member `INSTANCE`, providing access to the mapper implementation for clients
Based on the mapper interface, clients can perform object mappings in a very easy and type-safe manner: Based on the mapper interface, clients can perform object mappings in a very easy and type-safe manner:
```java
@Test @Test
public void shouldMapCarToDto() { public void shouldMapCarToDto() {
//given //given
Car car = new Car( "Morris", 2, 1980 ); Car car = new Car( "Morris", 2 );
//when //when
CarDto carDto = CarMapper.INSTANCE.carToCarDto( car ); CarDto carDto = CarMapper.INSTANCE.carToCarDto( car );
@ -58,8 +61,8 @@ Based on the mapper interface, clients can perform object mappings in a very eas
assertThat( carDto ).isNotNull(); assertThat( carDto ).isNotNull();
assertThat( carDto.getMake() ).isEqualTo( car.getMake() ); assertThat( carDto.getMake() ).isEqualTo( car.getMake() );
assertThat( carDto.getSeatCount() ).isEqualTo( car.getNumberOfSeats() ); assertThat( carDto.getSeatCount() ).isEqualTo( car.getNumberOfSeats() );
assertThat( carDto.getyearOfManufacture() ).isEqualTo( car.getyearOfManufacture() );
} }
```
Sometimes not only the names of two corresponding attributes differ, but also their types. This can be addressed by defining a custom type converter: Sometimes not only the names of two corresponding attributes differ, but also their types. This can be addressed by defining a custom type converter: