我有一个完全运行的服务器,提供随机文章,保存在 MySQL 数据库中,我希望能够在多边形内查找文章。
我发现MySQL已经支持Polygon
和Point
,所以我只使用了ST_CONTAINS
,它也受支持。由于文章确实有嵌入的纬度和经度,我想我可以创建一个点并尝试找出它是否包含在多边形中。
import org.springframework.data.geo.Polygon;
// ...I pass the JPA declaration...
@Query("SELECT an " +
"FROM Announce as an " +
"WHERE ST_CONTAINS(?1, Point(an.latLng.longitude, an.latLng.latitude))")
List<Announce> findAllInPolygon(Polygon polygonPoints);
我没有看到错误,但在运行时,我收到 fatal error :
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected AST node: ( near line 1, column 80 [SELECT an FROM com.desoftmotion.findnow.domain.Announce
as an WHERE ST_CONTAINS(POLYGON(?1), Point(an.latLng.longitude, an.latLng.latitude))]
我不明白,因为我认为 ST_CONTAINS
是已知的。
最佳答案
经过一番研究,我发现 ST_CONTAINS
应该与 true
进行比较。类似的东西
@Query("SELECT an "
"FROM Announce as an " +
"WHERE true = ST_CONTAINS(?1, Point(an.latLng.longitude, an.latLng.latitude))")
它对我有用,尽管我没有任何结果。
此外,Point
即使MySQL可以理解,但JPA也不能很好地映射它。所以我像这样连接起来
GeomFromText(CONCAT('POINT(', an.latLng.longitude, ' ', an.latLng.latitude, ')'))
关于mysql - 如何在Spring boot中使用ST_CONTAINS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57235617/