有两张表,一张部门,一张人员。
部门有多级,有两级的,有三级的,两张表靠部门号联系。
我需要后台拼接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数据,这样也能形成树,百度后还是不明白这个怎么用的