生成树形菜单-通过两个map集合

1.数据库sql语句

 create table company(
   company_id           int                            not null auto_increment,
   parent_company_id    int                            null,
   name                 varchar(128)                   null,
   phone                varchar(16)                    null,
   constraint PK_COMPANY primary key clustered (company_id)
);

2.查询语句DAO

public List getAllCompany() {
        List companies=null;
        String hql = "select c from Company c";
        Query query=getEntityManager().createQuery(hql);
        companies=query.getResultList();
        return companies;
    }

3.创建返回实体

public class CompanyTreeVo{
    private Integer companyId;
    private String name;
    private String phone;
    private List children;//...getter/setter..}

3.组装成tree-Service

        

public CompanyTreeVo findAllCompanyName() {
        List companies = companyDao.getAllCompany();
        if(companies!=null&&companies.size()>0) {
            Map objectMap = new HashMap<>();
            Map> childMap = new HashMap<>();
            Integer rootId = CompanyFactory.checkTree(companies, childMap, objectMap);
            CompanyTreeVo treeVo = CompanyFactory.initTreeCompany(rootId, childMap, objectMap);
            return treeVo;
        }else{
            return null;
        }}

    public static Integer checkTree(List companies,Map> childMap,Map objectMap){
        Map maps=new HashMap<>();
        Integer rootId=null;
        CompanyMenuVo companyMenuVo=null;
        if(companies!=null &&companies.size()>0){//判断查询过来的值不为空
            for(Company company :companies){//遍历查询的值
                objectMap.put(company.getCompanyId(),company);
                Integer parentId = company.getParentCompanyId();//获取每个对象的父id
                if(parentId != null){//如果父id不为null则进行下面操作
                    List list = childMap.get(parentId);//获取父id的集合是否初始化
                    if(list==null){
                        list = new ArrayList<>();
                    }
                    //将公司id添加集合中
                    list.add(company.getCompanyId());
                    //将集合添加到key为父id的map中
                    childMap.put(parentId,list);
                }else{//父id初始化操作
                    rootId=company.getCompanyId();
                }
            }
        }
        return rootId;
    }

    public static CompanyTreeVo initTreeCompany(Integer rootId,Map> childMap, Map objectMap){
        CompanyTreeVo rootVo = new CompanyTreeVo();
        List children = new ArrayList<>();
        Company company = objectMap.get(rootId);
        //将实体对象转换为vo对象
        initCompanyTreeVo(rootVo, company);
        List childList = childMap.get(rootId);
        if(childList!=null&&childList.size()>0){
            for(Integer  childId:childList){
                //递归将数据转入list集合中
                CompanyTreeVo childTreeVo = initTreeCompany(childId, childMap, objectMap);
                childTreeVo.setParentCompanyName(company.getName());
                children.add(childTreeVo);
            }
        }
        rootVo.setChildren(children);
        return rootVo;
    }

    //将实体对象转换为vo对象
    public static void initCompanyTreeVo(CompanyTreeVo treeVo,Company company){
        treeVo.setCompanyId(company.getCompanyId());
        treeVo.setName(company.getName());
        treeVo.setParentCompanyId(company.getParentCompanyId());
    }

 

 

 

 

 

    

 

 

你可能感兴趣的:(java框架)