Mybatis根据经度、纬度查询距离最近一个位置(Mysql )

<!--查询距离最近一个位置的接口-->
<select id="getNearPosition" parameterType="com.king.dto.PositionNearQueryDTO"
        resultMap="PositionNearResDTO">
    SELECT
        ACOS(
        SIN((CAST(#{latitude} AS DECIMAL(5,2)) * 3.1415) / 180 ) *
        SIN((CAST(t.latitude AS DECIMAL(5,2)) * 3.1415) / 180 ) +
        COS((CAST(#{latitude} as decimal(5,2)) * 3.1415) / 180 ) *
        COS((CAST(t.latitude AS DECIMAL(5,2)) * 3.1415) / 180 ) *
        COS((CAST(#{longitude} AS DECIMAL(5,2)) * 3.1415) / 180 - (CAST(t.longitude AS DECIMAL(4,2)) * 3.1415) / 180 )
        ) * 6380 AS distance,
        t1.position_logo,
        t1.position_name,
        t1.position_code,
        t1.province_code,
        t1.city_code,
        t1.district_code,
        t1.detail_address, 
        t.longitude,
        t.latitude,
        IF(t.`position` is null, '', t.`position`) as `position`,
        IF(t.`point_offset` is null, '', t.`point_offset`) as `point_offset`,
        t.operator
    FROM
        tb_position_config t
    INNER JOIN tb_position t1 on t1.position_code = t.position_code
    WHERE
        t1.deleted = 0
        AND t.latitude is not null AND t.latitude != ''
        AND t.longitude is not null AND t.longitude != ''
    ORDER BY CAST(distance AS decimal(15,2)) ASC
    LIMIT 0,1
</select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给您提供一个例子: 首先在pom.xml中添加相关依赖: ```xml <dependencies> <!-- Spring Boot MyBatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!-- MySQL 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies> ``` 然后在application.properties中配置数据源和MyBatis: ```properties # 数据库连接配置 spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # MyBatis 配置 mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.domain ``` 其中,"mydatabase"是你所操作的数据库名称,"root"是数据库用户名,"123456"是该用户名对应的密码。 接下来,定义一个实体类User: ```java package com.example.domain; import lombok.Data; @Data public class User { private Long id; private String name; private Integer age; } ``` 然后,定义一个Mapper接口UserMapper: ```java package com.example.mapper; import com.example.domain.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; @Mapper public interface UserMapper { List<User> findUsersByAge(@Param("age") Integer age); } ``` 接着,创建一个mapper/UserMapper.xml文件,在其中添加findUsersByAge查询语句: ```xml <mapper namespace="com.example.mapper.UserMapper"> <select id="findUsersByAge" resultType="com.example.domain.User"> SELECT * FROM user WHERE age = #{age} </select> </mapper> ``` 最后,在Service层调用这个Mapper: ```java package com.example.service.impl; import com.example.domain.User; import com.example.mapper.UserMapper; import com.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> findUsersByAge(Integer age) { return userMapper.findUsersByAge(age); } } ``` 完成以上步骤后,即可以通过访问接口来获取符合条件的用户数据了。比如,如果想查找年龄为18的用户,可以访问http://localhost:8080/users/18。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值