欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java递归菜单树转换成pojo对象

程序员文章站 2023-12-19 15:26:04
复制代码 代码如下:  package com.cjonline.foundation.authority.pojo;import java.util.array...
复制代码 代码如下:

  package com.cjonline.foundation.authority.pojo;
import java.util.arraylist;
import java.util.collections;
import java.util.iterator;
import java.util.list;
import org.apache.log4j.logger;
import com.cjonline.foundation.util.checknullempty;
/**
 * 实现递归的帮助类,最终的结果可以描述成如下:
 * 根(root)
 * --1(子系统1)
 * -----1.1
 * -------1.1.1
 * -------1.1.2
 * ------- ....
 * -----1.2
 * --2(子系统2)
 * -----2.1
 * -------2.1.1
 * -------2.1.2
 * ------- ....
 * -----2.2
 */
public class modellist implements java.io.serializable {
    private static final logger logger = logger.getlogger(modellist.class);
    private static final long serialversionuid = 6384598893693849820l;
    /**
     * model代表当前模块
     */
    private sysmodel model;
    /**
     * models代表当前模块下面的子模块,
     */
    private list<modellist> sublist = new arraylist<modellist>();
    /**
     * 菜单级别
     */
    private int flag = 0;
    public sysmodel getmodel() {
        return model;
    }
    public void setmodel(sysmodel model) {
        this.model = model;
    }
    public list<modellist> getsublist() {
        return sublist;
    }
    public void setsublist(list<modellist> sublist) {
        this.sublist = sublist;
    }
    public void setflag(int flag) {
        this.flag = flag;
    }
    public int getflag() {
        return flag;
    }
    /**
     * 递归方法根据传入的模块集合形成层级菜单
     */
    @suppresswarnings("unchecked")
    public modellist createtree2(list<sysmodel> ms) {
        //
        modellist node = new modellist();
        arraylist<sysmodel> fu = new arraylist<sysmodel>();// 用来存储parentid为空的父节点;
        arraylist<sysmodel> childs = new arraylist<sysmodel>();// 用来存储不是系统的模块
        // 将系统和模块菜单分开
        for (iterator<sysmodel> it = ms.iterator(); it.hasnext();) {
            sysmodel mode = (sysmodel) it.next();
            string parentid = mode.getparentid();
            if (parentid == null || parentid.equals("")) {
                fu.add(mode);
            } else {
                childs.add(mode);
            }
        }
        //由于是多个子系统,首先要找出有多少个子系统
        for (sysmodel model : fu) {
            modellist node1 = new modellist();
            node1.setflag(0);
            node1.setmodel(model);
            node.sublist.add(node1);
            appendchild(node1, childs);
        }
        return node;
    }
    /**
     * node节点 childs为所以系统下的子节点
     */
    public void appendchild(modellist node, list<sysmodel> childs) {
        if (node != null) {
            string systemid = node.getmodel().getsystemid();
            string smid = node.getmodel().getsysmoduleid();
            int flag = node.getflag();
            if (childs != null && childs.size() > 0) {
                for (sysmodel model : childs) {
                    string systemid2 = model.getsystemid();
                    string parentid2 = model.getparentid();
                    if (systemid.equals(systemid2)) {
                        if (parentid2.equals(smid)) {
                            modellist child = new modellist();
                            child.setmodel(model);
                            child.setflag(flag + 1);
                            node.getsublist().add(child);
                            appendchild(child, childs);
                        }
                    }
                }
            }
        }
    }
}
 

上一篇:

下一篇: