码迷,mamicode.com
首页 > 数据库 > 详细

Spring Boot+MongoDB (地区分页查询)1.导入依赖

时间:2020-04-28 21:38:08      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:计算   dem   static   --   int   pen   ret   on()   修改   

1.导入依赖

        <!--springboot整合mongodb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>
2.配置参数

server:
  port: 8888
spring:
  data:
    mongodb:
      uri: mongodb://test:test@localhost:27017/test
安装请看

3.示例

实体

package com.example.demo.entity;
 
import org.springframework.data.mongodb.core.mapping.Document;
 
import java.io.Serializable;
import java.util.Arrays;
 
/**
 * @Author: LiuYong
 * @Date:2020/04/16 15:30
 * @Description: TODO 描述
 */
@Document(collection = "area")
public class Area implements Serializable {
    private static final long serialVersionUID = 2154563134654545L;
    private int id;
    private String name;
    private double longitude;
    private double latitude;
    private double[] location;
 
    public double[] getLocation() {
        return location;
    }
 
    public void setLocation(double[] location) {
        this.location = location;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public double getLongitude() {
        return longitude;
    }
 
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
 
    public double getLatitude() {
        return latitude;
    }
 
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
 
    @Override
    public String toString() {
        return "Area{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", longitude=" + longitude +
                ", latitude=" + latitude +
                ", location=" + Arrays.toString(location) +
                ‘}‘;
    }
}
接口

package com.example.demo.service;
 
import com.example.demo.entity.Area;
import org.springframework.data.geo.GeoResult;
 
import java.util.List;
 
/**
 * @Author: LiuYong
 * @Date:2020/04/16 15:32
 * @Description: TODO 描述
 */
public interface AreaService {
    /**
     *
     * @param longitude 经度
     * @param latitude 纬度
     * @param dis 距离:千米
     * @return
     */
    List<Area> findNeighPosition(double longitude,double latitude,double dis);
    //新增
    void saveArea(Area area);
 
    //删除
    void removeArea(Integer id);
 
    //修改
    void updateArea(Area area);
 
    //根据编号查询
    Area findById(Integer id);
 
    //姓名模糊查询
    List<Area> fuzzyByName(String name);
 
    //查询所有
    List<Area> findAll();
 
    /**
     *
     * @param page  当前页
     * @param size  每页大小
     * @param longitude  经度
     * @param latitude   纬度
     * @param radius     距离:米
     * @return
     */
    List<Area> find(int page,int size,double longitude, double latitude, double radius);
}
实现类

package com.example.demo.service.impl;
 
import com.example.demo.entity.Area;
import com.example.demo.entity.Student;
import com.example.demo.service.AreaService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.geo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.regex.Pattern;
 
/**
 * @Author: LiuYong
 * @Date:2020/04/16 15:34
 * @Description: TODO 描述
 */
