jpa 使用blaze或querydsl实现postgis存储地理信息和查询
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.vividsolutions.jts.geom.Point;
import org.hibernate.spatial.postgis.GeometryType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import java.util.List;
@Repository
public class GeoRepository {
@Autowired
private EntityManager entityManager;
private JPAQueryFactory queryFactory;
@PostConstruct
private void init() {
queryFactory = new JPAQueryFactory(entityManager);
}
public List<PointOfInterest> findNearbyPOIs(Point location, double maxDistance) {
QPointOfInterest poi = QPointOfInterest.pointOfInterest;
NumberExpression<Double> distanceExpr =
JTS.function(GeometryType.GEOMETRY_DISTANCE, Double.class,
poi.location,
JTS.point(location.getX(), location.getY()));
return queryFactory
.selectFrom(poi)
.where(distanceExpr.lt(maxDistance))
.orderBy(distanceExpr.asc())
.fetch();
}
}
这个代码示例展示了如何使用Querydsl和JPA Query Factory来查询PostGIS中的地理信息。我们定义了一个findNearbyPOIs
方法,它接受一个位置点和一个最大距离,然后使用Querydsl的JTS函数计算每个POI与给定位置的距离,并以升序排列结果。最后,我们使用JPA Query Factory执行查询并获取结果列表。
评论已关闭