实体:
@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;
}
//subject树结构VO
@Data
public class Subject {
private String id;
private String title;
private List<Subject> children = new ArrayList<>();
}
实现:
@Override
public List<Subject> getAllSubject() {
List<Subject> subjects = new ArrayList<>();
//查询所有数据
List<EduSubject> all = baseMapper.selectList(null);
//查询所有的一级区域
QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
wrapper.eq("parent_id", 0);
List<EduSubject> oneEduSubjectList = baseMapper.selectList(wrapper);
for (EduSubject eduSubject : oneEduSubjectList) {
Subject subject = new Subject();
BeanUtils.copyProperties(eduSubject, subject);
//查询子节点
List<Subject> children = findChildrens(eduSubject.getId(), all);
subject.setChildren(children);
subjects.add(subject);
}
return subjects;
}
public List<Subject> findChildrens(String id, List<EduSubject> all) {
List<Subject> childList = new ArrayList<>();
if (id != null && all != null) {
for (EduSubject eduSubject : all) {
if (Objects.equals(eduSubject.getParentId(), id)) {
Subject subject1 = new Subject();
BeanUtils.copyProperties(eduSubject, subject1);
subject1.setChildren(findChildrens(subject1.getId(), all));
childList.add(subject1);
}
}
}
return childList;
}
可以改为Lambda表达式的写法。
标签:String,eduSubject,树结构,List,value,生成,private,简单,id From: https://www.cnblogs.com/sensenh/p/17986356