在工作中会将菜单、部门数据转成树结构的json方便前端展示树
1、从数据库把具有父子(id, parent_id)结构的数据从数据库查出来
省略查询步骤,这里新建两个常见的父子结构的菜单和部门实体
public class Dept {
private String deptId;
private String deptName;
private String parentDept;
public Dept(String deptId, String deptName, String parentDept) {
this.deptId = deptId;
this.deptName = deptName;
this.parentDept = parentDept;
}
}
public class Menu {
private String id;
private String name;
private String icon;
private String parentId;
public Menu(String id, String name, String icon, String parentId) {
this.id = id;
this.name = name;
this.icon = icon;
this.parentId = parentId;
}
}
2、通过递归将集合数据转成树对象
import org.springframework.cglib.beans.BeanMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataTree {
private String childrenName = "children";
private String key = "id";
private String parentKey = "parentId";
public DataTree() {
}
public DataTree(String childrenName, String key, String parentKey) {
this.childrenName = childrenName;
this.key = key;
this.parentKey = parentKey;
}
private List<Map> parseMap(List data){
List<Map> beanMaps = new ArrayList<Map>();
if(data.isEmpty()){
return null;
}
for (Object obj: data) {
BeanMap beanMap = BeanMap.create(obj);
Map map = new HashMap(beanMap);
beanMaps.add(map);
}
return beanMaps;
}
public List<Map> createTree(List data) {
List<Map> treeMap = parseMap(data);
List<Map> tree = new ArrayList<Map>();
for (Map node : treeMap) {
if (null == node.get(parentKey)) {
List<Map> child = createTree((String) node.get(key), data);
if(!child.isEmpty()){
node.put(childrenName, child);
}
tree.add(node);
}
}
return tree;
}
public List<Map> createTree(String parentNode, List data) {
List<Map> treeMap = parseMap(data);
List<Map> tree = new ArrayList<Map>();
for (Map node : treeMap) {
if(parentNode.equals(node.get(parentKey))){
List<Map> child = getChild(node, treeMap);
if(!child.isEmpty()){
node.put(childrenName, child);
}
tree.add(node);
}
}
return tree;
}
private List<Map> getChild(Map parentNode, List<Map> data) {
List<Map> tree = new ArrayList<Map>();
for (Map node : data) {
if(null != node.get(parentKey) &&
!node.get(key).equals(node.get(parentKey)) &&
!(node.get(key).equals(parentNode.get(parentKey)) && node.get(parentKey).equals(parentNode.get(key))) &&
parentNode.get(key).equals(node.get(parentKey))){
List<Map> child = getChild(node, data);
if(!child.isEmpty()){
node.put(childrenName, child);
}
tree.add(node);
}
}
return tree;
}
}
3、测试
public class TreeTest {
public static void main(String[] args) {
List<Menu> menuTree = new ArrayList<Menu>();
menuTree.add(new Menu("1", "首页", "no", null));
menuTree.add(new Menu("2", "用户管理", "no", null));
menuTree.add(new Menu("2-1", "修改头像", "no", "2"));
menuTree.add(new Menu("2-2", "修改邮箱", "no", "2"));
menuTree.add(new Menu("3", "系统管理", "no", null));
menuTree.add(new Menu("3-1", "菜单", "no", "3"));
menuTree.add(new Menu("3-2", "角色", "no", "3"));
List<Dept> deptTree = new ArrayList<Dept>();
deptTree.add(new Dept("1", "人事部", "0"));
deptTree.add(new Dept("2", "信息部", "0"));
deptTree.add(new Dept("2-1", "研发部", "2"));
deptTree.add(new Dept("2-2", "测试部", "2"));
deptTree.add(new Dept("2-3", "交付部", "2"));
DataTree tree = new DataTree();
List menuTreeDate = tree.createTree(menuTree);
System.out.println(JSON.toJSONString(menuTreeDate));
System.out.println(JSONArray.toJSONString(menuTreeDate));
DataTree tree2 = new DataTree("child", "deptId", "parentDept");
List deptTreeDate = tree2.createTree("0", deptTree);
System.out.println(JSON.toJSONString(deptTreeDate));
System.out.println(JSONArray.toJSON(deptTreeDate));
}
}
|