Ere*_*ent 5 java postgresql geolocation spring-boot
我有一个商店数据库,在该数据库中,我保存了这些商店的坐标。我想获取半径为10公里的商店列表,但是由于使用的是Postgres数据库,因此我不确定如何编写Postgres查询。
我的数据库:
我正在尝试将查询添加到springboot地理位置微服务中:
仓库代码:
@Repository
public interface SellerGeolocationRepository extends CrudRepository<Seller_Geolocation, Long> {
@Query(value="SELECT * FROM seller_geolocation_ms WHERE
// get coordinates that are in the vicinity
", nativeQuery = true)
public Set<Seller_Geolocation> findAllSellersInRange(double longitude, double latitude);
}
Run Code Online (Sandbox Code Playgroud)
SellerObject:
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "seller_geolocation_ms")
public class Seller_Geolocation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private static final long serialVersionUID = 2L;
private double latitude;
private double longitude;
private String country;
private String city;
private String zipCode;
private String town;
private String address;
private Long sellerId;
}
Run Code Online (Sandbox Code Playgroud)
为此,您需要安装 postgis :
酿造安装postgis
之后,您需要在 postgres 数据库“ seller_geolocation_ms ”上安装 postgis:
1. $ sudo -i -u postgres
2. $ psql
3. postgres=# \c seller_geolocation_ms;
4. seller_geolocation_ms=# CREATE EXTENSION postgis;
5. \q
Run Code Online (Sandbox Code Playgroud)
完成这些步骤后,postgis 应该安装在您的数据库上。那么接下来要做的就是在 Springboot 存储库接口上编写查询,如下所示:
@Repository
public interface SellerGeolocationRepository extends CrudRepository<Seller_Geolocation, Long> {
@Query(value="SELECT * FROM seller_geolocation_ms WHERE ST_DWithin(cast(seller_geolocation_ms.location as geography),ST_SetSRID(ST_Point(?2, ?1),4326), 10000);", nativeQuery = true)
public Set<Seller_Geolocation> findAllSellersInRange(double longitude, double latitude);
}
Run Code Online (Sandbox Code Playgroud)
要更深入地了解 ST_DWithin 如何使用,请点击此链接:此处
| 归档时间: |
|
| 查看次数: |
216 次 |
| 最近记录: |