Java生成树关系的菜单

1.菜单bean

public class Menu {

private String id;

private String menuname;

private String parentid;

private String sort;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id == null ? null : id.trim();
}

public String getMenuname() {
return menuname;
}

public void setMenuname(String menuname) {
this.menuname = menuname == null ? null : menuname.trim();
}

public String getParentid() {

return parentid;
}

public void setParentid(String parentid) {
this.parentid = parentid == null ? null : parentid.trim();
}

public Integer getSort() {

return sort;
}

public void setSort(Integer sort) {
this.sort = sort;
}

}

2.菜单树bean

import java.util.ArrayList;
import java.util.List;

import menu;

public class MenuTreeNew {

private List<MenuTreeNew> children;

private String id;

private String menuname;

private String parentid;

private Integer sort;

public MenuTreeNew(String id, String menuname, String parentid, int sort) {

super();
this.id = id;
this.menuname = menuname;
this.parentid = parentid;
this.sort = sort;
}

public List<MenuTreeNew> getChildren() {
return children;
}

public void setChildren(List<MenuTreeNew> children) {
this.children = children;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getMenuname() {
return menuname;
}

public void setMenuname(String menuname) {
this.menuname = menuname;
}

public String getParentid() {
return parentid;
}

public void setParentid(String parentid) {
this.parentid = parentid;
}

public Integer getSort() {
return sort;
}

public void setSort(Integer sort) {
this.sort = sort;
}

public MenuTreeNew(List<MenuTreeNew> children, String id, String menuname, String parentid, int sort) {
super();
this.children = children;
this.id = id;
this.menuname = menuname;
this.parentid = parentid;
this.sort = sort;
}


/**
* 部门树构建
* @author jgx
* @date 2019年3月22日
* @version 1.0
*/
public static class Builder{
private List<Menu> menus;
public Builder(List<Menu> menus) {
super();
this.menus = menus;
}

/**
* 查找孩子节点
* @param node
*/
private void findChildren(MenuTreeNew node){
if (node == null)
return;
if (node.getChildren() == null)
node.setChildren(new ArrayList<MenuTreeNew>());

for(Menu depart : menus){
if(depart.getParentid().equals(node.getId()) ){
MenuTreeNew dt = new MenuTreeNew(new ArrayList<MenuTreeNew>(),depart.getId(),depart.getMenuname(),depart.getParentid(),depart.getSort());
node.getChildren().add(dt);
findChildren(dt);
}
}
}

/**
* 构建
* @return 返回树菜单
*/
public List<MenuTreeNew> build(){
if (menus == null || menus.size() == 0)
return null;

List<MenuTreeNew> trees = new ArrayList<MenuTreeNew>();
//构建root
for(Menu depart : menus){
if (depart.getParentid().equals("0"))
trees.add(new MenuTreeNew(depart.getId(),depart.getMenuname(),depart.getParentid(),depart.getSort()));
}
//递归查找子节点
for(MenuTreeNew dt:trees){
findChildren(dt);
}
return trees;
}
}
}

3.controller

@RequestMapping(value="/list", method = RequestMethod.GET)
@ResponseBody
public ReponseResult<List<Menu>> list(){
List<Menu> menus = menuMapper.selectByExample(null);
return ReponseResult.success(menus);
}

原创代码,引用请标注出处  https://www.cnblogs.com/guangxiang/p/10621570.html

原文地址:https://www.cnblogs.com/guangxiang/p/10621570.html