之前在mybatis中的写法:
/** * @author zengwei * @email zengwei_str@163.com * @date 2020/7/23 */ ROUND(6378.138*2*ASIN(SQRT(POW(SIN((#{lat}*PI()/180-`lat`*PI()/180)/2),2)+COS(#{lat}*PI()/180)*COS(`lat`*PI()/180)*POW(SIN((#{lng}*PI()/180-`lng`*PI()/180)/2),2)))*1000)
现在公司用的是QueryDSL,写法和之前不一样,这里做个记录;
/** * @author zengwei * @email zengwei_str@163.com * @date 2020/7/23 */ public JPAQuery<UserDetails> findAllQuery(UserQueryParam param) { QUserDTO qu = QUserDTO.userDTO; NumberExpression<Double> distance = acos(sin(radians(Expressions.constant(param.getLatitude()))) .multiply(sin(radians(qu.latitude))) .add(cos(radians(Expressions.constant(param.getLatitude()))) .multiply(cos(radians(qu.latitude))) .multiply(cos(radians(Expressions.constant(param.getLongitude())).subtract(radians(qu.longitude)))) )).multiply(6371); JPAQuery jpaQuery = jpaQueryFactory.select(Projections.bean(UserDetails.class, ql.id, ql.username, distance.as("distance"))) .from(qu).orderBy(distance.asc()); }