From caad93555d9cbabae16f3409c3e99fc84d7c2800 Mon Sep 17 00:00:00 2001 From: panpan Date: Thu, 13 Jun 2024 10:24:16 +0800 Subject: [PATCH] =?UTF-8?q?[Feature]=20=E5=A2=9E=E5=8A=A0=20mongo=20reposi?= =?UTF-8?q?tory=20=E7=9A=84=20CRUD=E3=80=81=E8=81=9A=E5=90=88=E3=80=81?= =?UTF-8?q?=E5=88=86=E9=A1=B5=E7=AD=89=E5=9F=BA=E7=A1=80=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=20(#280)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 增加 mongo repository 的 CRUD、聚合、分页等基础实现 --- .../engine/repository/MongoRepository.java | 78 ++++++++++++++++++- .../repository/impl/MongoRepositoryImpl.java | 69 +++++++++++++++- 2 files changed, 142 insertions(+), 5 deletions(-) diff --git a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/MongoRepository.java b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/MongoRepository.java index 08e9c99a..cc585519 100644 --- a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/MongoRepository.java +++ b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/MongoRepository.java @@ -1,19 +1,93 @@ package com.xiaojusurvey.engine.repository; import com.xiaojusurvey.engine.common.entity.BaseEntity; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; +import java.util.Collection; import java.util.List; public interface MongoRepository { + /** + * 保存一个对象,如果对象已存在则更新它 + */ T save(T saveObject); - T findById(Object id, Class entityClass); + /** + * 保存一个对象到指定集合,如果对象已存在则更新它 + */ + T save(T saveObject, String collectionName); - List findAll(Class entityClass); + /** + * 批量保存一组对象 + */ + Collection batchSave(Collection batchToSave, Class entityClass); + /** + * 根据查询条件查找单个对象 + */ T findOne(Query query, Class entityClass); + /** + * 根据 ID 查找对象 + */ + T findById(Object id, Class entityClass); + + /** + * 查找某个类型的所有对象 + */ + List findAll(Class entityClass); + + /** + * 查找并修改符合条件的对象 + */ + T findAndModify(Query query, Update update, Class entityClass); + + /** + * 查找并删除符合条件的对象 + */ + T findAndRemove(Query query, Class entityClass); + + /** + * 更新第一个符合条件的对象 + */ + void updateFirst(Query query, Update update, Class entityClass); + + /** + * 更新所有符合条件的对象 + */ + void updateMulti(Query query, Update update, Class entityClass); + + /** + * 删除符合条件的对象 + */ + void delete(Query query, Class entityClass); + + /** + * 根据 ID 删除对象 + */ void deleteById(Object id, Class entityClass); + + /** + * 统计符合条件的对象数量 + */ + Long count(Query query, Class entityClass); + + /** + * 判断是否存在符合条件的对象 + */ + boolean exists(Query query, Class entityClass); + + /** + * 执行聚合查询,例如分组、过滤、排序、投影等 + */ + AggregationResults aggregate(Aggregation aggregation, Class inputType, Class outputType); + + /** + * 多条件分页查询 + */ + List page(Query query, int pageIndex, int pageSize, Class entityClass); } diff --git a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java index a39fa179..576b09ae 100644 --- a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java +++ b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java @@ -3,12 +3,17 @@ package com.xiaojusurvey.engine.repository.impl; import com.xiaojusurvey.engine.common.entity.BaseEntity; import com.xiaojusurvey.engine.common.exception.DaoException; import com.xiaojusurvey.engine.repository.MongoRepository; +import org.springframework.data.domain.PageRequest; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Repository; import javax.annotation.Resource; +import java.util.Collection; import java.util.List; import java.util.Objects; @@ -20,12 +25,27 @@ public class MongoRepositoryImpl implements MongoRepository { @Resource private MongoTemplate mongoTemplate; - @Override public T save(T saveObject) { return mongoTemplate.save(saveObject); } + @Override + public T save(T saveObject, String collectionName) { + return mongoTemplate.save(saveObject, collectionName); + } + + @Override + public Collection batchSave(Collection batchToSave, + Class entityClass) { + return mongoTemplate.insert(batchToSave, entityClass); + } + + @Override + public T findOne(Query query, Class entityClass) { + return mongoTemplate.findOne(query, entityClass); + } + @Override public T findById(Object id, Class entityClass) { if (Objects.isNull(id)) { @@ -39,10 +59,29 @@ public class MongoRepositoryImpl implements MongoRepository { return mongoTemplate.findAll(entityClass); } + @Override + public T findAndModify(Query query, Update update, Class entityClass) { + return mongoTemplate.findAndModify(query, update, entityClass); + } @Override - public T findOne(Query query, Class entityClass) { - return mongoTemplate.findOne(query, entityClass); + public T findAndRemove(Query query, Class entityClass) { + return mongoTemplate.findAndRemove(query, entityClass); + } + + @Override + public void updateFirst(Query query, Update update, Class entityClass) { + mongoTemplate.updateFirst(query, update, entityClass); + } + + @Override + public void updateMulti(Query query, Update update, Class entityClass) { + mongoTemplate.updateMulti(query, update, entityClass); + } + + @Override + public void delete(Query query, Class entityClass) { + mongoTemplate.remove(query, entityClass); } @Override @@ -50,4 +89,28 @@ public class MongoRepositoryImpl implements MongoRepository { Query query = new Query(Criteria.where("_id").is(id)); mongoTemplate.remove(query, entityClass); } + + @Override + public Long count(Query query, Class entityClass) { + return mongoTemplate.count(query, entityClass); + } + + @Override + public boolean exists(Query query, Class entityClass) { + return mongoTemplate.exists(query, entityClass); + } + + @Override + public AggregationResults aggregate(Aggregation aggregation, Class inputType, Class outputType) { + return mongoTemplate.aggregate(aggregation, inputType, outputType); + } + + @Override + public List page(Query query, int pageIndex, int pageSize, Class entityClass) { + PageRequest pageRequest = PageRequest.of(pageIndex, pageSize); + query.with(pageRequest); + + return mongoTemplate.find(query, entityClass); + } + }