编写树形接口demo
下面为编写树形接口的代码,实际项目已使用,可直接进行复制粘贴
树形接口
/**
* 树形列表
* @return
*/
public List<CourseClassification> tree(CourseClassification courseClassification);
接口实现类
。
@Override
public List<CourseClassification> tree(CourseClassification courseClassification) {
// 所有的列表数据
List<CourseClassification> list = baseMapper.selectList(null);
// 编写正常的树形列表
List<CourseClassification> level1Menus = list.stream().filter(categoryEntity -> categoryEntity.getParentId() == 0)
.map((menu) -> {
menu.setChildren(getChildrens(menu, list));
return menu;
}).collect(Collectors.toList());
return level1Menus;
}
/**
* 获取子菜单
*
* @param root
* @param all
* @return
*/
private List<CourseClassification> getChildrens(CourseClassification root, List<CourseClassification> all) {
List<CourseClassification> children = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentId().equals(root.getId());
}).map(categoryEntity -> {
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
}).collect(Collectors.toList());
return children;
}