[Feature] 增加 mongo repository 的 CRUD、聚合、分页等基础实现 (#280)

* feat: 增加 mongo repository 的 CRUD、聚合、分页等基础实现
This commit is contained in:
panpan 2024-06-13 10:24:16 +08:00 committed by GitHub
parent 3651b51e2a
commit caad93555d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 142 additions and 5 deletions

View File

@ -1,19 +1,93 @@
package com.xiaojusurvey.engine.repository; package com.xiaojusurvey.engine.repository;
import com.xiaojusurvey.engine.common.entity.BaseEntity; 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.Query;
import org.springframework.data.mongodb.core.query.Update;
import java.util.Collection;
import java.util.List; import java.util.List;
public interface MongoRepository { public interface MongoRepository {
/**
* 保存一个对象如果对象已存在则更新它
*/
<T extends BaseEntity> T save(T saveObject); <T extends BaseEntity> T save(T saveObject);
<T extends BaseEntity> T findById(Object id, Class<T> entityClass); /**
* 保存一个对象到指定集合如果对象已存在则更新它
*/
<T extends BaseEntity> T save(T saveObject, String collectionName);
<T extends BaseEntity> List<T> findAll(Class<T> entityClass); /**
* 批量保存一组对象
*/
<T extends BaseEntity> Collection<T> batchSave(Collection<? extends T> batchToSave, Class<T> entityClass);
/**
* 根据查询条件查找单个对象
*/
<T extends BaseEntity> T findOne(Query query, Class<T> entityClass); <T extends BaseEntity> T findOne(Query query, Class<T> entityClass);
/**
* 根据 ID 查找对象
*/
<T extends BaseEntity> T findById(Object id, Class<T> entityClass);
/**
* 查找某个类型的所有对象
*/
<T extends BaseEntity> List<T> findAll(Class<T> entityClass);
/**
* 查找并修改符合条件的对象
*/
<T extends BaseEntity> T findAndModify(Query query, Update update, Class<T> entityClass);
/**
* 查找并删除符合条件的对象
*/
<T extends BaseEntity> T findAndRemove(Query query, Class<T> entityClass);
/**
* 更新第一个符合条件的对象
*/
<T extends BaseEntity> void updateFirst(Query query, Update update, Class<T> entityClass);
/**
* 更新所有符合条件的对象
*/
<T extends BaseEntity> void updateMulti(Query query, Update update, Class<T> entityClass);
/**
* 删除符合条件的对象
*/
<T extends BaseEntity> void delete(Query query, Class<T> entityClass);
/**
* 根据 ID 删除对象
*/
<T extends BaseEntity> void deleteById(Object id, Class<T> entityClass); <T extends BaseEntity> void deleteById(Object id, Class<T> entityClass);
/**
* 统计符合条件的对象数量
*/
<T extends BaseEntity> Long count(Query query, Class<T> entityClass);
/**
* 判断是否存在符合条件的对象
*/
<T extends BaseEntity> boolean exists(Query query, Class<T> entityClass);
/**
* 执行聚合查询例如分组过滤排序投影等
*/
<T> AggregationResults<T> aggregate(Aggregation aggregation, Class<?> inputType, Class<T> outputType);
/**
* 多条件分页查询
*/
<T extends BaseEntity> List<T> page(Query query, int pageIndex, int pageSize, Class<T> entityClass);
} }

View File

@ -3,12 +3,17 @@ package com.xiaojusurvey.engine.repository.impl;
import com.xiaojusurvey.engine.common.entity.BaseEntity; import com.xiaojusurvey.engine.common.entity.BaseEntity;
import com.xiaojusurvey.engine.common.exception.DaoException; import com.xiaojusurvey.engine.common.exception.DaoException;
import com.xiaojusurvey.engine.repository.MongoRepository; 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.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.Criteria;
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.stereotype.Repository; import org.springframework.stereotype.Repository;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -20,12 +25,27 @@ public class MongoRepositoryImpl implements MongoRepository {
@Resource @Resource
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
@Override @Override
public <T extends BaseEntity> T save(T saveObject) { public <T extends BaseEntity> T save(T saveObject) {
return mongoTemplate.save(saveObject); return mongoTemplate.save(saveObject);
} }
@Override
public <T extends BaseEntity> T save(T saveObject, String collectionName) {
return mongoTemplate.save(saveObject, collectionName);
}
@Override
public <T extends BaseEntity> Collection<T> batchSave(Collection<? extends T> batchToSave,
Class<T> entityClass) {
return mongoTemplate.insert(batchToSave, entityClass);
}
@Override
public <T extends BaseEntity> T findOne(Query query, Class<T> entityClass) {
return mongoTemplate.findOne(query, entityClass);
}
@Override @Override
public <T extends BaseEntity> T findById(Object id, Class<T> entityClass) { public <T extends BaseEntity> T findById(Object id, Class<T> entityClass) {
if (Objects.isNull(id)) { if (Objects.isNull(id)) {
@ -39,10 +59,29 @@ public class MongoRepositoryImpl implements MongoRepository {
return mongoTemplate.findAll(entityClass); return mongoTemplate.findAll(entityClass);
} }
@Override
public <T extends BaseEntity> T findAndModify(Query query, Update update, Class<T> entityClass) {
return mongoTemplate.findAndModify(query, update, entityClass);
}
@Override @Override
public <T extends BaseEntity> T findOne(Query query, Class<T> entityClass) { public <T extends BaseEntity> T findAndRemove(Query query, Class<T> entityClass) {
return mongoTemplate.findOne(query, entityClass); return mongoTemplate.findAndRemove(query, entityClass);
}
@Override
public <T extends BaseEntity> void updateFirst(Query query, Update update, Class<T> entityClass) {
mongoTemplate.updateFirst(query, update, entityClass);
}
@Override
public <T extends BaseEntity> void updateMulti(Query query, Update update, Class<T> entityClass) {
mongoTemplate.updateMulti(query, update, entityClass);
}
@Override
public <T extends BaseEntity> void delete(Query query, Class<T> entityClass) {
mongoTemplate.remove(query, entityClass);
} }
@Override @Override
@ -50,4 +89,28 @@ public class MongoRepositoryImpl implements MongoRepository {
Query query = new Query(Criteria.where("_id").is(id)); Query query = new Query(Criteria.where("_id").is(id));
mongoTemplate.remove(query, entityClass); mongoTemplate.remove(query, entityClass);
} }
@Override
public <T extends BaseEntity> Long count(Query query, Class<T> entityClass) {
return mongoTemplate.count(query, entityClass);
}
@Override
public <T extends BaseEntity> boolean exists(Query query, Class<T> entityClass) {
return mongoTemplate.exists(query, entityClass);
}
@Override
public <T> AggregationResults<T> aggregate(Aggregation aggregation, Class<?> inputType, Class<T> outputType) {
return mongoTemplate.aggregate(aggregation, inputType, outputType);
}
@Override
public <T extends BaseEntity> List<T> page(Query query, int pageIndex, int pageSize, Class<T> entityClass) {
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
query.with(pageRequest);
return mongoTemplate.find(query, entityClass);
}
} }