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 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=COMPACT COMMENT='课程科目';
INSERT INTO `edu_subject` VALUES ('1286145989602295810', 'Java', '0', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145990072057857', 'java简介', '1286145989602295810', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145990294355970', 'Java入门', '1286145989602295810', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145990449545218', 'Linux', '0', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145990533431297', 'linux简介', '1286145990449545218', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145990780895234', 'Linux入门', '1286145990449545218', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145990915112962', 'VUE', '0', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145991112245250', 'vue简介', '1286145990915112962', 0, '2020-7-23 03:47:54', '2020-7-23 03:47:54');
INSERT INTO `edu_subject` VALUES ('1286145991753973761', 'vue入门', '1286145990915112962', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145991951106049', '系统/运维', '0', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145992110489601', 'Linux', '1286145991951106049', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145992307621889', 'Windows', '1286145991951106049', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145992370536449', '数据库', '0', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145993213591554', 'MySQL', '1286145992370536449', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145993502998529', 'MongoDB', '1286145992370536449', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145993570107394', '大数据', '0', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145993792405505', 'Hadoop', '1286145993570107394', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145993964371970', 'Spark', '1286145993570107394', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145994031480834', '人工智能', '0', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145994132144129', 'Python', '1286145994031480834', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145994266361857', '编程语言', '0', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
INSERT INTO `edu_subject` VALUES ('1286145994333470722', 'Java', '1286145994266361857', 0, '2020-7-23 03:47:55', '2020-7-23 03:47:55');
package com.cxx.entity;
/**
*
* 课程科目
*
*
* @author lwq
* @since 2020-07-15
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("edu_subject")
@ApiModel(value="Subject对象", description="课程科目")
public class Subject implements Serializable {
private static final long serialVersionUID=1L;
@ApiModelProperty(value = "课程类别ID")
@TableId(value = "id", type = IdType.ASSIGN_ID)
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;
}
package com.cxx.entity.vo;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SubjectVo implements Serializable {
private static final long serialVersionId=1L;
private String id;
private String title;
private List children=new ArrayList<>();
}
package com.cxx.service.impl;
/**
*
* 课程科目 服务实现类
*
*
* @author lwq
* @since 2020-07-15
*/
@Service
public class SubjectServiceImpl extends ServiceImpl implements SubjectService {
@Override
public List getTree() {
//查出所有分类
List subjectList = baseMapper.selectList(null);
List subjectsVo=new ArrayList<>();
//把所有分类数据塞到subjectsVo中
for (int i = 0; i < subjectList.size(); i++) {
Subject subject = subjectList.get(i);
SubjectsVo subjects = new SubjectsVo();
//把subject属性复制给subjects
BeanUtils.copyProperties(subject,subjects);
subjectsVo.add(subjects);
}
//查找所有一级分类
List list = subjectsVo.stream().filter(subjects -> subjects.getParentId().equals("0"))
.map((tree)->{
tree.setChildren(getChildrens(tree,subjectsVo));
return tree;
})
.collect(Collectors.toList());
return list;
}
//递归查找所有子分类
private List getChildrens(SubjectsVo root, List all) {
List children=all.stream().filter(subjects ->{
return subjects.getParentId().equals(root.getId());
//递归遍历
}).map(subjects -> {subjects.setChildren(getChildrens(subjects,all));
return subjects;
}).collect(Collectors.toList());
return children;
}
}
package com.cxx.controller.admin;
/**
*
* 课程科目 前端控制器
*
*
* @author lwq
* @since 2020-07-15
*/
@Api(description = "课程分类管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/education/subject")
public class SubjectController {
@ApiOperation("查询树形结构")
@GetMapping("getTree")
public R getChapterTree(){
List subjectList= subjectService.getTree();
if(subjectList==null){
return R.error().message("查询数据为空");
}else {
return R.ok().data("subjectList",subjectList);
}
}
}
{
"success": true,
"code": 20000,
"message": "成功",
"data": {
"subjectList": [
{
"id": "1286145989602295810",
"title": "Java",
"parentId": "0",
"children": [
{
"id": "1286145990072057857",
"title": "java简介",
"parentId": "1286145989602295810",
"children": []
},
{
"id": "1286145990294355970",
"title": "Java入门",
"parentId": "1286145989602295810",
"children": []
}
]
},
{
"id": "1286145990449545218",
"title": "Linux",
"parentId": "0",
"children": [
{
"id": "1286145990533431297",
"title": "linux简介",
"parentId": "1286145990449545218",
"children": []
},
{
"id": "1286145990780895234",
"title": "Linux入门",
"parentId": "1286145990449545218",
"children": []
}
]
},
{
"id": "1286145990915112962",
"title": "VUE",
"parentId": "0",
"children": [
{
"id": "1286145991112245250",
"title": "vue简介",
"parentId": "1286145990915112962",
"children": []
},
{
"id": "1286145991753973761",
"title": "vue入门",
"parentId": "1286145990915112962",
"children": []
}
]
},
{
"id": "1286145991951106049",
"title": "系统/运维",
"parentId": "0",
"children": [
{
"id": "1286145992110489601",
"title": "Linux",
"parentId": "1286145991951106049",
"children": []
},
{
"id": "1286145992307621889",
"title": "Windows",
"parentId": "1286145991951106049",
"children": []
}
]
},
{
"id": "1286145992370536449",
"title": "数据库",
"parentId": "0",
"children": [
{
"id": "1286145993213591554",
"title": "MySQL",
"parentId": "1286145992370536449",
"children": []
},
{
"id": "1286145993502998529",
"title": "MongoDB",
"parentId": "1286145992370536449",
"children": []
}
]
},
{
"id": "1286145993570107394",
"title": "大数据",
"parentId": "0",
"children": [
{
"id": "1286145993792405505",
"title": "Hadoop",
"parentId": "1286145993570107394",
"children": []
},
{
"id": "1286145993964371970",
"title": "Spark",
"parentId": "1286145993570107394",
"children": []
}
]
},
{
"id": "1286145994031480834",
"title": "人工智能",
"parentId": "0",
"children": [
{
"id": "1286145994132144129",
"title": "Python",
"parentId": "1286145994031480834",
"children": []
}
]
},
{
"id": "1286145994266361857",
"title": "编程语言",
"parentId": "0",
"children": [
{
"id": "1286145994333470722",
"title": "Java",
"parentId": "1286145994266361857",
"children": []
}
]
}
]
}
}