我先说明一下定定义,拉运地:地图中的一个点包含经度和维度,也是“起点”,运往地:地图中的一个点包含经度和维度,也是“终点”。我的数据库里存放着很多拉运地和运往地的坐标点。现在,我想要一个java语言编写的代码,我可以传入拉运地的偏差范围和运往地的偏差范围,然后从数据库中给我返回符合条件的坐标信息。注意:1. 我用的持久化框架是mybaties plus。2. 我传入的偏差范围是一个单位为"千米"的数。3. 我要的代码类型为工具类
时间: 2023-03-30 12:02:29 浏览: 41
可以回答这个问题。以下是一个示例代码,可以根据传入的拉运地和运往地的偏差范围,从数据库中查询符合条件的坐标信息:
```
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
@Component
public class CoordinateUtils {
@Resource
private BaseMapper<Coordinate> coordinateMapper;
public List<Coordinate> getCoordinates(double startLongitude, double startLatitude, double endLongitude, double endLatitude, double startRange, double endRange) {
QueryWrapper<Coordinate> wrapper = new QueryWrapper<>();
wrapper.between("start_longitude", startLongitude - startRange / 111, startLongitude + startRange / 111)
.between("start_latitude", startLatitude - startRange / 111, startLatitude + startRange / 111)
.between("end_longitude", endLongitude - endRange / 111, endLongitude + endRange / 111)
.between("end_latitude", endLatitude - endRange / 111, endLatitude + endRange / 111);
return coordinateMapper.selectList(wrapper);
}
}
```
在这个工具类中,我们使用了 Mybatis Plus 提供的 BaseMapper 来进行数据库操作。在 getCoordinates 方法中,我们使用 QueryWrapper 来构建查询条件,然后调用 selectList 方法来查询符合条件的坐标信息。其中,经度和纬度的偏差范围是通过将千米转换为经纬度的方式计算得到的。