MapStruct: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 10: | Line 10: | ||
} | } | ||
</source> | </source> | ||
| valign="top" | | | valign="top" | | ||
<source lang="java"> | <source lang="java"> | ||
Line 21: | Line 20: | ||
} | } | ||
</source> | </source> | ||
|- | |||
| valign="top" colspan="2" | | |||
---- | |||
|- | |- | ||
| valign="top" colspan="2" | | | valign="top" colspan="2" | | ||
Line 27: | Line 28: | ||
@Mapper | @Mapper | ||
public interface CarMapper { | public interface CarMapper { | ||
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class ); | CarMapper INSTANCE = Mappers.getMapper(CarMapper.class); | ||
@Mapping(source = "numberOfSeats", target = "seatCount") | @Mapping(source = "numberOfSeats", target = "seatCount") | ||
Line 33: | Line 34: | ||
} | } | ||
</source> | </source> | ||
|- | |||
| valign="top" colspan="2" | | |||
---- | |||
|- | |||
| valign="top" colspan="2" | | |||
<source lang="java"> | <source lang="java"> | ||
@Test | @Test | ||
public void shouldMapCarToDto() { | public void shouldMapCarToDto() { | ||
Car car = new Car("Morris", 5, CarType.SEDAN); | Car car = new Car("Morris", 5, CarType.SEDAN); | ||
CarDto dto = CarMapper.INSTANCE.toDto(car); | CarDto dto = CarMapper.INSTANCE.toDto(car); | ||
Line 47: | Line 52: | ||
} | } | ||
</source> | </source> | ||
|} | |||
==Lombok Example== | |||
{| | |||
| valign="top" | | |||
<source lang="java"> | |||
@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; | |||
} | |||
</source> | |||
| valign="top" | | |||
<source lang="java"> | |||
@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; | |||
} | |||
</source> | |||
|- | |||
| valign="top" colspan="2" | | |||
---- | |||
|- | |||
| valign="top" colspan="2" | | |||
<source lang="java"> | |||
@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; | |||
} | |||
</source> | |||
|- | |||
| valign="top" colspan="2" | | |||
---- | |||
|- | |||
| valign="top" colspan="2" | | |||
<source lang="java"> | |||
@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); | |||
} | |||
</source> | |||
|} | |} | ||
==Maven Plugins== | |||
<syntaxhighlight lang="xml"> | |||
<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> | |||
</syntaxhighlight> | |||
==References== | ==References== | ||
{| | {| | ||
| valign="top" | | | valign="top" | | ||
* [https://www.baeldung.com/java-mapstruct-mapping-collections Mapping Collections with MapStruct] | |||
* [[Convention for Database Tables]] | |||
* [https://plugins.jetbrains.com/plugin/10036-mapstruct-support IntelliJ IDEA MapStruct Support] | |||
* [https://www.baeldung.com/mapstruct Quick Guide to MapStruct] | * [https://www.baeldung.com/mapstruct Quick Guide to MapStruct] | ||
* [https://mapstruct.org/news/2019-12-06-mapstruct-and-quarkus/ Quarkus MapStruct] | |||
* [https://mapstruct.org/ MapStruct] | * [https://mapstruct.org/ MapStruct] | ||
* [[Liquibase]] | |||
* [[Lombok]] | * [[Lombok]] | ||
* [[Locale]] | |||
* [[JPA]] | |||
| valign="top" | | | valign="top" | | ||
* [https://marketplace.eclipse.org/content/mapstruct-eclipse-plugin MapStruct Eclipse Plugin] | |||
* [https://projectlombok.org/setup/eclipse Lombok Plugins for IDE] | |||
* [https://mapstruct.org/documentation/ide-support/ MapStruct IDE Support] | |||
|} | |} |
Latest revision as of 00:43, 30 May 2022
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>