MapStruct
public class Car {
private String make;
private int numberOfSeats;
private CarType type;
//constructor, getters, setters etc.
}
|
public class CarDto {
private String make;
private int seatCount;
private String type;
//constructor, getters, setters etc.
}
|
| |
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
@Mapping(source = "numberOfSeats", target = "seatCount")
CarDto toDto(Car car);
}
| |
| |
@Test
public void shouldMapCarToDto() {
Car car = new Car("Morris", 5, CarType.SEDAN);
CarDto dto = CarMapper.INSTANCE.toDto(car);
//then
assertThat(dto).isNotNull();
assertThat(dto.getMake()).isEqualTo("Morris");
assertThat(dto.getSeatCount()).isEqualTo(5);
assertThat(dto.getType()).isEqualTo("SEDAN");
}
|
Lombok Example
@Data
public class LeadDto implements Serializable {
private static final long serialVersionUID = -1L;
private String id;
private String code;
private String fullName;
private String mobileNo;
private String email;
}
|
@Data
public class LeadVyo implements Serializable {
private static final long serialVersionUID = -2L;
private String id;
private String createdDate;
private String createdTime;
private String code;
private String fullName;
private String mobileNo;
private String email;
}
|
| |
@Data
@Entity
@EntityListeners(AuditTrailListener.class)
public class Lead implements AuditTrail {
private static final long serialVersionUID = -3L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sqn_lead")
@GenericGenerator(name = "sqn_lead", strategy = "org.chorke.academia.utility.Base36Style", parameters = {
@Parameter(name = Base36Style.INITIAL_VALUE, value = Base36Style.DIGIT_6_MIN),
@Parameter(name = Base36Style.SEQUENCE_NAME, value = "sqn_lead"),
@Parameter(name = Base36Style.ALLOCATION_SIZE, value = "1"),
})
private String id;
@Version
private Long versionNo = 0L;
private LocalDateTime createdOn;
private String createdBy;
private LocalDateTime updatedOn;
private String updatedBy;
private LocalDateTime deletedOn;
private String code;
private String fullName;
private String mobileNo;
private String email;
}
| |
| |
@Mapper
public interface LeadMapper {
LeadMapper NEW = Mappers.getMapper(LeadMapper.class);
LeadDto toDto(Lead e);
default List<LeadDto> toDto(Collection<Lead> collection) {
List<LeadDto> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(collection)) collection.forEach(e -> list.add(toDto(e)));
return list;
}
Lead toEnt(LeadDto d);
default List<Lead> toEnt(Collection<LeadDto> collection) {
List<Lead> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(collection)) collection.forEach(d -> list.add(toEnt(d)));
return list;
}
@Mappings({
@Mapping(target = "createdDate", source = "e.createdOn", dateFormat = "dd/MM/yyyy"),
@Mapping(target = "createdTime", source = "e.createdOn", dateFormat = "HH:mm")})
LeadVyo toVyo(Lead e);
default List<LeadVyo> toVyo(Collection<Lead> collection) {
List<LeadVyo> list = new ArrayList<>();
if (!CollectionUtils.isEmpty(collection)) collection.forEach(e -> list.add(toVyo(e)));
return list;
}
LeadVyo toVyo(LeadDto d);
}
|
Maven Plugins
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>