使用 mapstruct 将点映射到经纬度

Mapping a Point to latitude and longitude with mapstruct

我有这个型号:

import org.locationtech.jts.geom.Point;

@Entity
@Table(name = "t_hotel")
public class THotel {
    private String name;
    private Point coordinates;
}

我想要这个 DTO

public class HotelDTO {
    private String nome;
    private Double latitude;
    private Double longitude;
}

如何用mapstruct映射两者?

我没有深入org.locationtech.jts.geom.Pointclass,但希望对您有所帮助。假设 Point.java 看起来像:

public class Point {
    private Double latitude;
    private Double longitude;

    // constructors and getters/setters present here
}

在这种情况下,映射器将如下所示:

@Mapper(componentModel = "spring")
public interface THotelMapper {

    @Mapping(source = "nome", target = "name")
    @Mapping(expression = "java(toPoint(dto))", target = "coordinates")
    THotel mapToEntity(HotelDTO dto);

    @Mapping(source = "name", target = "nome")
    @Mapping(expression = "java(tHotel.getCoordinates().getLatitude())", target = "latitude")
    @Mapping(expression = "java(tHotel.getCoordinates().getLongitude())", target = "longitude")
    HotelDTO mapToDto(THotel tHotel);

    default Point toPoint(HotelDTO dto) {
        // here you need to set latitude and longitude to Point some how
        return new Point(dto.getLatitude(),dto.getLongitude());
    }
}

您可以让 mapstruct 将附加参数传递给以下映射操作。在这种情况下,我将附加字段标记为上下文 (org.mapstruct.Context) 字段,这样它们就不会自动用于映射到 THotel:

@Mapper(componentModel = "spring")
interface THotelMapper {

    @Mapping(source = "nome", target = "name")
    @Mapping(source = ".", target = "coordinates")
    THotel mapToEntity(HotelDTO dto, @Context PrecisionModel precisionModel, @Context int SRID);

    @Mapping(source = "name", target = "nome")
    @Mapping(source = "coordinates.y", target = "latitude")
    @Mapping(source = "coordinates.x", target = "longitude")
    HotelDTO mapToDto(THotel tHotel);

    default Point toPoint(HotelDTO dto, @Context PrecisionModel precisionModel, @Context int SRID) {
        return new Point(new Coordinate(dto.getLongitude(), dto.getLatitude()), precisionModel, SRID);
    }
}

或者您可以 然后使用默认值:

@Mapper(componentModel = "spring")
interface THotelMapper {

    @Mapping(source = "nome", target = "name")
    @Mapping(source = ".", target = "coordinates")
    THotel mapToEntity(HotelDTO dto);

    @Mapping(source = "name", target = "nome")
    @Mapping(source = "coordinates.y", target = "latitude")
    @Mapping(source = "coordinates.x", target = "longitude")
    HotelDTO mapToDto(THotel tHotel);

    default Point toPoint(HotelDTO dto) {
        return new Point( new Coordinate( dto.getLongitude(), dto.getLatitude() ), new PrecisionModel(), 4326 );
    }
}

我还注意到 Point 构造函数被标记为已弃用,所以这是一个没有该构造函数的版本:

@Mapper(componentModel = "spring")
interface THotelMapper {

    @Mapping(source = "nome", target = "name")
    @Mapping(source = ".", target = "coordinates")
    THotel mapToEntity(HotelDTO dto, @Context GeometryFactory geomFactory);

    @Mapping(source = "name", target = "nome")
    @Mapping(source = "coordinates.y", target = "latitude")
    @Mapping(source = "coordinates.x", target = "longitude")
    HotelDTO mapToDto(THotel tHotel);

    default Point toPoint(HotelDTO dto, @Context GeometryFactory geomFactory) {
        return geomFactory.createPoint( new Coordinate( dto.getLongitude(), dto.getLatitude() ) );
    }
}

ps。我希望我没有得到 the longitude/latitude to x/y mixed up, but you can read about that here.