所以我有成績控制器,我想顯示表格,顯示學生的名字,學生的姓氏,然后是表格成績的成績。我通過學生中的 Grades_id 鏈接成績,以便每個學生都有自己的成績。
問題是,我不知道如何使它作業。
@GetMapping("/grades")
public String grades(Model theModel){
List<students> theStudents = studentService.findAll();
List<grades> theGrades = null;
for(students s : theStudents){
theGrades = (List<grades>) gradesService.findById(s.getGrades_id());
}
theModel.addAttribute("grades",theGrades);
theModel.addAttribute("students",theStudents);
return "/ediary/principal/grades";
}
/
public grades findById(int theId) {
Optional<grades> result = gradesRepository.findById(theId);
grades theGrades = null;
if(result.isPresent()){
theGrades = result.get();
} else{
throw new RuntimeException("Did not find grade id - " theId);
}
return theGrades;
}
我試過這樣的東西,但它顯然不起作用,
所以我主要嘗試的是:
| 名字 | 姓氏 | 英語 | 數學 | ...
我該怎么做呢?

uj5u.com熱心網友回復:
我認為您可能會重新考慮您的概念,如果您確實 Student 有一個成績串列,并且就將 Student 物體的主鍵作為外鍵而言,Grade 物體是關系的所有者,那么您可以輕松實作這一點,然后當您獲取 Student 時,您可以獲得與之關聯的每個 Grade 物件。
uj5u.com熱心網友回復:
而不是Optional從findById()回傳中回傳List,而不是在 findById()中findById()寫入。
例如findAllById()
List<grades> result = gradesRepository.findAllById(theId);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459047.html
