Commit a208fd3f authored by duanledexianxianxian's avatar duanledexianxianxian 😁

add:menu

parent 44c9f39a
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>demo-parent</artifactId>
<groupId>com.duanledexianxianxian.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>menu</artifactId>
</project>
\ No newline at end of file
package com.duanledexianxianxian.demo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author duanledexianxianxian
* @date 2020/1/12 23:51
* @since 1.0.0
*/
public class Application {
private static String[][] menus = {
{null, "0001", "用户管理", "/urlMgr"},
{"0001", "00010001", "用户管理-添加用户", "/urlMgr/add"},
{"0001", "00010002", "用户管理-修改用户", "/urlMgr/edit"},
{"0001", "00010003", "用户管理-删除用户", "/urlMgr/delete"},
{"0001", "00010004", "用户管理-查询用户", "/urlMgr/query"},
{null, "0002", "系统管理", "/systemMgr"},
{"0002", "00020001", "系统管理-系统管理0001", "/systemMgr/0001"},
{"00020001", "000200010001", "系统管理-系统管理0001-系统管理0001", "/systemMgr/0001/0001"},
{"000200010001", "0002000100010001", "系统管理-系统管理0001-系统管理0001-系统管理0001", "/systemMgr/0001/0001/0001"},
};
public static void main(String[] args) {
List<Menu> menuList = Lists.newArrayList();
Arrays.asList(menus).forEach(x -> {
Menu menu = new Menu();
menu.setParentMenuCode(x[0]);
menu.setMenuCode(x[1]);
menu.setMenuTitle(x[2]);
menu.setMenuUrl(x[3]);
menuList.add(menu);
});
menuList.forEach(x -> System.out.println(x.toString()));
MenuVO menuVO = getTreeMenuVO(menuList);
ObjectMapper mapper = new ObjectMapper();
try {
System.out.println(mapper.writeValueAsString(menuVO));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
private static MenuVO getTreeMenuVO(List<Menu> menuList) {
if (CollectionUtils.isEmpty(menuList)) {
return null;
}
List<MenuVO> menuVOList = Lists.newArrayList();
menuList.forEach(x -> {
MenuVO menuVO = new MenuVO();
BeanUtils.copyProperties(x, menuVO);
menuVOList.add(menuVO);
});
// 转成map
Map<String, MenuVO> menuMap = menuVOList.stream().collect(Collectors.toMap(MenuVO::getMenuCode, x -> x));
// 构建根节点
MenuVO menuVO = new MenuVO();
menuVO.setParentMenuCode(null);
menuVO.setMenuCode("root");
menuVO.setMenuTitle("根节点");
for (MenuVO menu : menuVOList) {
if (menuMap.containsKey(menu.getParentMenuCode())) {
if (menuMap.get(menu.getParentMenuCode()).getChildren() == null) {
menuMap.get(menu.getParentMenuCode()).setChildren(Lists.newArrayList());
}
menuMap.get(menu.getParentMenuCode()).getChildren().add(menu);
} else {
if (menuVO.getChildren() == null) {
menuVO.setChildren(Lists.newArrayList());
}
menuVO.getChildren().add(menu);
}
}
return menuVO;
}
}
package com.duanledexianxianxian.demo;
import lombok.Data;
/**
* 菜单
*
* @author duanledexianxianxian
* @date 2020/1/12 23:49
* @since 1.0.0
*/
@Data
public class Menu {
private String parentMenuCode;
private String menuCode;
private String menuTitle;
private String menuUrl;
}
package com.duanledexianxianxian.demo;
import lombok.Data;
import java.util.List;
/**
* 菜单
*
* @author duanledexianxianxian
* @date 2020/1/12 23:49
* @since 1.0.0
*/
@Data
public class MenuVO {
private String parentMenuCode;
private String menuCode;
private String menuTitle;
private String menuUrl;
private List<MenuVO> children;
}
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<modules> <modules>
<module>kafka</module> <module>kafka</module>
<module>menu</module>
</modules> </modules>
<parent> <parent>
...@@ -39,6 +40,12 @@ ...@@ -39,6 +40,12 @@
<artifactId>json-path</artifactId> <artifactId>json-path</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment