• 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

  • 相关阅读:
    《深入V8引擎-第01课》
    《各 JavaScript 引擎的简介,及相关资料》
    《【前端性能】必须要掌握的原生JS实现JQuery》
    《[iOS][OC] 开发利器:控制器传送门VCPicker(附demo)》
    《iOS 上的 CSS 样式协议 VKCssProtocol》
    ICML 2019论文录取Top100:谷歌霸榜
    进阶!自然语言处理背后的数据科学
    学界!关于GAN的灵魂七问
    如何优化深度学习模型
    从DeepNet到HRNet,这有一份深度学习“人体姿势估计”全指南
  • 原文地址:https://www.cnblogs.com/guangxiang/p/10621570.html
Copyright © 2020-2023  润新知