目录
第一章 EduChapterService.java
第二章 EduChapterController.java
第三章 EduChapterServiceImpl.java
第一章 EduChapterService.java
先写章节的服务类接口(下一步写实现类)
小tips:这里的IService是Mybatis-plus的接口。
public interface EduChapterService extends IService<EduChapter> {
//课程大纲列表,根据课程id进行查询
List<ChapterVo> getChapterVideoByCourseId(String courseId);
}
第二章 EduChapterController.java
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
@Autowired
private EduChapterService chapterService;
//课程大纲列表,根据课程id进行查询
@GetMapping("getChapterVideo/{courseId}")
public R getChapterVideo(@PathVariable String courseId) {
List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
return R.ok().data("allChapterVideo",list);
}
}
第三章 EduChapterServiceImpl.java
步骤:
1.根据课程id查询课程里面所有的章节
小tips:
1.@Service的作用
如果有一个类带了@Service注解,将自动注册到Spring容器,不需要再在applicationContext里面定义bean了。
2.@Autowired作用
可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
3.eq 等于equals 相等的意思
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
@Autowired
private EduVideoService videoService;//注入小节service
@Override
public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
//1.根据课程id查询课程里面所有的章节
QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
wrapperChapter.eq("course_id",courseId);
List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);
//先把查询出来的数据封装到eduChapterList
2.根据课程id查询课程里面所有的小节
//2.根据课程id查询课程里面所有的小节
QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
wrapperChapter.eq("course_id",courseId);
List<EduVideo> eduVideoList = videoService.list(wrapperVideo);
3.遍历查询章节list集合进行封装
//创建list集合,用于最终封装数据
List<ChapterVo> finalList = new ArrayList<>();
//3 遍历查询章节list集合进行封装
//遍历查询章节list集合
for (int i = 0; i < eduChapterList.size(); i++) { //eduChapterList.fori可以快速生成for循环
//每个章节
EduChapter eduChapter = eduChapterList.get(i);
//eduChapter对象值复制到ChapterVo里面
ChapterVo chapterVo = new ChapterVo();
BeanUtils.copyProperties(eduChapter,chapterVo);
//把chapterVo放到最终list集合
finalList.add(chapterVo);
4.遍历查询章节的小节list集合,进行封装
//创建集合,用于封装章节的小节
List<VideoVo> videoList = new ArrayList<>();
//4.遍历查询小节list集合,进行封装
for (int m = 0; m < eduVideoList.size(); m++) {
//得到每个小节
EduVideo eduVideo = eduVideoList.get(m);
//判断:小节里面chapter和章节里面id是否一样
if(eduVideo.getChapterId().equals(eduChapter.getId())) {
//进行封装
VideoVo videoVo = new VideoVo();
BeanUtils.copyProperties(eduVideo,videoVo);
//放到小节封装集合
videoList.add(videoVo);
}
}
//把封装之后小节list集合,放到章节对象里面
chapterVo.setChildren(videoList);
}
return finalList;
}