3.2.2 后端基本结构
初始化数据库CREATEDATABASEzx_edu_vod; USEzx_edu_vod; CREATETABLEvod_category( cate_idBIGINTPRIMARYKEY, cate_nameVARCHAR(64), `level`BIGINT, parent_idBIGINT, `type`VARCHAR(10) COMMENT'取值:default、material'); 完善JavaBeanpackagecom.czxy.zx.vod.utils; importcom.baomidou.mybatisplus.annotation.TableField; importcom.baomidou.mybatisplus.annotation.TableId; importcom.baomidou.mybatisplus.annotation.TableName; importlombok.Data; importjava.util.ArrayList; importjava.util.List; /** vod 分类封装对象(无限极)* @author 桐叔* @email liangtong@itcast.cn*/"vod_category") (publicclassVodCategory { privateLongcateId; privateStringcateName; privateLonglevel; privateLongparentId; privateStringtype; exist=false) (privateList<VodCategory>children; } mapperpackagecom.czxy.zx.vod.mapper; importcom.baomidou.mybatisplus.core.mapper.BaseMapper; importcom.czxy.zx.vod.utils.VodCategory; importorg.apache.ibatis.annotations.Mapper; /*** @author 桐叔* @email liangtong@itcast.cn*/publicinterfaceVodCategoryMapperextendsBaseMapper<VodCategory> { } servicepackagecom.czxy.zx.vod.service; importcom.baomidou.mybatisplus.extension.service.IService; importcom.czxy.zx.vod.utils.VodCategory; /*** @author 桐叔* @email liangtong@itcast.cn*/publicinterfaceVodCategoryServiceextendsIService<VodCategory> { } packagecom.czxy.zx.vod.service.impl; importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl; importcom.czxy.zx.vod.mapper.VodCategoryMapper; importcom.czxy.zx.vod.service.VodCategoryService; importcom.czxy.zx.vod.utils.VodCategory; importorg.springframework.stereotype.Service; importorg.springframework.transaction.annotation.Transactional; /*** @author 桐叔* @email liangtong@itcast.cn*/publicclassVodCategoryServiceImplextendsServiceImpl<VodCategoryMapper, VodCategory>implementsVodCategoryService { }
3.2.3 定时添加
- 每个月1号零点更新数据
- 修改启动类
packagecom.czxy.zx; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.cloud.client.discovery.EnableDiscoveryClient; importorg.springframework.scheduling.annotation.EnableScheduling; /*** @author 桐叔* @email liangtong@itcast.cn*/publicclassZxVodServiceApplication { publicstaticvoidmain(String[] args) { SpringApplication.run(ZxVodServiceApplication.class,args); } }
packagecom.czxy.zx.vod.schedule; importcom.czxy.zx.vod.service.VodCategoryService; importcom.czxy.zx.vod.utils.VideoUtils; importcom.czxy.zx.vod.utils.VodCategory; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.scheduling.annotation.Scheduled; importorg.springframework.stereotype.Component; importjavax.annotation.Resource; importjava.util.List; /*** @author 桐叔* @email liangtong@itcast.cn*/publicclassCategorySchedule { privateVideoUtilsvideoUtils; privateVodCategoryServicevodCategoryService; privateLoggerLOGGER=LoggerFactory.getLogger(CategorySchedule.class); /*** 每月1号零点更新*/cron="0 0 0 1 * ?") (publicvoidhandlerCategory() { // 1 获得所有分类List<VodCategory>categoryList=videoUtils.getCategoryList(); // 2 校验通过后,没有添加batchAll(categoryList); } /*** 批量处理所有分类,递归处理子分类* @param categoryList*/privatevoidbatchAll(List<VodCategory>categoryList) { System.out.println("处理:"+categoryList.size()); if(LOGGER.isInfoEnabled()) { LOGGER.info("处理:"+categoryList.size()); } for (VodCategoryvodCategory : categoryList) { VodCategoryfindVodCategory=vodCategoryService.getById(vodCategory.getCateId()); if(findVodCategory==null) { vodCategoryService.save(vodCategory); if(LOGGER.isInfoEnabled()) { LOGGER.info("添加:"+vodCategory.getCateId()); } } else { // TODO 是否更新? } // 处理孩子batchAll(vodCategory.getChildren()); } } }
3.2.4 查询所有分类
packagecom.czxy.zx.vod.controller; importcom.aliyun.vod.upload.resp.BaseResponse; importcom.czxy.zx.vo.BaseResult; importcom.czxy.zx.vod.service.VodCategoryService; importcom.czxy.zx.vod.utils.VodCategory; importorg.springframework.web.bind.annotation.GetMapping; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; importjavax.annotation.Resource; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; /*** @author 桐叔* @email liangtong@itcast.cn*/"/vodcategory") (publicclassVodCategoryController { privateVodCategoryServicevodCategoryService; publicBaseResultfindAll() { //1 查询所有List<VodCategory>list=vodCategoryService.list(); //2 处理List<VodCategory>categoryList=newArrayList<>(); Map<Long,VodCategory>cache=newHashMap<>(); list.forEach(vodCategory-> { if(vodCategory.getParentId() ==-1) { categoryList.add(vodCategory); } else { VodCategorycacheCategory=cache.get(vodCategory.getParentId()); // 初始化计划List<VodCategory>childrenList=cacheCategory.getChildren(); if(childrenList==null) { cacheCategory.setChildren(newArrayList<>()); } // 追加数据到集合cacheCategory.getChildren().add(vodCategory); } cache.put(vodCategory.getCateId(), vodCategory); }); //3 返回returnBaseResult.ok("查询成功", categoryList); } }
3.3 vod转码模板管理
3.3.1 后端基本结构
表结构#转码模板表CREATETABLEvod_template( template_idVARCHAR(32) PRIMARYKEY, `name`VARCHAR(128) COMMENT'', is_defaultVARCHAR(20) COMMENT'取值:Default、NotDefault', transcode_modeVARCHAR(20) COMMENT'取值:NoTranscode、FastTranscode'); JavaBean,修改VodTemplatepackagecom.czxy.zx.vod.utils; importcom.baomidou.mybatisplus.annotation.IdType; importcom.baomidou.mybatisplus.annotation.TableId; importcom.baomidou.mybatisplus.annotation.TableName; importlombok.Data; /*** @author 桐叔* @email liangtong@itcast.cn*/"vod_template") (publicclassVodTemplate { type=IdType.ASSIGN_UUID) (privateStringtemplateId; //idprivateStringname; //名称privateStringisDefault; //是否默认,取值:Default、NotDefaultprivateStringtranscodeMode; //模式,取值:NoTranscode、FastTranscode}
packagecom.czxy.zx.vod.mapper; importcom.baomidou.mybatisplus.core.mapper.BaseMapper; importcom.czxy.zx.vod.utils.VodTemplate; importorg.apache.ibatis.annotations.Mapper; /*** @author 桐叔* @email liangtong@itcast.cn*/publicinterfaceVodTemplateMapperextendsBaseMapper<VodTemplate> { }
packagecom.czxy.zx.vod.service.impl; importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl; importcom.czxy.zx.vod.mapper.VodTemplateMapper; importcom.czxy.zx.vod.service.VodTemplateService; importcom.czxy.zx.vod.utils.VodTemplate; importorg.springframework.stereotype.Service; importorg.springframework.transaction.annotation.Transactional; /*** @author 桐叔* @email liangtong@itcast.cn*/publicclassVodTemplateServiceImplextendsServiceImpl<VodTemplateMapper, VodTemplate>implementsVodTemplateService { }