谷粒学院69——添加课程功能的实现
添加课程功能的实现
添加课程的思路很简单:上传excel表格实现课程添加,我们需要做下面这些事。
(1)引入easyexcel依赖
(2)使用代码生成器生成实体类对应的controller、service和,mapper。
将CodeGenerator
的数据库表改为edu_subject
即可。
strategy.setInclude("edu_subject");//根据数据库哪张表生成,有多张表就加逗号继续填写
运行生成代码,如果没有生成试着把CodeGenerator
移到启动类同一级目录试试。在生成的controller
中添加注解@CrossOrigin
解决跨域问题。
(3)编写controller
@RestController
@CrossOrigin
@RequestMapping("/eduservice/edu-subject")
public class EduSubjectController {
@Autowired
private EduSubjectService eduSubjectService;
@PostMapping("/addSubject")
public R addSubject(MultipartFile file) {
eduSubjectService.addSubject(file);
return R.ok();
}
}
记得在EduSubjectService接口及其实现类中增加addSubject()方法。
(4) 创建实体类
\entity\excel
下创建实体类SubjectData
。
@Data
@ToString
public class SubjectData {
//一级分类
@ExcelProperty(index = 0)
private String oneSubjectName;
//二级分类
@ExcelProperty(index = 1)
private String twoSubjectName;
}
(4)读取excel文件
新建包listner
,包下建类SubjectExcelListener
。
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
//因为SubjectExcelListener不能交给spring进行ioc管理,需要自己手动new,不能注入其他对象
//不能实现数据库操作
public EduSubjectService eduSubjectService;
//有参,传递subjectService用于操作数据库
public SubjectExcelListener(EduSubjectService eduSubjectService) {
this.eduSubjectService = eduSubjectService;
}
//无参
public SubjectExcelListener() {
}
//读取excel内容,一行一行读取
@Override
public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
//表示excel中没有数据,就不需要读取了
if (subjectData==null){
throw new GuliException(20001,"添加失败");
}
//一行一行读取,每次读取有两个值,第一个值一级分类,第二个值二级分类
//判断是否有一级分类是否重复
EduSubject existOneSubject = this.existOneSubject(eduSubjectService, subjectData.getOneSubjectName());
if (existOneSubject == null){ //没有相同的一级分类,进行添加
existOneSubject = new EduSubject();
existOneSubject.setParentId("0"); //设置一级分类id值,0代表为一级分类
existOneSubject.setTitle(subjectData.getOneSubjectName());//设置一级分类名
eduSubjectService.save(existOneSubject);//给数据库添加一级分类
}
//获取一级分类的id值
String parent_id = existOneSubject.getId();
//判断是否有耳机分类是否重复
EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), parent_id);
if (existTwoSubject==null){//没有相同的二级分类,进行添加
existTwoSubject = new EduSubject();
existTwoSubject.setParentId(parent_id); //设置二级分类id值
existTwoSubject.setTitle(subjectData.getTwoSubjectName());//设置二级分类名
eduSubjectService.save(existTwoSubject);//给数据库添加二级分类
}
}
//判断一级分类不能重复添加
private EduSubject existOneSubject(EduSubjectService eduSubjectService,String name){
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.eq("title",name)
.eq("parent_id","0");
EduSubject oneSubject = eduSubjectService.getOne(wrapper);
return oneSubject;
}
//判断二级分类不能重复添加
private EduSubject existTwoSubject(EduSubjectService eduSubjectService,String name,String parentId){
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.eq("title",name)
.eq("parent_id",parentId);
EduSubject twoSubject = eduSubjectService.getOne(wrapper);
return twoSubject;
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
实现service
,注意新增了参数eduSubjectService
,对应的controller与接口请自行调整
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
//添加课程分类
@Override
public void addSubject(MultipartFile file,EduSubjectService eduSubjectService) {
try {
//文件输入流
InputStream is = file.getInputStream();
//调用方法进行读取
EasyExcel.read(is, SubjectData.class,new SubjectExcelListener(eduSubjectService))
.sheet().doRead();
}catch (Exception e){
e.printStackTrace();
}
}
}
还有一个小细节值得注意,就是要把实体类中时间设置为自动填充。
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="EduSubject对象", description="课程科目")
public class EduSubject implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "课程类别ID")
@TableId(value = "id", type = IdType.ID_WORKER_STR)
private String id;
@ApiModelProperty(value = "类别名称")
private String title;
@ApiModelProperty(value = "父ID")
private String parentId;
@ApiModelProperty(value = "排序字段")
private Integer sort;
@ApiModelProperty(value = "创建时间")
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@ApiModelProperty(value = "更新时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;
}
使用swagger进行测试即可。
java全栈日日学 文章被收录于专栏
java全栈每日必学,不要高估自己一年能做的事,不要低估自己十年能做的事