如何处理spring数据mongo db动态查询创建。 我有一个列表,我需要通过各种过滤条件(即扇区,类别,状态,或扇区和状态组合列表)进行过滤。这是用户具有过滤列表的所有灵活性作为他的希望。 所以在spring数据中mongo db如何处理这种用例。 使用if .. else并执行不同的查询是不可能给出所有类型的组合。
我的mongodb版本:v3.4.4
模型
@Document 公共类投资扩展AbstractMongoDocument {
private String legalInvestmentName;
private InvestmentCategory category; => ENUM type
private InvestmentSector sector;
private InvestmentStatus status;
private InvestmentType type;
.................................
} 从UI调用方法,同时单击搜索按钮
filterList(sector, category, type, status): Observable<any[]> {
var params = new URLSearchParams();
params.set("sector", sector);
params.set("category", category);
params.set("type", type);
params.set("status", status);
return this.http.get('investments/filterBy', { search: params })
.map(this.extractData)
.catch(this.handleError);
}
控制器
@GetMapping(value = "/filterBy")
@CrossOrigin(value = "*")
public List<Investment> getInvestmentListFilterBy(@RequestParam(value = "sector", required = false) String sector, @RequestParam(value = "category", required = false) String category, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "status", required = false) String status) throws InvestmentNotFoundException {
String investorSector = "";
String investmentCategory = "";
String investmentType = "";
String investmentStatus = "";
if (sector != null && !sector.isEmpty()) {
investorSector = InvestmentSector.convert(sector).name();
} else if (category != null && !category.isEmpty()) {
investmentCategory = InvestmentCategory.convert(category).name();
} else if (type != null && !type.isEmpty()) {
investmentType = InvestmentType.convert(type).name();
} else if (status != null && !status.isEmpty()) {
investmentStatus = InvestmentStatus.convert(status).name();
}
List<Investment> investmentList = null;
investmentList = investmentService.findAllInvestmentFilterBy(investorSector, investmentCategory, investmentType, investmentStatus);
return investmentList;
}
服务
@Override
public List<Investment> findAllInvestmentFilterBy(String sector, String investmentategory, String type, String status) {
List investmentList = null;
if ((sector != null && !sector.isEmpty()) && (investmentategory != null && !investmentategory.isEmpty()) && (type != null && !type.isEmpty()) && (status != null && !status.isEmpty())) {
investmentList = investmentRepository.filterBySectorCategoryTypeStatusAndCurrentStatus(sector, investmentategory, type, status, CurrentStatus.ACTIVE.name());
} else if ((sector != null && !sector.isEmpty()) && (investmentategory != null && !investmentategory.isEmpty()) && (type != null && !type.isEmpty())) {
investmentList = investmentRepository.filterBySectorCategoryTypeAndCurrentStatus(sector, investmentategory, type, CurrentStatus.ACTIVE.name());
} else if ((sector != null && !sector.isEmpty()) && (investmentategory != null && !investmentategory.isEmpty())) {
investmentList = investmentRepository.filterBySectorAndCategoryAndCurrentStatus(sector, investmentategory, CurrentStatus.ACTIVE.name());
} else if ((sector != null && !sector.isEmpty())) {
investmentList = investmentRepository.filterBySectorAndCurrentStatus(sector, CurrentStatus.ACTIVE.name());
} else if ((investmentategory != null && !investmentategory.isEmpty())) {
investmentList = investmentRepository.filterByCategory(investmentategory, CurrentStatus.ACTIVE.name());
} else if ((type != null && !type.isEmpty())) {
investmentList = investmentRepository.filterByTypeAndCurrentStatus(type, CurrentStatus.ACTIVE.name());
} else if ((status != null && !status.isEmpty())) {
investmentList = investmentRepository.filterByStatusAndCurrentStatus(status, CurrentStatus.ACTIVE.name());
} else {
investmentList = investmentRepository.getAllInvestmentsByCurrentStatus(CurrentStatus.ACTIVE.name());
}
return investmentList;
}