반응형
@Mapper(componentModel = "spring")
public interface TestMapper extends EntityMapper<TestDTO, Test> {
TestMapper MAPPER = Mappers.getMapper(TestMapper.class);
default Long map(LocalDateTime localDateTime) {
return localDateTime != null ? localDateTime.toInstant(ZoneOffset.of("+09:00")).toEpochMilli() : null;
}
@Override
@Mappings({
@Mapping(source = "insertDt", target = "insertMilli", qualifiedByName = "toLong"),
@Mapping(source = "updateDt", target = "updateMilli", qualifiedByName = "toLong")
})
TestDTO toDto(Test test);
@Named("toLong")
default Long toLong(LocalDateTime localDateTime) {
return map(localDateTime);
}
}
mapStruct에서 qualifiedByName을 이용하여 아래처럼 LocalDateTime을 epoch Milli 로 변경할수 있습니다
@Mapper(componentModel = "spring")
public interface TestMapper extends EntityMapper<TestDTO, Test> {
TestMapper MAPPER = Mappers.getMapper(TestMapper.class);
default Long map(LocalDateTime localDateTime) {
return localDateTime != null ? localDateTime.toInstant(ZoneOffset.of("+09:00")).toEpochMilli() : null;
}
@Override
@Mapping(source = "insertDt", target = "insertMilli", qualifiedByName = "toLong")
TestDTO toDto(Test test);
@Named("toLong")
default Long toLong(LocalDateTime localDateTime) {
return map(localDateTime);
}
}
만약에 여러필드를 변경하고 싶다면 아래 코드처럼 @Mappings를 이용하시면 됩니다
반응형
'Spring' 카테고리의 다른 글
[Spring JPA] 외래키를 복합키에 포함하기 (0) | 2023.03.31 |
---|---|
Gradle (0) | 2023.03.29 |
[Spring] 조회메서드에 @Transactional(readOnly=true)를 사용해야하는 이유 (0) | 2023.03.23 |
[JAVA] JAVA에서 윈도우, 우분투 NTP 서버 설정 변경하는 방법 (0) | 2023.03.22 |
[Spring] Springdoc Swagger 그룹, 정렬, 카테고리 접기 (0) | 2023.03.21 |
댓글