课程大纲-章节和小节列表功能(接口)

35 阅读1分钟

1、 通过代码生成器生成章节、小节代码

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;
public class CodeGenerator {
    @Test
    public void run() {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("testjava");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式
        mpg.setGlobalConfig(gc);
        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("guli");
        dsc.setPassword("123123");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        // 4、包配置
        PackageConfig pc = new PackageConfig();
        //com.atguigu.eduservice
        pc.setModuleName("eduservice"); //模块名
        pc.setParent("com.atguigu");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);
        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("edu_chapter","edu_video");
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
        mpg.setStrategy(strategy);


        // 6、执行
        mpg.execute();
    }
}

2、 创建两个vo,章节vo、小节vo,章节vo包含小节vo的List集合

@Data
public class VideoVo {
    private String id;
    private String title;
}

@Data
public class ChapterVo {
    private String id;
    private String title;
    private List<VideoVo> children = new ArrayList<>();
}

3、根据课程id查询章节小节信息接口

(1)EduChapterController添加根据课程id查询章节小节信息方法

@ApiOperation(value = "根据课程id查询章节小节信息")
@GetMapping("getChapterVideoById/{courseId}")
public R getChapterVideoById(@PathVariable String courseId){
    List<ChapterVo> allChapterVideo = chapterService.getChapterVideoById(courseId);
    return R.ok().data("allChapterVideo",allChapterVideo);
}

(2)在EduChapterService添加根据课程id查询章节小节信息接口方法

List<ChapterVo> getChapterVideoById(String courseId);

(3)在EduChapterServiceImpl实现根据课程id查询章节小节信息接口方法

//根据课程id查询章节小节信息
@Override
public List<ChapterVo> getChapterVideoById(String courseId) {
    //1根据课程id查询章节信息
    QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
    wrapperChapter.eq("course_id",courseId);
    List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);
    //2根据课程id查询小节信息
    QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
    wrapperVideo.eq("course_id",courseId);
    List<EduVideo> videoList = videoService.list(wrapperVideo);
    //3创建集合,用于封装
    List<ChapterVo> finalList = new ArrayList<>();
    //4遍历章节封装
    for (int i = 0; i < chapterList.size(); i++) {
        EduChapter eduChapter = chapterList.get(i);
        ChapterVo chapterVo = new ChapterVo();
        BeanUtils.copyProperties(eduChapter,chapterVo);
        finalList.add(chapterVo);
        //5遍历小节封装
        List<VideoVo> videoVoList = new ArrayList<>();
        for (int m = 0; m < videoList.size(); m++) {
            EduVideo eduVideo = videoList.get(m);
            if(eduChapter.getId().equals(eduVideo.getChapterId())){
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(eduVideo,videoVo);
                videoVoList.add(videoVo);
            }
        }
        chapterVo.setChildren(videoVoList);
    }
       return finalList;
}