美文网首页
jpa保存mysql的point坐标类型

jpa保存mysql的point坐标类型

作者: 爱的旋转体 | 来源:发表于2020-07-23 11:01 被阅读0次

1 yml或properties配置文件

# 在spring.jpa.properties中添加
hibernate.dialect: org.hibernate.spatial.dialect.mysql.MySQL5SpatialDialect

2 pom.xml

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
        </dependency>

3 实体类

import org.geolatte.geom.G2D;
import org.geolatte.geom.Point;
import org.geolatte.geom.crs.CrsRegistry;

    // 前端传过来的是lat和lon两个字段,后端响应的也是这两个字段
    @Column(name = "location",columnDefinition = "Point")
    @JsonIgnore
    @JSONField(serialize = false,deserialize = false)
    private Point location;

    @Transient
    private Double lat;
    
    @Transient
    private Double lon;

    public Point getLocation() {
        if(location == null) {
            if(lat != null && lon != null) {
                location = new Point(new G2D(lon, lat),CrsRegistry.getCoordinateReferenceSystemForEPSG(4326, null));
            }
        }
        return location;
    }

    public void setLocation(Point location) {
        this.location = location;
    }

    public Double getLat() {
        return lat != null ? lat : (location == null ? null : location.getPosition().getCoordinate(1));
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public Double getLon() {
        return lon != null ? lon : (location == null ? null : location.getPosition().getCoordinate(0));
    }

    public void setLon(Double lon) {
        this.lon = lon;
    }

相关文章

网友评论

      本文标题:jpa保存mysql的point坐标类型

      本文链接:https://www.haomeiwen.com/subject/ummclktx.html