开发者社区> 问答> 正文

【新人】关于ztree,后台怎么查询可以快速形成一棵部门人员树?

有两张表,一张部门,一张人员。
部门有多级,有两级的,有三级的,两张表靠部门号联系。

我需要后台拼接json,在前台用ztree展示部门人员树,尝试用for循环写了下,查询效率低的可怕,一分钟才显示出来。

    public void getTree(){

        System.out.println("getTree");
        String zdjbbm=departmentService.queryFirstDepartment().get(0).getDepartmentGuid();
        List<Department> list= departmentService.queryDepartmentsBySupId(zdjbbm);//最大级别部门
        PrintWriter out = null;
        JSONArray jsonArray =  new JSONArray();

        for(Department d:list){
            JSONObject json=new JSONObject();
            json.put("id", d.getDepartmentGuid());
            json.put("pId", "-1");
            json.put("type", "-1");//一级部门
            json.put("name", d.getDepartmentName());
            json.put("open", false);
            json.put("src", "");
            jsonArray.add(json);

            List<Department> list2=departmentService.getDepartmentByPid(d.getDepartmentGuid());
            if(!list2.isEmpty()&&list2.size()>0){
                for(Department d2:list2){
                    JSONObject json2=new JSONObject();
                    json2.put("id", d2.getDepartmentGuid());
                    json2.put("pId", d.getDepartmentGuid());
                    json2.put("type", "0");//二级部门
                    json2.put("name", d2.getDepartmentName());
                    json2.put("open", false);
                    json2.put("src", "");
                    jsonArray.add(json2);



                    List<Department> list3=departmentService.getDepartmentByPid(d2.getDepartmentGuid());
                    if(!list3.isEmpty()&&list3.size()>0){
                        for(Department d3:list3){
                            JSONObject json3=new JSONObject();
                            json3.put("id", d3.getDepartmentGuid());
                            json3.put("pId", d2.getDepartmentGuid());
                            json3.put("type", "0");//三级部门
                            json3.put("name", d3.getDepartmentName());
                            json3.put("open", false);
                            json3.put("src", "");
                            jsonArray.add(json3);

                            //三级部门下的人员
                            List<Employee> emps3=employeeService.getAllEmployeeById(d3.getDepartmentGuid());
                            if(!emps3.isEmpty()&&emps3.size()>0){
                                for(Employee e:emps3){
                                    JSONObject jsonemp=new JSONObject();
                                    jsonemp.put("id", e.getEmployeeGuid());
                                    jsonemp.put("pId", d3.getDepartmentGuid());
                                    jsonemp.put("type", "1");//三级部门人员
                                    jsonemp.put("name", e.getEmployeeName()+"("+e.getEmployeeMobile()+")");
                                    jsonemp.put("open", false);
                                    jsonemp.put("src", "");
                                    jsonArray.add(jsonemp);
                                }
                            }


                        }
                    }

                }


            }
        }
        String result = jsonArray.toString();

        try {
            this.getRequest().setCharacterEncoding("utf-8");
            out = getResponse().getWriter();
            out.write(result);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 }

这是我开始写的,查的太慢了,但是看了别人的都是异步查询,看不懂,希望有大神能给个同步树的思路(可以的话同步,异步都能解释下)

ztree我只用过pid,id这样形成树,我看大神好像用了个 $("#tree").treeview(o),o是自己定义的json数据,这样也能形成树,百度后还是不明白这个怎么用的

展开
收起
杨冬芳 2016-07-05 15:02:59 5089 0
1 条回答
写回答
取消 提交回答
  • IT从业

    jquery zTree 入门,从后台获取数据zTree分层显示,分层联动示例

    2019-07-17 19:51:30
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载