SpringBoot 環境真的很新,對所有的注解和方法都不熟悉。
我的問題很簡單,但我完全卡住了。我有一個 Table Device,其中包含一些列,例如Type、Constructor、Model。
我想按模型為每個建構式和型別計算設備數量,并以以下格式回傳所有這些設備的映射:
"numberOfDevicePerTypeConstructorModel": {
"Type1": {
"Constructor1": {
"Model1": 0,
"Model2": 0,
},
"Constructor2": {
"Model1": 0,
"Model2": 0,
}
},
"Type2": {
"Constructor1": {
"Model1": 0,
"Model2": 0,
},
"Category2": {
"Model1": 0,
"Model2": 0,
}
},
}
例如,我可以輕松地按模型獲取設備數量的簡單 Map<String, Long> ,如下所示:
for (String model : deviceRepository.findDistinctModel()) {
deviceStats.numberOfDevicePerModel.put(model, deviceRepository.countByModel(model));
}
但是,如果現在我嘗試在Type和Constructor上添加一些“for 回圈”并制作一些中間 Map<String, Long> ,它實際上并不起作用。
public class DeviceService() {
public class DeviceStats() {
public Map<String,Long> numberPerModel;
public Map<String,Map<String,Long>> numberPerConstructorModel;
public Map<String, Map<String,Map<String,Long>>> numberOfDevicePerTypeConstructorModel;
DeviceStats(){
numberPerModel = new HashMap<String,Long>();
numberPerConstructorModel = new HashMap<String,Map<String,Long>>();
numberOfDevicePerTypeConstructorModel = new HashMap<String,Map<String,Map<String,Long>>>();
}
}
public DeviceStats getDeviceStats() {
for(String type : deviceRepository.findDistinctType()) {
for(String constructor : deviceRepository.findDistinctConstructorByType(type)){
for (String model : deviceRepository.findDistinctModelByTypeAndConstructor(type,constructor)) {
deviceStats.numberPerModel.put(model, deviceRepository.countByModelAndConstructorAndType(model, constructor,type));
}
deviceStats.numberPerConstructorModel.put(constructor, deviceStats.numberPerModel);
}
deviceStats.numberOfDevicePerTypeConstructorModel.put(type, deviceStats.numberPerConstructorModel);
}
}
}
希望我已經清楚了。感謝您提供有關如何輕松做到這一點的幫助或建議。(我確信它可以在幾行代碼內完成,但我不知道如何。)
謝謝
uj5u.com熱心網友回復:
如果我可以在這里宣講一點:
我認為在 for 回圈中進行 db 呼叫不是一個好主意。如果您首先以適合您的方式構建查詢,那么在處理之前,您最終會得到一個List<DeviceStat> deviceStats,然后:
public static Map<String, Map<String, Map<String, Long>>> getDeviceStats(List<DeviceStat> deviceStats) {
return deviceStats.stream()
.collect(Collectors.groupingBy(DeviceStat::getType,
Collectors.groupingBy(DeviceStat::getConstructor,
Collectors.groupingBy(DeviceStat::getModel,
Collectors.counting()))));
}
uj5u.com熱心網友回復:
我認為您不需要嵌入式課程。你需要這樣的東西:
public class DeviceService() {
public Map<String, Map<String, Map<String, Long>>> getDeviceStats() {
Map<String, Map<String, Map<String, Long>>> numberOfDevicePerTypeConstructorModel =
new HashMap<String, Map<String, Map<String, Long>>>();
for (String type : deviceRepository.findDistinctType()) {
Map<String, Map<String, Long>> numberPerConstructorModel = new HashMap<String, Map<String, Long>>();
numberOfDevicePerTypeConstructorModel.put(type, numberPerConstructorModel);
for (String constructor : deviceRepository.findDistinctConstructorByType(type)) {
Map<String, Long> numberPerModel = new HashMap<String, Long>();
numberPerConstructorModel.put(constructor, numberPerModel);
for (String model : deviceRepository.findDistinctModelByTypeAndConstructor(type, constructor)) {
numberPerModel.put(model, deviceRepository.countByModelAndConstructorAndType(model, constructor, type));
}
}
}
return numberOfDevicePerTypeConstructorModel;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/335972.html
上一篇:如何模擬服務層中的方法
