xiyou 发表于 2018-10-27 09:44:50

Spring-MongoDB与LBS

package com.digitalchina.lbs.mongo.impl;  
import java.util.List;
  
import org.apache.log4j.Logger;
  
import org.springframework.beans.factory.annotation.Autowired;
  
import org.springframework.data.mongodb.core.CollectionOptions;
  
import org.springframework.data.mongodb.core.DefaultIndexOperations;
  
import org.springframework.data.mongodb.core.MongoTemplate;
  
import org.springframework.data.mongodb.core.geo.Box;
  
import org.springframework.data.mongodb.core.geo.Circle;
  
import org.springframework.data.mongodb.core.geo.Point;
  
import org.springframework.data.mongodb.core.geo.Polygon;
  
import org.springframework.data.mongodb.core.index.GeospatialIndex;
  
import org.springframework.data.mongodb.core.index.Index;
  
import org.springframework.data.mongodb.core.index.Index.Duplicates;
  
import org.springframework.data.mongodb.core.query.Criteria;
  
import org.springframework.data.mongodb.core.query.Order;
  
import org.springframework.data.mongodb.core.query.Query;
  
import org.springframework.data.mongodb.core.query.Update;
  
import org.springframework.stereotype.Component;
  
import com.digitalchina.lbs.mongo.api.MongoLBSAPI;
  
import com.digitalchina.lbs.serve.util.Distance;
  
import com.digitalchina.lbs.serve.util.InformationVo;
  
import com.mongodb.DB;
  
import com.mongodb.DBCollection;
  
import edu.emory.mathcs.backport.java.util.Collections;
  
