使用递归方法建树
controller
@GetMapping(value = "/tree/menu")
public HttpResponseTemp<?> getTreeMenu() {
return pueService.getTreeMenu();
}
service.Impl
public HttpResponseTemp<?> getTreeMenu() {
List<Pue> pueList = pueBiz.selectAll();
if (CollectionUtil.isEmpty(pueList)) {
return ResultStat.OK.wrap(null, "结果为空");
}
List<ComponentInstance> componentInstances = componentBiz.selectAllComponentInstance();
if (CollectionUtil.isEmpty(componentInstances)) {
return ResultStat.OK.wrap(null, "结果为空");
}
Map<Integer, Integer> collect = componentInstances.stream().filter(Objects::nonNull).collect(Collectors.toMap(ComponentInstance::getId, ComponentInstance::getParentId));
Set<Integer> componentInstanceIdSet = new HashSet<>();
pueList.stream().filter(Objects::nonNull).forEach(s -> {
this.getComponentInstanceList(s.getComponentInstanceId(), componentInstanceIdSet, collect);
});
componentInstances = componentInstances.stream().filter(Objects::nonNull).filter(s -> componentInstanceIdSet.contains(s.getId())).collect(Collectors.toList());
List<ConfigurationDesignMenuDTO> configurationDesignMenuDTOS = ConfigurationDesignMenuDTO.list2Tree(ConfigurationDesignMenuDTO.of(componentInstances));
return ResultStat.OK.wrap(configurationDesignMenuDTOS);
}
/**
* 递归获取主键实例的集合
* @param componentInstanceId
* @param componentInstanceIdSet
* @param collect
*/
private void getComponentInstanceList(int componentInstanceId, Set<Integer> componentInstanceIdSet, Map<Integer, Integer> collect) {
Integer parentId = collect.get(componentInstanceId);
componentInstanceIdSet.add(componentInstanceId);
if (parentId > 0) {
getComponentInstanceList(parentId, componentInstanceIdSet, collect);
}
}
ConfigurationDesignMenuDTO类
public class ConfigurationDesignMenuDTO {
private String key;
private int id;
private String title;
private int componentId;
private int parentId;
private List<ConfigurationDesignMenuDTO> children = new ArrayList<>();
public static ConfigurationDesignMenuDTO of(ComponentInstance componentInstance) {
if (componentInstance == null) {
return null;
}
ConfigurationDesignMenuDTO configurationDesignMenuDTO = new ConfigurationDesignMenuDTO();
BeanUtil.copyNotNullProperties(componentInstance, configurationDesignMenuDTO);
configurationDesignMenuDTO.setTitle(componentInstance.getName());
configurationDesignMenuDTO.setKey("" + configurationDesignMenuDTO.getId());
return configurationDesignMenuDTO;
}
public static List<ConfigurationDesignMenuDTO> of(List<ComponentInstance> componentInstances) {
if (CollectionUtils.isEmpty(componentInstances)) {
return null;
}
return componentInstances.stream().map(ConfigurationDesignMenuDTO::of).collect(Collectors.toList());
}
/**
* 使用递归方法建树
*
* @param treeNodes 树节点
* @return 数据树列表
*/
public static List<ConfigurationDesignMenuDTO> list2Tree(List<ConfigurationDesignMenuDTO> treeNodes) {
if (CollectionUtils.isEmpty(treeNodes)) {
return Collections.emptyList();
}
boolean existRootNode = false;
//初始化一个新的列表
List<ConfigurationDesignMenuDTO> newTree = new ArrayList<>();
for (ConfigurationDesignMenuDTO treeNode : treeNodes) {
//选择根节点数据开始找子节点
if (isRootNode(treeNode)) {
newTree.add(findChildren(treeNode, treeNodes));
existRootNode = true;
}
}
//也可能大家都是根节点
if (!existRootNode) {
return treeNodes;
}
return newTree;
}
/**
* 判断节点是否是根节点
*
* @param checkNode
* @return
*/
private static boolean isRootNode(ConfigurationDesignMenuDTO checkNode) {
//判断checkNode是不是有父节点. 有,则返回false
if (checkNode.getParentId() == 0) {
return true;
}
return false;
}
/**
* 递归查找子节点
*
* @param treeNodes
* @return
*/
private static ConfigurationDesignMenuDTO findChildren(ConfigurationDesignMenuDTO parentNode, List<ConfigurationDesignMenuDTO> treeNodes) {
List<ConfigurationDesignMenuDTO> children = parentNode.getChildren();
for (ConfigurationDesignMenuDTO item : treeNodes) {
//找子节点,判断parentNode是不是有子节点
if (parentNode.getId() == item.getParentId()) {
item.setKey(parentNode.getKey() + "-" + item.getId());
children.add(findChildren(item, treeNodes));
}
}
return parentNode;
}
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getComponentId() {
return componentId;
}
public void setComponentId(int componentId) {
this.componentId = componentId;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public List<ConfigurationDesignMenuDTO> getChildren() {
return children;
}
public void setChildren(List<ConfigurationDesignMenuDTO> children) {
this.children = children;
}
mapper
@Select("SELECT * from pue where removed =0 ORDER BY id DESC ")
List<Pue> selectAll();
@Select("SELECT * from component_instance")
List<ComponentInstance> selectAll();
mysql
CREATE TABLE `pue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(1024) DEFAULT NULL,
`componentInstanceId` int(11) DEFAULT NULL,
`creator` int(11) DEFAULT NULL,
`createTime` bigint(20) DEFAULT NULL,
`updateTime` bigint(20) DEFAULT NULL,
`deleteTime` bigint(20) DEFAULT NULL,
`removed` int(2) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
CREATE TABLE `component_instance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentId` int(11) DEFAULT NULL,
`componentId` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`canvasData` mediumtext,
`backgroundPicture` varchar(1024) DEFAULT NULL,
`locationData` mediumtext NOT NULL,
`propertyData` mediumtext NOT NULL,
`oidSerialNumber` int(4) DEFAULT NULL,
`dynamicRingHostId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=61 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;