课程分类的建表语句
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='课程科目'; # # Data for table "edu_subject" # INSERT INTO `edu_subject` VALUES ('1178214681118568449','后端开发','0',1,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681139539969','Java','1178214681118568449',1,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681181483010','前端开发','0',3,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681210843137','JavaScript','1178214681181483010',4,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681231814658','云计算','0',5,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681252786178','Docker','1178214681231814658',5,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681294729217','Linux','1178214681231814658',6,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681324089345','系统/运维','0',7,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681353449473','Linux','1178214681324089345',7,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681382809602','Windows','1178214681324089345',8,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681399586817','数据库','0',9,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681428946945','MySQL','1178214681399586817',9,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681454112770','MongoDB','1178214681399586817',10,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681483472898','大数据','0',11,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681504444418','Hadoop','1178214681483472898',11,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681529610242','Spark','1178214681483472898',12,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681554776066','人工智能','0',13,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681584136193','Python','1178214681554776066',13,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681613496321','编程语言','0',14,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178214681626079234','Java','1178214681613496321',14,'2019-09-29 15:47:25','2019-09-29 15:47:25'),('1178585108407984130','Python','1178214681118568449',2,'2019-09-30 16:19:22','2019-09-30 16:19:22'),('1178585108454121473','HTML/CSS','1178214681181483010',3,'2019-09-30 16:19:22','2019-09-30 16:19:22');
课程对应的实体
package com.atguigu.eduservice.entity; import com.baomidou.mybatisplus.annotation.IdType; import java.util.Date; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; import java.util.List; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; /** * <p> * 课程科目 * </p> * * @author wangjie * @since 2020-07-08 */ @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 = "创建时间") private Date gmtCreate; @ApiModelProperty(value = "更新时间") private Date gmtModified; @ApiModelProperty(value = "子菜单") private List<EduSubject> children; }
获取树形结构的主要代码
package com.atguigu.eduservice.service.impl; import com.atguigu.eduservice.entity.EduSubject; import com.atguigu.eduservice.mapper.EduSubjectMapper; import com.atguigu.eduservice.service.EduSubjectService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import java.util.*; /** * <p> * 课程科目 服务实现类 * </p> * * @author wangjie * @since 2020-07-08 */ @Service public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService { @Override public List<EduSubject> findAllSubjectList() { return findTree(); } /* * 排序,根据order排序 */ public Comparator<EduSubject> order() { Comparator<EduSubject> comparator = new Comparator<EduSubject>() { @Override public int compare(EduSubject o1, EduSubject o2) { if (o1.getSort() != o2.getSort()) { return o1.getSort() - o2.getSort(); } return 0; } }; return comparator; } /** * 生成树 * * @return */ public List<EduSubject> findTree() { //最终的树形结构 List<EduSubject> data = new ArrayList<>(); try {//查询所有菜单 QueryWrapper<EduSubject> subjectWrapper = new QueryWrapper<>(); subjectWrapper.select(EduSubject.class,info->!info.getColumn().equals("children")); List<EduSubject> allMenu = baseMapper.selectList(subjectWrapper); //根节点 List<EduSubject> rootMenu = new ArrayList<EduSubject>(); for (EduSubject nav : allMenu) { if (nav.getParentId().equals("0")) {//父节点是0的,为根节点。 rootMenu.add(nav); } } /* 根据Menu类的order排序 */ Collections.sort(rootMenu, order()); //为根菜单设置子菜单,getClild是递归调用的 for (EduSubject nav : rootMenu) { /* 获取根节点下的所有子节点 使用getChild方法*/ List<EduSubject> childList = getChild(nav.getId(), allMenu); nav.setChildren(childList);//给根节点设置子节点 } data.addAll(rootMenu); return data; } catch (Exception e) { e.printStackTrace(); return data; } } /** * 获取子节点 * * @param id 父节点id * @param allMenu 所有菜单列表 * @return 每个根节点下,所有子菜单列表 */ public List<EduSubject> getChild(String id, List<EduSubject> allMenu) { //子菜单 List<EduSubject> childList = new ArrayList<EduSubject>(); for (EduSubject nav : allMenu) { // 遍历所有节点,将所有菜单的父id与传过来的根节点的id比较 //相等说明:为该根节点的子节点。 if (nav.getParentId().equals(id)) { childList.add(nav); } } //递归 for (EduSubject nav : childList) { nav.setChildren(getChild(nav.getId(), allMenu)); } Collections.sort(childList, order());//排序 //如果节点下没有子节点,返回一个空List(递归退出) if (childList.size() == 0) { return new ArrayList<EduSubject>(); } return childList; } }