java 三级菜单遍历
@Override public List<YjztCity> getYjzt(){ List<YjztCity> yjztCities = yjztCityMapper.selectByExample(null); //先获取你想要在哪些菜单范围内搜索 List<YjztCity> rootMenus = new ArrayList<>(); for (YjztCity menu : yjztCities) { //从最上级菜单开始展示 if(menu.getType().equals("2")){ rootMenus.add(menu); } } for (YjztCity rootMenu : rootMenus) { //把最上级菜单的子菜单获取到,子菜单的子菜单也会获取到(目前是三层,由于是递归调用,100层也没问题) List<YjztCity> child = getChild(rootMenu.getZfwqydm(), yjztCities); rootMenu.setChildren(child); } return rootMenus; } private List<YjztCity> getChild(String id,List<YjztCity> menus){ List<YjztCity> childList = new ArrayList<>(); for (YjztCity menu : menus) { if(menu.getZfwsjqydm().equals(id)){ //当菜单范围内的父id等于传入的id,获取该id的所有子列表 childList.add(menu); } } for (YjztCity menu : childList) { //当上面循环语句查询不到子菜单,长度为0,自然也就不会递归调用了 //将该id的子菜单便利,通过递归调用,给每一个子菜单赋值子菜单 List<YjztCity> child = getChild(menu.getZfwqydm(), menus); menu.setChildren(child); } if(childList.size()==0){ //子菜单的长度为0,返回null,null不会被jackson解析 return null; } return childList; }
数据库设计
标签:遍历,java,menu,List,childList,菜单,child,id From: https://www.cnblogs.com/xingmeng63/p/16890174.html