@Override
public List<SubjectNodeTreeVo> getAllSubjects() {
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.select("id","title","parent_id");
List<EduSubject> eduSubjects = baseMapper.selectList(wrapper);
List<SubjectNodeTreeVo> nodes = new ArrayList<>();
eduSubjects.forEach(eduSubject -> {
SubjectNodeTreeVo node = new SubjectNodeTreeVo();
BeanUtils.copyProperties(eduSubject,node);
nodes.add(node);
});
Map<String, List<SubjectNodeTreeVo>> children = nodes.stream().filter(node -> (!node.getParentId().equals("0")))
.collect(Collectors.groupingBy(node -> node.getParentId()));
nodes.forEach(node -> node.setChildren(children.get(node.getId())));
return nodes.stream().filter(node -> node.getParentId().equals("0")).collect(Collectors.toList());
}
package com.njxzc.eduService.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@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;
}
package com.njxzc.eduService.entity.vo;
import lombok.Data;
import java.util.List;
@Data
public class SubjectNodeTreeVo {
private String parentId;
private String title;
private String id;
private List<SubjectNodeTreeVo> children;
}