管理端经常会用到树形菜单这种数据格式,
oracle
有现成的函数实现起来还是比较简单,但是mysql
没有这种函数,如果自己设置函数的话,由于长度的限制,太多的数据就没办法遍历了
示例1:【使用实体类】使用代码的方式遍历出树形格式
数据库获取list列表,通过一定的条件筛选
1、实体类
@Data
@Builder
public class Menu {
/**
* id
*/
public Integer id;
/**
* 名称
*/
public String name;
/**
* 父id ,根节点为0
*/
public Integer parentId;
/**
* 子节点信息
*/
public List<Menu> childList;
public Menu(Integer id, String name, Integer parentId) {
this.id = id;
this.name = name;
this.parentId = parentId;
}
public Menu(Integer id, String name, Integer parentId, List<Menu> childList) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.childList = childList;
}
}
2、递归组装树形结构
@Test
public void testtree(){
//模拟从数据库查询出来
List<Menu> menus = Arrays.asList(
new Menu(1,"根节点",0),
new Menu(2,"子节点1",1),
new Menu(3,"子节点1.1",2),
new Menu(4,"子节点1.2",2),
new Menu(5,"根节点1.3",2),
new Menu(6,"根节点2",1),
new Menu(7,"根节点2.1",6),
new Menu(8,"根节点2.2",6),
new Menu(9,"根节点2.2.1",7),
new Menu(10,"根节点2.2.2",7),
new Menu(11,"根节点3",1),
new Menu(12,"根节点3.1",11)
);
//获取父节点
List<Menu> collect = menus.stream().filter(m -> m.getParentId() == 0).map(
(m) -> {
m.setChildList(getChildrens(m, menus));
return m;
}
).collect(Collectors.toList());
System.out.println("-------转json输出结果-------");
System.out.println(JSON.toJSON(collect));
}
/**
* 递归查询子节点
* @param root 根节点
* @param all 所有节点
* @return 根节点信息
*/
private List<Menu> getChildrens(Menu root, List<Menu> all) {
List<Menu> children = all.stream().filter(m -> {
return Objects.equals(m.getParentId(), root.getId());
}).map(
(m) -> {
m.setChildList(getChildrens(m, all));
return m;
}
).collect(Collectors.toList());
return children;
}
结果
示例2:【使用Map】
public static void main(String[] args) {
List<Map> list = new ArrayList<>();
Map map1 = new HashMap();
map1.put("id", 1);
map1.put("pid", 0);
map1.put("name", "名字1");
Map map2 = new HashMap();
map2.put("id", 2);
map2.put("pid", 0);
map2.put("name", "名字2");
Map map3 = new HashMap();
map3.put("id", 3);
map3.put("pid", 1);
map3.put("name", "名字3");
Map map4 = new HashMap();
map4.put("id", 4);
map4.put("pid", 2);
map4.put("name", "名字4");
Map map5 = new HashMap();
map5.put("id", 5);
map5.put("pid", 1);
map5.put("name", "名字5");
Map map6 = new HashMap();
map6.put("id", 6);
map6.put("pid", 2);
map6.put("name", "名字6");
Map map7 = new HashMap();
map7.put("id", 7);
map7.put("pid", 3);
map7.put("name", "名字7");
Map map8 = new HashMap();
map8.put("id", 8);
map8.put("pid", 4);
map8.put("name", "名字8");
Map map9 = new HashMap();
map9.put("id", 9);
map9.put("pid", 3);
map9.put("name", "名字9");
Map map10 = new HashMap();
map10.put("id", 10);
map10.put("pid", 4);
map10.put("name", "名字10");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
list.add(map6);
list.add(map7);
list.add(map8);
list.add(map9);
list.add(map10);
//获取父节点
List<Map> collect = list.stream().filter(m -> Objects.equals(m.get("pid"),0)).map(
(m) -> {
m.put("children",getChildrens(m, list));
return m;
}
).collect(Collectors.toList());
System.out.println("ok");
}
/**
* 递归查询子节点
* @param root 根节点
* @param all 所有节点
* @return 根节点信息
*/
private static List<Map> getChildrens(Map root, List<Map> all) {
List<Map> children = all.stream().filter(m -> {
return Objects.equals(m.get("pid"), root.get("id"));
}).map(
(m) -> {
m.put("children", getChildrens(m, all));
return m;
}
).collect(Collectors.toList());
return children;
}