<select id="selectChaptersWithResources" resultMap="ChapterDtoMap"> SELECT c.id AS chapter_id, c.parent_id, c.name, c.courseId, sc.id AS sub_chapter_id, sc.parent_id AS sub_parent_id, sc.name AS sub_name, sc.courseId AS sub_courseId, r.id AS resource_id, r.section_id, r.name AS resource_name FROM chapter c LEFT JOIN chapter sc ON c.id = sc.parent_id LEFT JOIN resource r ON sc.id = r.section_id WHERE c.courseId = #{courseId} AND c.parent_id IS NULL </select>
</mapper>
三、其他文件
1. Mapper
language-java
1 2 3 4
public interface ChapterMapper { List<ChapterDto> selectChaptersWithResources(Long courseId); }
2. Service
language-java
1 2 3 4 5 6 7 8 9 10 11
@Service public class ChapterService { @Autowired private ChapterMapper chapterMapper;
public List<ChapterDto> getChaptersWithResources(Long courseId) { return chapterMapper.selectChaptersWithResources(courseId); } }
3. Controller
language-java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@RestController @RequestMapping("/chapters") public class ChapterController { @Autowired private ChapterService chapterService;
@GetMapping("/{courseId}") public ResponseEntity<List<ChapterDto>> getChapters(@PathVariable Long courseId) { List<ChapterDto> chapters = chapterService.getChaptersWithResources(courseId); return ResponseEntity.ok(chapters); } }