@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private MongoTemplate mongoTemplate;
 
 
    @Override
    public List<Area> findNeighPosition(double longitude, double latitude, double dis) {
        //先计算查询点的经纬度范围
        //地球半径千米
        double r = 6371;
        double dlng =  2*Math.asin(Math.sin(dis/(2*r))/Math.cos(latitude*Math.PI/180));
        //角度转为弧度
        dlng = dlng*180/Math.PI;
        double dlat = dis/r;
        dlat = dlat*180/Math.PI;
        double minlat =latitude-dlat;
        double maxlat = latitude+dlat;
        System.out.println(longitude+"******");
        System.out.println(dlng+"******");
        double minlng = longitude -dlng;
        double maxlng = longitude + dlng;
        System.out.println("minlat:"+minlat);
        System.out.println("maxlat:"+maxlat);
        System.out.println("minlng:"+minlng);
        System.out.println("maxlng:"+maxlng);
 
        Query query = new Query();
        Criteria criteria = Criteria.where("longitude").gte(minlng).lte(maxlng).and("latitude").gte(minlat).lte(maxlat);
        query.addCriteria(criteria);
        return mongoTemplate.find(query, Area.class);
    }
 
    @Override
    public void saveArea(Area area) {
        mongoTemplate.save(area);
    }
 
    @Override
    public void removeArea(Integer id) {
        Query query=new Query(Criteria.where("_id").is(id));
        mongoTemplate.remove(query,Area.class);
    }
 
    @Override
    public void updateArea(Area area) {
        Query query=new Query(Criteria.where("_id").is(area.getId()));
        Update update=new Update();
        update.set("name",area.getName());
        mongoTemplate.updateFirst(query,update,Area.class);
    }
 
    @Override
    public Area findById(Integer id) {
        //可累加条件
        Query query=new Query(Criteria.where("_id").is(id));
        Area area = mongoTemplate.findOne(query, Area.class);
        return area;
    }
 
    @Override
    public List<Area> fuzzyByName(String name) {
        //模糊匹配
        Pattern pattern = Pattern.compile("^.*"+name+".*$", Pattern.CASE_INSENSITIVE);
        Query query = Query.query(Criteria.where("name").regex(pattern));
        List<Area> areas = mongoTemplate.find(query, Area.class);
        return areas;
    }
 
    @Override
    public List<Area> findAll() {
        return mongoTemplate.findAll(Area.class);
    }
 
    /**
     *
     * @param page  当前页
     * @param size  每页大小
     * @param longitude  经度
     * @param latitude   纬度
     * @param radius     距离:米
     * @return
     */
    @Override
    public List<Area> find(int page,int size,double longitude, double latitude, double radius) {
        Point point = new Point(longitude, latitude);
        //平面单位“度”,1米=(1/1000/111)度
        radius=radius/1000/111;
 
        Query query = new Query(
                Criteria.where("location")
                        .near(point)
                        .maxDistance(radius)
        );
        long count = mongoTemplate.count(query, Area.class);
        System.out.println(count);
        Pageable pageable = PageRequest.of(page-1,size);
        query=query.with(pageable);
        List<Area> areas = mongoTemplate.find(query, Area.class);
        return areas;
    }
}
————————————————
原文链接:https://blog.csdn.net/qq_40369944/java/article/details/105597555

https://www.05jl.com/search/%E6%9D%8F%E9%91%AB%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E9%91%AB%E5%A8%B1%E4%B9%90%E6%80%BB%E4%BB%A3__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E9%91%AB%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E9%91%AB%E6%80%BB%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E9%91%AB%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E9%91%AB%E5%A8%B1%E4%B9%90%E6%80%BB%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E9%91%AB%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E5%BD%A9%E5%B9%B3%E5%8F%B0%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E5%BD%A9%E4%BB%A3%E7%90%86_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E5%BD%A9%E6%80%BB%E4%BB%A3__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E5%BD%A9%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E5%BD%A9%E6%80%BB%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E5%BD%A9%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E4%BB%A3%E7%90%86_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%B9%B3%E5%8F%B0%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%A8%B1%E4%B9%90%E4%BB%A3%E7%90%86__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E6%80%BB%E4%BB%A3__Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%A8%B1%E4%B9%90%E6%80%BB%E4%BB%A3_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E6%80%BB%E4%BB%A3%E7%90%86_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3%E7%90%86_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%A8%B1%E4%B9%90%E6%80%BB%E4%BB%A3%E7%90%86_Q%E3%80%901530667%E3%80%91
https://www.05jl.com/search/%E6%9D%8F%E8%80%80%E5%A8%B1%E4%B9%90%E5%B9%B3%E5%8F%B0%E6%80%BB%E4%BB%A3%E7%90%86_Q%E3%80%901530667%E3%80%91

Spring Boot+MongoDB (地区分页查询)1.导入依赖

标签:计算   dem   static   --   int   pen   ret   on()   修改   

原文地址:https://www.cnblogs.com/dasdfdfecvcx/p/12797363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!