Sakura_ZHJ 2015-07-24 01:14 采纳率: 20%
浏览 2429

【新人】关于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数据,这样也能形成树,百度后还是不明白这个怎么用的

  • 写回答

1条回答 默认 最新

  • Go 旅城通票 2015-07-24 03:58
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。