首页 > 其他分享 >简单树结构生成

简单树结构生成

时间:2024-01-25 09:44:23浏览次数:21  
标签:String eduSubject 树结构 List value 生成 private 简单 id

实体:
@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

相关文章

  • jdk的keytool生成jks和获取jks的信息,公匙
     1.生成jks。执行命令:keytool-genkeypair-aliasmytest-keyalg RSA-keypassmypass-keystoremytest.jks-storepassmypass-keystorejks文件保存路径生成的mytest.jks证书中包含我们的密钥:公钥和私钥。2.利用"keytool-list-v-keystoretest.jks"查看JKS中生......
  • JAVA XStream简单使用
    XStreamxStream=newXStream(newStaxDriver());xStream.addPermission(AnyTypePermission.ANY);//不加后面转实体变异常xStream.processAnnotations(resultDTO.getClass());resultDTO=(ResultDTO)xStream.fromXML(result); 1、实体类设置别名  @XStreamAlias("Ma......
  • 生成八字
    条件:农历(阴历);出生时辰 万年历查询: (例:辛巳年、寅庚月、丁巳日) 干支纪时法:http://114.xixik.com/chinese-era/(例:丙午时)时辰对应:现-晚11:00~01:00  古-子时(2小时=1时辰)  仅为个人学习 ......
  • CF-1184-E3-最小生成树+倍增+并查集
    1184-E3题目大意给定一个\(n\)个点,\(m\)条边的无向图,边带权。对于每条边,你需要找到最大值\(x\),使得把这条边的权值修改为\(x\)后能够出现在最小生成树上。Solution先把整个图的最小生成树弄出来,然后将边分为树边以及非树边来考虑:非树边:对于一个非树边连接了\(x\)和\(y\)的......
  • MFC 简单的绘图用法
    ▲效果在OnDraw函数中://CMFCApplication1View绘图voidCMFCApplication1View::OnDraw(CDC*pDC){CMFCApplication1Doc*pDoc=GetDocument();ASSERT_VALID(pDoc);if(!pDoc)return;/*a)定义画家b)定义画笔(画刷)CPen,CBrush......
  • thinkphp 路由分组 url生成
    路由分组route/app.php//路由分组相同规则的路由方一起分组//Route::group('blog',function(){//Route::rule(':id','blog/index');//Route::rule('index','blog/login');//});//或者//Route::group(function(){/......
  • Oracle数据类型的简单学习之一
    Oracle数据类型的简单学习之一背景因为信创安可替代的发展有很多项目提到了数据库切换到国产数据库的要求.一般情况是要求从Oracle/SQLServer迁移到国产的:达梦/瀚高/人大金仓/南大通用等数据库.但是因为Oracle作为数据库领域No.1的存在他对SQL的规范标准支持的并不......
  • 树结构及前中后续遍历
    publicclassTree{publicstaticvoidmain(String[]args){Treeroot=newTree(50);Tree.insert(root,30);Tree.insert(root,60);Tree.insert(root,70);Tree.insert(root,100);Tree.insert(root,80);......
  • 国产车灯芯片9V 12V 24V 48V 60V 90V 外围简单-H5022L
    车灯芯片是用于控制车灯光系统的集成电路,其主要功能是管理车辆的照明系统。以下是车灯芯片的一些优点:节能环保:一些先进的车灯芯片采用高效能的LED技术,相比传统照明系统更为节能,有助于减少车辆能耗,降低碳排放。多功能性:车灯芯片能够支持多种灯光模式,包括近光、远光、转向灯等,实现多......
  • MFC 简单的事件测试
    View的WM_LBUTTONDOWN事件消息。voidCMFCApplication1View::OnLButtonDown(UINTnFlags,CPointpoint){CStringstr;str.Format(TEXT("x=%d,y=%d"),point.x,point.y);MessageBox(str);CView::OnLButtonDown(nFlags,point);}View......