|
@@ -13,67 +13,58 @@ import org.springframework.data.mongodb.core.query.NearQuery;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.data.mongodb.core.query.Update;
|
|
import org.springframework.data.mongodb.core.query.Update;
|
|
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
public abstract class MongoDBDao<T> {
|
|
public abstract class MongoDBDao<T> {
|
|
|
- @Resource(name = "newTemplate")
|
|
|
|
|
- private MongoTemplate mongoTemplate;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 子类通过此方法指定使用哪个 MongoTemplate(对应不同数据源)
|
|
|
|
|
+ */
|
|
|
|
|
+ protected abstract MongoTemplate getMongoTemplate();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 反射获取泛型类型
|
|
|
|
|
+ */
|
|
|
|
|
+ protected abstract Class<T> getEntityClass();
|
|
|
|
|
|
|
|
/***
|
|
/***
|
|
|
* 保存一个对象
|
|
* 保存一个对象
|
|
|
- * @param t
|
|
|
|
|
*/
|
|
*/
|
|
|
public T save(T t) {
|
|
public T save(T t) {
|
|
|
- return this.mongoTemplate.save(t);
|
|
|
|
|
|
|
+ return this.getMongoTemplate().save(t);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 批量插入
|
|
* 批量插入
|
|
|
- *
|
|
|
|
|
- * @param t
|
|
|
|
|
*/
|
|
*/
|
|
|
public List<T> insertBatch(List<T> t) {
|
|
public List<T> insertBatch(List<T> t) {
|
|
|
- return (List<T>) mongoTemplate.insert(t, getEntityClass());
|
|
|
|
|
|
|
+ return (List<T>) getMongoTemplate().insert(t, getEntityClass());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 反射获取泛型类型
|
|
|
|
|
- *
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- protected abstract Class<T> getEntityClass();
|
|
|
|
|
-
|
|
|
|
|
/***
|
|
/***
|
|
|
- * 根据id从几何中查询对象
|
|
|
|
|
- * @param id
|
|
|
|
|
- * @return
|
|
|
|
|
|
|
+ * 根据id从集合中查询对象
|
|
|
*/
|
|
*/
|
|
|
public T queryById(String id) {
|
|
public T queryById(String id) {
|
|
|
Query query = new Query(Criteria.where("_id").is(id));
|
|
Query query = new Query(Criteria.where("_id").is(id));
|
|
|
- return this.mongoTemplate.findOne(query, this.getEntityClass());
|
|
|
|
|
|
|
+ return this.getMongoTemplate().findOne(query, this.getEntityClass());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
/***
|
|
|
* 删除对象
|
|
* 删除对象
|
|
|
- * @param t
|
|
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public int delete(T t) {
|
|
public int delete(T t) {
|
|
|
- return (int) this.mongoTemplate.remove(t).getDeletedCount();
|
|
|
|
|
|
|
+ return (int) this.getMongoTemplate().remove(t).getDeletedCount();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据id删除
|
|
* 根据id删除
|
|
|
- *
|
|
|
|
|
- * @param id
|
|
|
|
|
*/
|
|
*/
|
|
|
public void deleteById(String id) {
|
|
public void deleteById(String id) {
|
|
|
Criteria criteria = Criteria.where("_id").is(id);
|
|
Criteria criteria = Criteria.where("_id").is(id);
|
|
|
if (null != criteria) {
|
|
if (null != criteria) {
|
|
|
Query query = new Query(criteria);
|
|
Query query = new Query(criteria);
|
|
|
- T obj = this.mongoTemplate.findOne(query, this.getEntityClass());
|
|
|
|
|
|
|
+ T obj = this.getMongoTemplate().findOne(query, this.getEntityClass());
|
|
|
log.info("-------------->MongoDB deleteById start");
|
|
log.info("-------------->MongoDB deleteById start");
|
|
|
if (obj != null) {
|
|
if (obj != null) {
|
|
|
this.delete(obj);
|
|
this.delete(obj);
|
|
@@ -82,29 +73,21 @@ public abstract class MongoDBDao<T> {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public int delete(Query query) {
|
|
public int delete(Query query) {
|
|
|
- return (int) mongoTemplate.remove(query, getEntityClass()).getDeletedCount();
|
|
|
|
|
|
|
+ return (int) getMongoTemplate().remove(query, getEntityClass()).getDeletedCount();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据条件查询对象
|
|
* 根据条件查询对象
|
|
|
- *
|
|
|
|
|
- * @param query
|
|
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public T queryOne(Query query) {
|
|
public T queryOne(Query query) {
|
|
|
- return mongoTemplate.findOne(query, getEntityClass());
|
|
|
|
|
|
|
+ return getMongoTemplate().findOne(query, getEntityClass());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据mongo条件查询列表
|
|
* 根据mongo条件查询列表
|
|
|
- *
|
|
|
|
|
- * @param query
|
|
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public List<T> queryList(Query query) {
|
|
public List<T> queryList(Query query) {
|
|
|
- return mongoTemplate.find(query, this.getEntityClass());
|
|
|
|
|
|
|
+ return getMongoTemplate().find(query, this.getEntityClass());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -114,7 +97,7 @@ public abstract class MongoDBDao<T> {
|
|
|
* @param update 要更新的数据
|
|
* @param update 要更新的数据
|
|
|
*/
|
|
*/
|
|
|
public long updateFirst(Query query, Update update) {
|
|
public long updateFirst(Query query, Update update) {
|
|
|
- UpdateResult result = mongoTemplate.updateFirst(query, update, getEntityClass());
|
|
|
|
|
|
|
+ UpdateResult result = getMongoTemplate().updateFirst(query, update, getEntityClass());
|
|
|
return result.getModifiedCount();
|
|
return result.getModifiedCount();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -123,65 +106,51 @@ public abstract class MongoDBDao<T> {
|
|
|
*
|
|
*
|
|
|
* @param query 查询条件
|
|
* @param query 查询条件
|
|
|
* @param update 更新
|
|
* @param update 更新
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public long upsert(Query query, Update update) {
|
|
public long upsert(Query query, Update update) {
|
|
|
- UpdateResult result = mongoTemplate.upsert(query, update, getEntityClass());
|
|
|
|
|
-
|
|
|
|
|
|
|
+ UpdateResult result = getMongoTemplate().upsert(query, update, getEntityClass());
|
|
|
return result.getModifiedCount();
|
|
return result.getModifiedCount();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 批量更新
|
|
* 批量更新
|
|
|
- *
|
|
|
|
|
- * @param query
|
|
|
|
|
- * @param update
|
|
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public long batchUpdate(Query query, Update update) {
|
|
public long batchUpdate(Query query, Update update) {
|
|
|
- UpdateResult result = mongoTemplate.updateMulti(query, update, getEntityClass());
|
|
|
|
|
|
|
+ UpdateResult result = getMongoTemplate().updateMulti(query, update, getEntityClass());
|
|
|
return result.getModifiedCount();
|
|
return result.getModifiedCount();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 查询数量
|
|
* 查询数量
|
|
|
- *
|
|
|
|
|
- * @param query
|
|
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public long count(Query query) {
|
|
public long count(Query query) {
|
|
|
- return mongoTemplate.count(query, this.getEntityClass());
|
|
|
|
|
|
|
+ return getMongoTemplate().count(query, this.getEntityClass());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 批量删除
|
|
* 批量删除
|
|
|
- *
|
|
|
|
|
- * @param query
|
|
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public long deleteBatch(Query query) {
|
|
public long deleteBatch(Query query) {
|
|
|
- DeleteResult result = mongoTemplate.remove(query, this.getEntityClass());
|
|
|
|
|
|
|
+ DeleteResult result = getMongoTemplate().remove(query, this.getEntityClass());
|
|
|
return result.getDeletedCount();
|
|
return result.getDeletedCount();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 管道查询
|
|
* 管道查询
|
|
|
- * @param agg
|
|
|
|
|
- * @return
|
|
|
|
|
*/
|
|
*/
|
|
|
public AggregationResults<JSONObject> aggregate(Aggregation agg) {
|
|
public AggregationResults<JSONObject> aggregate(Aggregation agg) {
|
|
|
- return mongoTemplate.aggregate(agg, getEntityClass(), JSONObject.class);
|
|
|
|
|
|
|
+ return getMongoTemplate().aggregate(agg, getEntityClass(), JSONObject.class);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 管道查询
|
|
* 管道查询
|
|
|
|
|
+ *
|
|
|
* @param aggregation 条件参数
|
|
* @param aggregation 条件参数
|
|
|
* @param resultClass 返回类对象
|
|
* @param resultClass 返回类对象
|
|
|
- * @param <U> 返回类型
|
|
|
|
|
- * @return
|
|
|
|
|
|
|
+ * @param <U> 返回类型
|
|
|
*/
|
|
*/
|
|
|
- public <U> List<U> aggregate(Aggregation aggregation,Class<U> resultClass){
|
|
|
|
|
- AggregationResults<U> results = mongoTemplate.aggregate(aggregation, this.getEntityClass(), resultClass);
|
|
|
|
|
|
|
+ public <U> List<U> aggregate(Aggregation aggregation, Class<U> resultClass) {
|
|
|
|
|
+ AggregationResults<U> results = getMongoTemplate().aggregate(aggregation, this.getEntityClass(), resultClass);
|
|
|
return results.getMappedResults();
|
|
return results.getMappedResults();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -189,11 +158,10 @@ public abstract class MongoDBDao<T> {
|
|
|
* 管道查询,返回原始 Document(用于需要手动处理 _id 类型转换的场景)
|
|
* 管道查询,返回原始 Document(用于需要手动处理 _id 类型转换的场景)
|
|
|
*/
|
|
*/
|
|
|
public AggregationResults<org.bson.Document> aggregateRaw(Aggregation agg) {
|
|
public AggregationResults<org.bson.Document> aggregateRaw(Aggregation agg) {
|
|
|
- return mongoTemplate.aggregate(agg, getEntityClass(), org.bson.Document.class);
|
|
|
|
|
|
|
+ return getMongoTemplate().aggregate(agg, getEntityClass(), org.bson.Document.class);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public GeoResults<T> geoNear(NearQuery nearQuery) {
|
|
public GeoResults<T> geoNear(NearQuery nearQuery) {
|
|
|
- return mongoTemplate.geoNear(nearQuery, this.getEntityClass());
|
|
|
|
|
|
|
+ return getMongoTemplate().geoNear(nearQuery, this.getEntityClass());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|