思路,先将个人信息存放进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;
}
至此查询完毕