查询树形目录(内存遍历成树返回)

实体

java 复制代码
@Data
@TableName("dtp_sm_servicetype")
@ApiModel(value = "SmServicetype对象", description = "服务类型")
@EqualsAndHashCode(callSuper = true)
public class SmServicetype extends BaseEntity {

	@ApiModelProperty("服务类型名称")
	private String name;
	@ApiModelProperty("服务标题")
	private String title;
	@ApiModelProperty("服务分类描述")
	private String descinfo;
	@ApiModelProperty("服务分类父节点编号")
	private Long parentId;
	@ApiModelProperty("排序")
	private Integer orderIndex;

	// 存储下(子)级节点信息
	@TableField(exist = false)
	private List<SmServicetype> childrenList;
}

调用

java 复制代码
/**
 * 服务类型 分页
 */
@ApiOperation(value = "查询所有服务类型", notes = "传入smServicetype")
public R<List<SmServicetypeVO>> serviceTypeTree(SmServicetype smServicetype) {
	//查询所有树数据list
	List<SmServicetype> allList = smServicetypeService.list();
	// 内存中递归成树
	List<SmServicetype> typeTreeList = this.selectSmServicetypeTrees(allList);
	return R.data(SmServicetypeWrapper.build().listVO(typeTreeList));
}

private List<SmServicetype> selectSmServicetypeTrees(List<SmServicetype> smServicetypeList) {
	List<SmServicetype> collect = smServicetypeList.stream()
		.filter(item -> item.getParentId() == -1)   //ParentId=-1的节点为根节点(根据具体情况修改)    filter为过滤不符合条件的
		.map(item -> {
			item.setChildrenList(getChildrenList(item, smServicetypeList));
			return item;
		})
		.collect(Collectors.toList());
	return collect.size() == 0 ? smServicetypeList : collect;

}

private List<SmServicetype> getChildrenList(SmServicetype smServicetype, List<SmServicetype> smServicetypeList) {
	List<SmServicetype> collect = smServicetypeList.stream()
		.filter(item -> item.getParentId().equals(smServicetype.getId()))
		.map(item -> {
			item.setChildrenList(getChildrenList(item, smServicetypeList));
			return item;
		})
		.collect(Collectors.toList());
	return collect;
}

相关推荐
今天和Aboo结婚了吗15 小时前
【Broker一重启消息没了:一次RabbitMQ非持久化+没开Confirm的血亏事故】
java·rabbitmq·messagequeue·bug排查
daidaidaiyu20 小时前
一文学习 工作流开发 BPMN、 Flowable
java
SuniaWang21 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
sheji341621 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
m0_726965981 天前
面面面,面面(1)
java·开发语言
xuhaoyu_cpp_java1 天前
过滤器与监听器学习
java·经验分享·笔记·学习
程序员小假1 天前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha1 天前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34161 天前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端