MySQL 并不支持自定义投影坐标系,所以如果用 ST_Distance() 来计算中国境内的两点距离,算出来的结果和真实距离的偏差大到基本不可用,所以需要使用空间函数的话,趁早放弃 MySQL 该用 PostGIS 。

Links

  • Hibernate ORM Hibernate Spatial | MVNRepository
  • MySQL Spatial Function Reference

    安装

    添加依赖:
    1. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-spatial -->
    2. <dependency>
    3. <groupId>org.hibernate</groupId>
    4. <artifactId>hibernate-spatial</artifactId>
    5. <version>5.4.10.Final</version>
    6. </dependency>

    配置

    配置我们的断言:
    1. spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect

    使用

    首先我们在模型中添加需要的 Geometry 字段: ```java import org.locationtech.jts.geom.Point;

@Column(columnDefinition = “POINT”) private Point location;

  1. 测试:
  2. ```java
  3. import org.locationtech.jts.geom.Coordinate;
  4. import org.locationtech.jts.geom.GeometryFactory;
  5. import org.locationtech.jts.geom.Point;
  6. @Test
  7. public void addTest() {
  8. Point location = this.geometryFactory.createPoint(new Coordinate(113, 22));
  9. UserDO userDO = new UserDO();
  10. userDO.setEmail("liuzhaowei55@gmial.com");
  11. userDO.setPassword("123456");
  12. userDO.setNickname("Where");
  13. userDO.setAvatar("https://static.moorper.com/avatar.png");
  14. userDO.setLocation(location);
  15. this.userRepository.save(userDO);
  16. }