@Component

  
public>  private final static Logger log=Logger.getLogger(MongoLBSImpl.class);
  @Autowired
  private MongoTemplate mongoTemplate;
  /**
  *方法:分页查找数据库中以对角线为轴的矩形内的数据
  *
  * @param lowerLeft表示左下顶点的经纬坐标,upperRight表示右上顶点的经纬坐标,classzz表示类的全称名,radius表示半径,key表示关键字
  *            ,startNum表示起始数,limit表示个数(必填)
  *@return List
  * **/
  @SuppressWarnings("unchecked")

  public List boxByPage(String collectionName,Query query ,double[] lowerLeft,double[] upperRight, >  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
  return null;
  }
  try {
  if (query==null) {
  log.warn("传给MongoLBSImpl类中的boxByPage()的query为null。");
  query = new Query();
  }
  // 多边形
  Box box = new Box(lowerLeft, upperRight);
  query.addCriteria(Criteria.where("infoMap.loc").within(box));
  /*if(key!=null){
  query.addCriteria(Criteria.where("infoMap.sname").regex("^.*" + key + ".*$"));
  }*/
  query.skip(startNum);
  query.limit(limit);

  return mongoTemplate.find(query,>  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的boxByPage()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:分页查找数据库中以对角线为轴的矩形内的数据
  *
  * @param lowerLeft表示左下顶点的经纬,upperRight表示右上顶点的经纬,classzz表示类的全称名,radius表示半径,key表示关键字
  *            ,startNum表示起始数,limit表示个数(必填)
  *@return List
  * **/
  @SuppressWarnings("unchecked")

  public List boxByPage(String collectionName,Query query ,Point lowerLeft, Point upperRight,>  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
  return null;
  }
  try {
  if (query==null) {
  log.warn("传给MongoLBSImpl类中的boxByPage()的query为null。");
  query = new Query();
  }
  //矩形
  Box box = new Box(lowerLeft, upperRight);
  query.addCriteria(Criteria.where("infoMap.loc").within(box));
  /*if(key!=null){
  query.addCriteria(Criteria.where("infoMap.sname").regex("^.*" + key + ".*$"));
  }*/
  query.skip(startNum);
  query.limit(limit);

  return mongoTemplate.find(query,>  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的boxByPage()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  // 方圆,分页,模糊
  /**
  *方法:分页查找数据库中以某点为圆心且指定半径内的数据
  *
  * @param centerX表示圆心的纬度,centerY表示圆心的经度,classzz表示类的全称名,radius表示半径,key表示关键字
  *            ,startNum表示起始数,limit表示个数(必填)
  *@return List
  * **/
  @SuppressWarnings("unchecked")

  public List circleByPage(String collectionName,Query query ,double centerX, double centerY,double radius,Class>  System.out.println("============MongoLBSImpl=======circleCriteria===========");
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
  return null;
  }
  try {
  if (query==null) {
  log.warn("传给MongoLBSImpl类中的circleByPage()的query为null。");
  query = new Query();
  }
  // 圆
  Circle cri = new Circle(centerX, centerY, Distance.getChange(radius));
  query.addCriteria(Criteria.where("infoMap.loc").within(cri));
  query.skip(startNum);
  query.limit(limit);
  //System.out.println("-=-==---=-=--=-=-=-=-========-====:"+query);

  //System.out.println(mongoTemplate.find(query,>
  List list=mongoTemplate.find(query,>  if (list!=null) {
  log.info("传给MongoLBSImpl类中的circleByPage()计算距离。");
  for (InformationVo informationVo : list) {
  informationVo.setDistance(Distance.GetDistance(centerX, centerY, ((List)(informationVo.getInfoMap().get("loc"))).get(0), ((List)(informationVo.getInfoMap().get("loc"))).get(1)));
  }
  Collections.sort(list);
  }
  return list;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的circleByPage()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:分页查找数据库中以某点为圆心且指定半径内的数据
  *
  * @param centerX表示圆心的纬度,centerY表示圆心的经度,classzz表示类的全称名,radius表示半径,key表示关键字
  *            ,startNum表示起始数,limit表示个数(必填)
  *@return List
  * **/
  @SuppressWarnings("unchecked")

  public List circleByPage(String collectionName,Query query ,Point center,double radius,Class>  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
  return null;
  }
  try {
  if (query==null) {
  log.warn("传给MongoLBSImpl类中的circleByPage()的query为null。");
  query = new Query();
  }
  // 圆
  Circle cri = new Circle(center, Distance.getChange(radius));
  query.addCriteria(Criteria.where("infoMap.loc").within(cri));
  query.skip(startNum);
  query.limit(limit);

  List list=mongoTemplate.find(query,>  if (list!=null) {
  log.info("传给MongoLBSImpl类中的circleByPage()计算距离。");
  for (InformationVo informationVo : list) {
  informationVo.setDistance(Distance.GetDistance(center.getX(), center.getY(), ((List)(informationVo.getInfoMap().get("loc"))).get(0), ((List)(informationVo.getInfoMap().get("loc"))).get(1)));
  }
  }
  Collections.sort(list);
  return list;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的circleByPage()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  // 条件查找,并分页
  /**
  *方法:根据条件分页查找数据库中指定的数据
  *
  * @param startNum表示起始数,limit表示个数,criteria表示指定的条件
  *@return List
  * **/
  public List conditionByPage(String collectionName,int startNum, int limit,Criteria... criteria) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的conditionByPage()中collectionName为null");
  return null;
  }
  try {
  // 分页
  Query query = new Query();
  query.skip(startNum);
  query.limit(limit);
  for (Criteria c : criteria) {
  query.addCriteria(c);
  }
  return mongoTemplate.find(query, InformationVo.class,collectionName);
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的conditionByPage()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:根据条件分页查找数据库中指定的数据
  *
  * @param startNum表示起始数,limit表示个数,query表示指定的条件
  *@return List
  * **/
  public List conditionByPage(String collectionName,int startNum, int limit,Query query) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的boxByPage()中collectionName为null");
  return null;
  }
  try {
  // 分页
  if (query==null) {
  query = new Query();
  }
  query.skip(startNum);
  query.limit(limit);
  /*for (Criteria c : criteria) {
  query.addCriteria(c);
  }*/
  return mongoTemplate.find(query, InformationVo.class,collectionName);
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的conditionByPage()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:建立指定名称的集合
  *

  * @param collectionName表示指定的集合名称;collectionOptions表示建立集合的选项(CollectionOptions(Integer>  *@return boolean
  * **/
  public boolean createCollection(String collectionName,
  CollectionOptions collectionOptions) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的createCollection()中collectionName为null");
  return false;
  }
  try {
  if (collectionOptions!=null) {
  log.info("执行createCollection("+collectionName+","+collectionOptions+");");
  mongoTemplate.createCollection(collectionName, collectionOptions);
  }else{
  log.info("执行createCollection("+collectionName+");");
  mongoTemplate.createCollection(collectionName);
  }
  return true;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的createCollection()出现异常。");
  e.printStackTrace();
  return false;
  }
  }
  //地理索引--未实现
  public boolean createdGeoIndex(String collectionName, String indexName,
  String key, Integer max, Integer min) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的createdGeoIndex()中collectionName为null");
  return false;
  }
  try {
  System.out.println("===========createdGeoIndex=============");
  DefaultIndexOperations indexopera=new DefaultIndexOperations(mongoTemplate, collectionName);
  GeospatialIndex geoIndex=new GeospatialIndex(key) ;
  if(indexName!=null){
  geoIndex.named(indexName);
  }
  if ( max!= null) {
  geoIndex.withMax(max);
  }else {
  geoIndex.withMax(180);
  }
  if ( min!= null) {
  geoIndex.withMax(min);
  }else {
  geoIndex.withMax(-180);
  }
  indexopera.ensureIndex(geoIndex);
  return true;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的createdGeoIndex()出现异常。");
  e.printStackTrace();
  return false;
  }
  }
  /**
  *方法:为指定的集合指定的字段建立索引
  *
  * @param collectionName表示指定的集合;indexName表示指定索引的名称;key表示需要索引的字段;
  * duplicates表示是否为唯一索引,可以置为null默认为Index.Duplicates.RETAIN (保留),另一个是Index.Duplicates.DROP(删除);order表示升序还是降序,默认升序
  *@return boolean
  * **/
  public boolean createdIndex(String collectionName, String indexName,
  String key, Duplicates duplicates, Order order) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的createdIndex()中collectionName为null");
  return false;
  }
  try {
  DefaultIndexOperations indexopera=new DefaultIndexOperations(mongoTemplate, collectionName);
  Index index=new Index(key, order) ;
  index.named(indexName);
  if (duplicates!=null) {
  index.unique(duplicates);
  }
  indexopera.ensureIndex(index);
  return true;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的createdIndex()出现异常。");
  e.printStackTrace();
  return false;
  }
  }
  /**
  *方法:删除指定集合中所有的索引
  *
  * @param collectionName表示指定的集合
  *@return boolean
  * **/
  public boolean dropAllIndexeByALL(String collectionName) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的dropAllIndexeByALL()中collectionName为null");
  return false;
  }
  try {
  DefaultIndexOperations index=new DefaultIndexOperations(mongoTemplate, collectionName);
  index.dropAllIndexes();
  return true;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的dropAllIndexeByALL()出现异常。");
  e.printStackTrace();
  return false;
  }
  }
  /**
  *方法:建立指定名称的集合
  *

  * @param collectionName表示指定的集合名称;collectionOptions表示建立集合的选项(CollectionOptions(Integer>  *@return boolean
  * **/
  public boolean dropCollection(String collectionName) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的dropCollection()中collectionName为null");
  return false;
  }
  try {
  mongoTemplate.dropCollection(collectionName);
  return true;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的dropCollection()出现异常。");
  e.printStackTrace();
  return false;
  }
  }
  /**
  *方法:删除指定的索引
  *
  * @param collectionName表示指定的集合;indexName表示指定索引的名称;
  *@return boolean
  * **/
  public boolean dropIndex(String collectionName, String indexName) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的dropIndex()中collectionName为null");
  return false;
  }
  try {
  DefaultIndexOperations index=new DefaultIndexOperations(mongoTemplate, collectionName);
  index.dropIndex(indexName);
  return true;
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的dropIndex()出现异常。");
  e.printStackTrace();
  return false;
  }
  }
  /**
  *方法:判断当前数据库指定名称的集合是否存在
  *
  * @param 无
  *@return DBCollection
  * **/
  publicboolean collectionExists(String collectionName){
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的collectionExists()中collectionName为null");
  return false;
  }
  try {
  return mongoTemplate.collectionExists(collectionName);
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的collectionExists()出现异常。");
  e.printStackTrace();
  return false;
  }
  }
  /**
  *方法:查找数据库中所有的数据
  *

  * @param>  *            (必填)
  *@return List
  * **/
  @SuppressWarnings("unchecked")

  public List findAll(String collectionName,Class>  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的findAll()中collectionName为null");
  return null;
  }
  try {

  return mongoTemplate.find(new Query(),>
  //return mongoTemplate.findAll(classzz,>  // Query(),InformationVo.class);
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的findAll()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:分页查找数据库中所有的数据
  *

  * @param>  *            ,startNum表示起始数,limit表示个数(必填)
  *@return List
  * **/
  @SuppressWarnings("unchecked")

  public List findAllByPage(String collectionName,Class>  int limit) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的findAllByPage()中collectionName为null");
  return null;
  }
  try {
  // 分页
  Query query = new Query();
  query.skip(startNum);
  query.limit(limit);

  return mongoTemplate.find(query,>  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的findAllByPage()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:根据唯一条件精确查找指定的数据
  *
  * @param criteria表示指定的条件,criteria表示指定的条件
  *@return List
  * **/
  @SuppressWarnings("unchecked")

  public InformationVo findOne(String collectionName,Criteria criteria,>  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的findOne()中collectionName为null");
  return null;
  }
  try {
  // 分页
  Query query = new Query();
  query.addCriteria(criteria);
  query.limit(1);

  List list=mongoTemplate.find(query,>  if (list.size()>0) {
  return list.get(0);
  }else {
  return null;
  }
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的findOne()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:获取当前数据库指定名称的集合
  *
  * @param 无
  *@return DBCollection
  * **/
  public DBCollection getCollection(String collectionName) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的getCollection()中collectionName为null");
  return null;
  }
  try {
  return mongoTemplate.getCollection(collectionName);
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的getCollection()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法:获取符合指定条件的文档数
  *
  * @param query表示查询条件;collectionName表示集合名
  *@return long(若返回-1.表示错误)
  * **/
  public long getCount(Query query, String collectionName) {
  if (collectionName==null) {
  log.error("传给MongoLBSImpl类中的getCount()中collectionName为null");
  return -1;
  }
  try {
  return mongoTemplate.count(query, collectionName);
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的getCount()出现异常。");
  e.printStackTrace();
  return -1;
  }
  }
  /**
  *方法:获取当前数据库
  *
  * @param 无
  *@return DB
  * **/
  public DB getDb() {
  try {
  return mongoTemplate.getDb();
  } catch (Exception e) {
  log.warn("传给MongoLBSImpl类中的getDb()出现异常。");
  e.printStackTrace();
  return null;
  }
  }
  /**
  * 方法:向数据库批量插入对象
  *
  * @param list表示对象集合
  *            ,classzz表示类的全称名(必填)
  * @return boolean
  * **/
  //@SuppressWarnings("unchecked")
  public boolean insertList(List
页: [1]
查看完整版本: Spring-MongoDB与LBS