组装树数据,递归的一点感想

原代码:

public JSONArray addJSON(Integer id, JSONArray array, String type) {
		Integer pId = null;
		if(type != null && type.equals("Group")) {
			return returnAsJsonFromGroup(array, id);
		} 
		DetachedCriteria dc1 = DetachedCriteria.forClass(TbDepartmentTree.class);
		if(id == null) {
			dc1.add(Restrictions.isNull("tbDepartmentTree.deparetId"));
		} else {
			dc1.add(Restrictions.eq("tbDepartmentTree.deparetId", id));
		}
		dc1.addOrder(Order.asc("sequence"));
		List<TbDepartmentTree> t1 = tbDepartmentTreeService.findAllByCriteria(dc1);
		if(t1 != null) {
			for(TbDepartmentTree t : t1) {
				JSONObject json = new JSONObject();
				json.put("id", t.getDeparetId());
				if(id == null) {
					if(t.getSequence() == 1) {
						pId = t.getDeparetId();
						json.put("pId", 0);
					} else {
						json.put("pId", pId);
					}
					json.put("open", false);
					json.put("nochecked", true);
				} else {
					json.put("pId", id);
				}
				json.put("name", t.getDeparetName());
				array.add(json);
				if(type != null && type.equals("User")) {
					array = returnAsJsonFromStaff(array, t.getDeparetId());
				}
				addJSON(t.getDeparetId(), array, type);
			}
		}
    	return array;
	}

现在的代码:

public void treeAsJson() {
		Integer pId = null;
		JSONArray array = new JSONArray();
		HttpServletRequest request = ServletActionContext.getRequest();
		String type = request.getParameter("type");
		if(type != null && type.equals("Group")) {
			 array = getAllVG(array);
		} else {
			if(tdts == null) {
				tdts = getAllDept();
			}
			for(TbDepartmentTree t : tdts) {
				JSONObject json = new JSONObject();
				json.put("id", t.getDeparetId());
				if(t.getTbDepartmentTree() == null) {
					if(t.getSequence() == 1) {
						pId = t.getDeparetId();
						json.put("pId", 0);
					} else {
						json.put("pId", pId);
					}
				} else {
					if(t.getDeparetId().equals(t.getTbDepartmentTree().getDeparetId())) {
						json.put("pId", t.getDeparetId());
					} else {
						json.put("pId", t.getTbDepartmentTree().getDeparetId());
					}
				}
				json.put("open", false);
				json.put("nochecked", true);
				json.put("name", t.getDeparetName());
				array.add(json);
				
				if(type != null && type.equals("User")) {
					if(tbusers == null) {
						tbusers = getAllUser();
					} else {
						if(tbusers != null && tbusers.size() > 0) {
							List<TbUser> users = tbusers;
							for(TbUser user : users) {
							    if(user.getDepartment().getDeparetId().equals(t.getDeparetId())) {
									JSONObject json1 = new JSONObject();
									json1.put("id", user.getUserId());
									json1.put("pId", t.getDeparetId());
									json1.put("name", user.getFullName());
									array.add(json1);
								}
							}
						}
					}
				}
			}
		}
		
		//输出数据
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().println(array.toString());
		} catch (IOException e) {
			log.info("输出树数据异常");
		}
	}

之前的想法是,把部门数据读出来的同时,把部门下的人员也一并挂进来,这样由于总是一小段的获取人员数据,向数据库发出的请求比较多,从而导致日志中输出的记录也比较多

根据两点建议:

①:人员和部门的数据改动不大

②:是用静态变量封装,在内存中操作,可以节约时间

比对后,后者比前者快多了,以前点开那个人员树非常慢的。

你可能感兴趣的:(tree,树)