如何通过 Query dsl NumberExpression 列进行排序
在mariadb中,我创建了计算用户点和地点点之间距离的函数
/* create function */
DELIMITER $$
CREATE
FUNCTION `u_st_distance_sphere`(`user_point` POINT, `place_point` POINT)
RETURNS double
BEGIN
return (6371*acos(cos(radians(ST_X(user_point)))*cos(radians(ST_X(place_point)))*cos(radians(ST_Y(place_point))
-radians(ST_Y(user_point)))+sin(radians(ST_X(user_point)))*sin(radians(ST_X(place_point)))));
END$$
DELIMITER ;
要使用该函数,请在CustomDialect类中注册函数并在.yml中应用
public class MariadbCustomDialect extends MariaDB103Dialect {
public MariadbCustomDialect() {
super();
this.registerFunction(
"u_st_distance_sphere",
new StandardSQLFunction("u_st_distance_sphere", StandardBasicTypes.DOUBLE)
);
}
}
spring:
...
jpa:
...
database-platform: ...global.config.MariadbCustomDialect
我想制作用点计算的距离列并按距离升序排序
//in PlaceCustomRepository
//making filtered JPQLQuery
String pointWKT = String.format("POINT(%s %s)",
placeFilterParam.getLatitude(),
placeFilterParam.getLongitude());
NumberTemplate<Double> expr = Expressions.numberTemplate(
Double.class,
"u_st_distance_sphere( ST_GeomFromText({0}), ST_GeomFromText({1}) )",
pointWKT,
place.point);
JPQLQuery<PlaceResponseTest> query = jpaQueryFactory
.select(
new QPlaceResponseTest(place.id,
expr.as("distance")))
.from(place)
.orderBy();
如何指定距离列??
谢谢
In mariadb, I created the function to calucate distance between user point and place point
/* create function */
DELIMITER $
CREATE
FUNCTION `u_st_distance_sphere`(`user_point` POINT, `place_point` POINT)
RETURNS double
BEGIN
return (6371*acos(cos(radians(ST_X(user_point)))*cos(radians(ST_X(place_point)))*cos(radians(ST_Y(place_point))
-radians(ST_Y(user_point)))+sin(radians(ST_X(user_point)))*sin(radians(ST_X(place_point)))));
END$
DELIMITER ;
To use the function, register function in CustomDialect class and apply in .yml
public class MariadbCustomDialect extends MariaDB103Dialect {
public MariadbCustomDialect() {
super();
this.registerFunction(
"u_st_distance_sphere",
new StandardSQLFunction("u_st_distance_sphere", StandardBasicTypes.DOUBLE)
);
}
}
spring:
...
jpa:
...
database-platform: ...global.config.MariadbCustomDialect
And I want to make distance column that calculated with points and sort by distance ascending order
//in PlaceCustomRepository
//making filtered JPQLQuery
String pointWKT = String.format("POINT(%s %s)",
placeFilterParam.getLatitude(),
placeFilterParam.getLongitude());
NumberTemplate<Double> expr = Expressions.numberTemplate(
Double.class,
"u_st_distance_sphere( ST_GeomFromText({0}), ST_GeomFromText({1}) )",
pointWKT,
place.point);
JPQLQuery<PlaceResponseTest> query = jpaQueryFactory
.select(
new QPlaceResponseTest(place.id,
expr.as("distance")))
.from(place)
.orderBy();
How to specify distance column??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现使用常量表达式可以工作
I found that use a constant expression can work