搜索附近人

思路,先将个人信息存放进MySQL数据库中,存进数据库的同时,需要将这个人的经纬度存放进MongoDB中,MongoDB中可以之村用户的主要信息等等,经纬度是必须要存进MongoDB的,需要将经纬度存放成一个double[],并且将该字段添加索引,然后利用你的经纬度查询MongoDB,就可以实现查询附近人。
存入MySQL就不必写了,直接存放MongoDB
我使用的是MongoDB自带的工具类
1.设计MongoDB实体类
public class InstallPersonMongoEntity {

    private Integer InstallPersonId;

    private String InstallPersonName;

    @GeoSpatialIndexed
    private double[] loc;

    @Override
    public String toString() {
        return "InstallPersonMongoEntity{" +
                "InstallPersonId=" + InstallPersonId +
                ", InstallPersonName='" + InstallPersonName + '\'' +
                ", loc=" + loc +
                '}';
    }

}
2.查询该id是否存在MongoDB中,如果存在,修改经纬度,如果不存在,则新增
    实现类的方法
    public int insertInstallPerson(InstallPersonVO installPersonVO) {//(InstallPersonVO 是个人信息的实体类,不是Mongo的实体类
        //先查询MongoDB有没有安装人员信息的数据
        Query query = new Query(Criteria.where("InstallPersonId").is(installPersonVO.getInstallPersonId()));
        List installPerson = mongoTemplate.find(query,InstallPersonMongoEntity.class);
        int resultCount = 0;
        if(installPerson.size() > 0){
            InstallPersonMongoEntity installPersonMongoEntity = (InstallPersonMongoEntity) installPerson.get(0);
            System.err.println(installPersonMongoEntity.getInstallPersonName());
            //修改mongo人员信息
            resultCount = updatePersonMongo(installPersonVO);
        }else{
            //新增mongo人员信息
            resultCount = addPersonMongo(installPersonVO);
        }
        return resultCount;
    }

public int updatePersonMongo(InstallPersonVO installPersonVO) {
        Query query = new Query(Criteria.where("InstallPersonId").is(installPersonVO.getInstallPersonId()));
        InstallPersonMongoEntity installPersonMongoEntity = new InstallPersonMongoEntity();
        installPersonMongoEntity.setInstallPersonId(installPersonVO.getInstallPersonId());
        installPersonMongoEntity.setInstallPersonName(installPersonVO.getInstallPersonName());
        installPersonMongoEntity.setLoc(new double[]{installPersonVO.getLon(),installPersonVO.getLat()});
        mongoTemplate.upsert(query, new Update().set("loc",installPersonMongoEntity.getLoc()),InstallPersonMongoEntity.class);
        return 1;
    }

public int addPersonMongo(InstallPersonVO installPersonVO) {
        //将人员信息插入进MongoDB中,方便坐标查询
        InstallPersonMongoEntity installPersonMongoEntity = new InstallPersonMongoEntity();
        installPersonMongoEntity.setInstallPersonId(installPersonVO.getInstallPersonId());
        installPersonMongoEntity.setInstallPersonName(installPersonVO.getInstallPersonName());
        installPersonMongoEntity.setLoc(new double[]{installPersonVO.getLon(),installPersonVO.getLat()});
        mongoTemplate.insert(installPersonMongoEntity);
        return 1;
    }
3.使用navicat操作Mongo,并添加索引

4.查询附近人
    实现类
public List<InstallPersonMongoEntity> selectInstallPersonList(Integer installRepairId) {
        
    //相当于MySQL的分页,查询多少人
        int pageNum = 1; 
        int pageSize = 5;
    //将个人信息的经纬度查询出来
        Map map = installRepairOrderMapper.selectLonLat(installRepairId);
        Query query = new Query(
                Criteria.where("loc").near(
                        new GeoJsonPoint(Double.parseDouble(map.get("lon") + ""),Double.parseDouble(map.get("lat") + ""))
                )
                //.maxDistance(10 / 111.12)
        );
        //根据经纬度查询MongoDB
        PageRequest pageable = PageRequest.of(pageNum - 1,pageSize);
        List<InstallPersonMongoEntity> installPersonVOList = mongoTemplate.find(query.with(pageable),InstallPersonMongoEntity.class);
        //installPersonVOList就是查询出来的附近人的个人信息
       return installPersonVOList;
    }
至此查询完毕

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值