Mapping Entity/DTO objects with Mapstruct
ActivityType and Activity mappings are achieved with Mapstruct:
1. Pom.xml
.....
<<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Final</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
2. Entity class
public class DayActivity implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String todoKg;
private String todoSets;
private String todoReps;
private String loggedReps;
private LocalDate logDate;
private ActivityType activitytype;
private Activity activity;
public DayActivity() {}
public DayActivity(String amountFactor, LocalDate logDate, String loggedReps, String todoKg, String todoReps, String todoSets, Activity activity, ActivityType activityType) {
this.logDate = logDate;
this.loggedReps = loggedReps;
this.todoKg = todoKg;
this.todoSets = todoSets;
this.todoReps = todoReps;
this.activity = activity;
this.activitytype = activityType;
}
// getters, setters...
}
3. DTO class
(ignoreUnknown = true)
public class DayActivityDTO implements Serializable {
private String id;
private String activityId;
private String activityTypeId;
private String todoKg;
private String todoSets;
private String todoReps;
private String loggedReps;
private LocalDate logDate;
private String activityTypeTitle;
private String activitySubType;
// getters, setters...
}
4. Mapper class
@Mapper(unmappedTargetPolicy = org.mapstruct.ReportingPolicy.IGNORE, componentModel = "spring")
public abstract class DayActivityMapper {
@Mappings({
@Mapping(source = "activitytype", target = "activityTypeId", qualifiedByName = "actTypeToId"),
@Mapping(source = "activitytype", target = "activityTypeTitle", qualifiedByName = "actTypeToTitle"),
@Mapping(source = "activity", target = "activitySubType", qualifiedByName = "actToSubType"),
@Mapping(source = "activity", target = "activityId", qualifiedByName = "actToActId")
})
public abstract DayActivityDTO toDto(DayActivity entity);
@Named("actTypeToId")
static String actTypeToId(ActivityType activityType) {
return Long.toString(activityType.getId());
}
@Named("actTypeToTitle")
static String actTypeToTitleMethod(ActivityType activityType) {
return activityType.getTitle();
}
@Named("actToSubType")
static String actToSubTypeMethod(Activity activity) {
return activity.getActivitySubType();
}
@Named("actToActId")
static String actToActIdMethod(Activity activity) {
return Long.toString(activity.getId());
}
public List<DayActivityDTO> toDtos(List<DayActivity> entities) {
List<DayActivityDTO> result = new ArrayList<>();
for (DayActivity d : entities) {
result.add(this.toDto(d));
}
return result;
}
}
5. Usage in Service layer
In your ServiceImpl:
// Declare Mapper class
private final DayActivityMapper dayActivityMapper;
// Inject dependency
public YourServiceImpl(DayActivityMapper dayActivityMapper) {
this.dayActivityMapper = dayActivityMapper;
}
// Use in method:
// Get entities list
List<DayActivity> dayActivities = // get your entities..
// Map to DTOs
List<DayActivityDTO> dayActivityDTOs = dayActivityMapper.toDtos(dayActivities);
6. Generate mapper implementations in target folder
mvn clean install
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
What may be missing, or could get better?