29.课程分类添加功能
一、数据库导入
CREATE TABLE `edu_subject` ( `id` char(19) NOT NULL COMMENT '课程类别ID', `title` varchar(10) NOT NULL COMMENT '类别名称', `parent_id` char(19) NOT NULL DEFAULT '0' COMMENT '父ID', `sort` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '排序字段', `gmt_create` datetime NOT NULL COMMENT '创建时间', `gmt_modified` datetime NOT NULL COMMENT '更新时间', PRIMARY KEY (`id`), KEY `idx_parent_id` (`parent_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='课程科目';
二、课程分类功能
1、引入easyexcel依赖
<dependencies> <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel --> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.4</version> </dependency> </dependencies>
2、使用代码生成器生成课程分类代码
只需要更改表名即可
3、创建实体类和excel对应关系
@Data public class SubjectData { @ExcelProperty(index = 0) private String oneSubjectName;//第一列 @ExcelProperty(index = 1) private String twoSubjectName;//第二列 }
4、编写EduSubjectController代码
@RestController
@RequestMapping("/eduservice/edu-subject")
public class EduSubjectController {
@Autowired
private EduSubjectService subjectService;
// 添加课程分类
//获取上传过来的文件,把文件读取出来
@PostMapping("addSubject")
public R addSubject(MultipartFile file){
//上传过来的文件
subjectService.saveSubject(file, subjectService);
return R.ok();
}
}
5、编写对应的EduSubjectService接口代码
public interface EduSubjectService extends IService<EduSubject> {
void saveSubject(MultipartFile file, EduSubjectService subjectService);
}
传入EduSubjectService对象是为了后面监听器调用
6.编写接口实现类EduSubjectServiceImpl代码
//添加课程分类
@Override
public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
try{
//文件输入流
InputStream in = file.getInputStream();
//调用方法进行读取
EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
}catch (Exception e){
e.printStackTrace();
}
}
使用EasyExcel.read对excel的数据进行读取,参数SubjectData.class为与excel对应的实体类,监听器new SubjectExcelListener用于读取并保存数据
7、监听器实现
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> { //因为SubjectExcelListener不能交给spring进行管理(即@service),需要自己new,不能注入其他对象 //不能实现数据库操作,需要手动的jdbc public EduSubjectService subjectService; public SubjectExcelListener() {} public SubjectExcelListener(EduSubjectService eduSubjectService) { this.subjectService = eduSubjectService; } //读取excel内容,一行一行进行读取 @Override public void invoke(SubjectData subjectData, AnalysisContext analysisContext) { if(subjectData == null) { throw new GuliException(20001,"文件数据为空"); } //一行一行读取,每次读取都有两个值,第一个为一级分类,第二个为二级分类 //判断一级分类是否重复 EduSubject existOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName()); if (existOneSubject == null){ //没有相同的一级分类,则添加 existOneSubject = new EduSubject(); existOneSubject.setParentId("0"); existOneSubject.setTitle(subjectData.getOneSubjectName());//一级分类名称 subjectService.save(existOneSubject); } //获取一级分类id值 String pid = existOneSubject.getId(); //添加二级分类 //判断二级分类是否重复 EduSubject existTwoSubject = this.existTwoSubject(subjectService, subjectData.getTwoSubjectName(),pid); if (existTwoSubject == null){ //没有相同的二级分类,则添加 existTwoSubject = new EduSubject(); existTwoSubject.setParentId(pid); existTwoSubject.setTitle(subjectData.getTwoSubjectName());//二级分类名称 subjectService.save(existTwoSubject); } } //判断一级分类不能重复添加 private EduSubject existOneSubject(EduSubjectService subjectService,String name) { QueryWrapper<EduSubject> wrapper = new QueryWrapper<>(); wrapper.eq("title",name); wrapper.eq("parent_id","0"); EduSubject oneSubject = subjectService.getOne(wrapper); return oneSubject; } //判断二级分类不能重复添加 private EduSubject existTwoSubject(EduSubjectService subjectService,String name,String pid) { QueryWrapper<EduSubject> wrapper = new QueryWrapper<>(); wrapper.eq("title",name); wrapper.eq("parent_id",pid); EduSubject twoSubject = subjectService.getOne(wrapper); return twoSubject; } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { } }
excel数据:
对excel进行一行行遍历,首先判断一级分类在数据库中是否存在,不存在则添加,置parent_id为0,存在则不做操作。
进而判断二级分类是否存在,存在则添加,置parent_id为一级分类的id,存在则不做操作。
最终保存到数据库中: