图片.png图片.png图片.png图片.png

一、添加依赖

1、service-edu模块配置依赖

  1. <dependencies>
  2. <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
  3. <dependency>
  4. <groupId>com.alibaba</groupId>
  5. <artifactId>easyexcel</artifactId>
  6. <version>2.1.1</version>
  7. </dependency>
  8. </dependencies>

二、业务处理

1、SubjectAdminController

  1. package com.guli.edu.controller.admin;
  2. @Api(description="课程分类管理")
  3. @CrossOrigin //跨域
  4. @RestController
  5. @RequestMapping("/eduservice/subject")
  6. public class SubjectAdminController {
  7. @Autowired
  8. private SubjectService subjectService;
  9. //添加课程分类
  10. @ApiOperation(value = "Excel批量导入")
  11. @PostMapping("addSubject")
  12. public R addSubject(MultipartFile file) {
  13. //1 获取上传的excel文件 MultipartFile
  14. //返回错误提示信息
  15. subjectService.importSubjectData(file,subjectService);
  16. //判断返回集合是否为空
  17. return R.ok();
  18. }
  19. }

2、创建和Excel对应的实体类

  1. import com.alibaba.excel.annotation.ExcelProperty;
  2. import lombok.Data;
  3. @Data
  4. public class ExcelSubjectData {
  5. @ExcelProperty(index = 0)
  6. private int oneSubjectName;
  7. @ExcelProperty(index = 1)
  8. private String twoSubjectName;
  9. }

3、SubjectService

(1)接口

  1. void batchImport(MultipartFile file);

(2)实现类

  1. //添加课程分类
  2. //poi读取excel内容
  3. @Override
  4. public void importSubjectData(MultipartFile file,EduSubjectService subjectService) {
  5. try {
  6. //1 获取文件输入流
  7. InputStream inputStream = file.getInputStream();
  8. // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
  9. EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
  10. }catch(Exception e) {
  11. e.printStackTrace();
  12. throw new GuliException(20002,"添加课程分类失败");
  13. }
  14. }

4、创建读取Excel监听器

  1. import com.alibaba.excel.context.AnalysisContext;
  2. import com.alibaba.excel.event.AnalysisEventListener;
  3. import com.atguigu.eduservice.entity.EduSubject;
  4. import com.atguigu.eduservice.entity.vo.ExcelSubjectData;
  5. import com.atguigu.eduservice.service.EduSubjectService;
  6. import com.atguigu.servicebase.handler.GuliException;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Map;
  11. public class SubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {
  12. public EduSubjectService subjectService;
  13. public SubjectExcelListener() {}
  14. //创建有参数构造,传递subjectService用于操作数据库
  15. public SubjectExcelListener(EduSubjectService subjectService) {
  16. this.subjectService = subjectService;
  17. }
  18. //一行一行去读取excle内容
  19. @Override
  20. public void invoke(ExcelSubjectData user, AnalysisContext analysisContext) {
  21. if(user == null) {
  22. throw new GuliException(20001,"添加失败");
  23. }
  24. //添加一级分类
  25. EduSubject existOneSubject = this.existOneSubject(subjectService,user.getOneSubjectName());
  26. if(existOneSubject == null) {//没有相同的
  27. existOneSubject = new EduSubject();
  28. existOneSubject.setTitle(user.getOneSubjectName());
  29. existOneSubject.setParentId("0");
  30. subjectService.save(existOneSubject);
  31. }
  32. //获取一级分类id值
  33. String pid = existOneSubject.getId();
  34. //添加二级分类
  35. EduSubject existTwoSubject = this.existTwoSubject(subjectService,user.getTwoSubjectName(), pid);
  36. if(existTwoSubject == null) {
  37. existTwoSubject = new EduSubject();
  38. existTwoSubject.setTitle(user.getTwoSubjectName());
  39. existTwoSubject.setParentId(pid);
  40. subjectService.save(existTwoSubject);
  41. }
  42. }
  43. //读取excel表头信息
  44. @Override
  45. public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
  46. System.out.println("表头信息:"+headMap);
  47. }
  48. //读取完成后执行
  49. @Override
  50. public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
  51. //判断一级分类是否重复
  52. private EduSubject existTwoSubject(EduSubjectService subjectService,String name,String pid) {
  53. QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
  54. wrapper.eq("title",name);
  55. wrapper.eq("parent_id",pid);
  56. EduSubject eduSubject = subjectService.getOne(wrapper);
  57. return eduSubject;
  58. }
  59. //判断一级分类是否重复
  60. private EduSubject existOneSubject(EduSubjectService subjectService,String name) {
  61. QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
  62. wrapper.eq("title",name);
  63. wrapper.eq("parent_id","0");
  64. EduSubject eduSubject = subjectService.getOne(wrapper);
  65. return eduSubject;
  66. }
  67. }

三、分类列表前端实现

1、参考 views/tree/index.vue

2、创建api

api/edu/subject.js

  1. import request from '@/utils/request'
  2. const api_name = '/admin/edu/subject'
  3. export default {
  4. getNestedTreeList() {
  5. return request({
  6. url: `${api_name}`,
  7. method: 'get'
  8. })
  9. }
  10. }

3、list.vue

  1. <template>
  2. <div class="app-container">
  3. <el-input v-model="filterText" placeholder="Filter keyword" style="margin-bottom:30px;" />
  4. <el-tree
  5. ref="subjectTree"
  6. :data="subjectList"
  7. :props="defaultProps"
  8. :filter-node-method="filterNode"
  9. class="filter-tree"
  10. default-expand-all
  11. />
  12. </div>
  13. </template>
  14. <script>
  15. import subject from '@/api/edu/subject'
  16. export default {
  17. data() {
  18. return {
  19. filterText: '',
  20. subjectList: [],
  21. defaultProps: {
  22. children: 'children',
  23. label: 'title'
  24. }
  25. }
  26. },
  27. watch: {
  28. filterText(val) {
  29. this.$refs.subjectTree.filter(val)
  30. }
  31. },
  32. created() {
  33. this.fetchNodeList()
  34. },
  35. methods: {
  36. fetchNodeList() {
  37. subject.getNestedTreeList().then(response => {
  38. if (response.success === true) {
  39. this.subjectList = response.data.items
  40. }
  41. })
  42. },
  43. filterNode(value, data) {
  44. if (!value) return true
  45. return data.title.indexOf(value) !== -1
  46. }
  47. }
  48. }
  49. </script>

四、分类列表后端实现

1、创建vo

  1. package com.guli.edu.vo;
  2. @Data
  3. public class SubjectVo {
  4. private String id;
  5. private String title;
  6. }
  1. package com.guli.edu.vo;
  2. @Data
  3. public class SubjectNestedVo {
  4. private String id;
  5. private String title;
  6. private List<SubjectVo> children = new ArrayList<>();
  7. }

2、创建controller

  1. @ApiOperation(value = "嵌套数据列表")
  2. @GetMapping("")
  3. public R nestedList(){
  4. List<SubjectNestedVo> subjectNestedVoList = subjectService.nestedList();
  5. return R.ok().data("items", subjectNestedVoList);
  6. }

3、创建service

接口

  1. List<SubjectNestedVo> nestedList();

实现Final

  1. @Override
  2. public List<SubjectNestedVo> nestedList() {
  3. //最终要的到的数据列表
  4. ArrayList<SubjectNestedVo> subjectNestedVoArrayList = new ArrayList<>();
  5. //获取一级分类数据记录
  6. QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
  7. queryWrapper.eq("parent_id", 0);
  8. queryWrapper.orderByAsc("sort", "id");
  9. List<Subject> subjects = baseMapper.selectList(queryWrapper);
  10. //获取二级分类数据记录
  11. QueryWrapper<Subject> queryWrapper2 = new QueryWrapper<>();
  12. queryWrapper2.ne("parent_id", 0);
  13. queryWrapper2.orderByAsc("sort", "id");
  14. List<Subject> subSubjects = baseMapper.selectList(queryWrapper2);
  15. //填充一级分类vo数据
  16. int count = subjects.size();
  17. for (int i = 0; i < count; i++) {
  18. Subject subject = subjects.get(i);
  19. //创建一级类别vo对象
  20. SubjectNestedVo subjectNestedVo = new SubjectNestedVo();
  21. BeanUtils.copyProperties(subject, subjectNestedVo);
  22. subjectNestedVoArrayList.add(subjectNestedVo);
  23. //填充二级分类vo数据
  24. ArrayList<SubjectVo> subjectVoArrayList = new ArrayList<>();
  25. int count2 = subSubjects.size();
  26. for (int j = 0; j < count2; j++) {
  27. Subject subSubject = subSubjects.get(j);
  28. if(subject.getId().equals(subSubject.getParentId())){
  29. //创建二级类别vo对象
  30. SubjectVo subjectVo = new SubjectVo();
  31. BeanUtils.copyProperties(subSubject, subjectVo);
  32. subjectVoArrayList.add(subjectVo);
  33. }
  34. }
  35. subjectNestedVo.setChildren(subjectVoArrayList);
  36. }
  37. return subjectNestedVoArrayList;
  38. }

五、优化前端过滤功能

  1. filterNode(value, data) {
  2. if (!value) return true
  3. return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1
  4. }