2017年7月30日---阶段性工作总结

近期工作总结:

最近接触的是内蒙古移动渠道后台管理系统的一个项目,该项目已经上线。

我负责的内容只是优化他的一个树结构的回显。

该树一共有7个跟节点,原方式是在前台页面去递归然后去查询接口,这种方式我之前基本就没见过

也没想到他会是这么一个逻辑。他包含子节点的节点一共90多个,然后一次点击事件,展开这个树接口被调用了90多次。。。sql语句发了n条。

然后他有一个库是保存所有节点的库,另外一个库是保存已经选中节点的库。这样他两个库数据还需要去比对。


我是通过Map去做的,提高了效率。我一次查询所有的。然后直接返回整个树结构,这样以后加入redis也方便,比查好几次更方便用缓存。


    // 构建ComboTree
    private ComboTree myComboTree(Object obj, ComboTreeModel comboTreeModel, Set inIDs) {
        ComboTree tree = new ComboTree();
        Map attributes = new HashMap();
        ReflectHelper reflectHelper = new ReflectHelper(obj);
        String id = oConvertUtils.getString(reflectHelper.getMethodValue(comboTreeModel.getIdField()));
        tree.setId(id);
        tree.setPid(oConvertUtils.getString(reflectHelper.getMethodValue("这里定义pid")));
        tree.setText(oConvertUtils.getString(reflectHelper.getMethodValue(comboTreeModel.getTextField())));
        if (comboTreeModel.getSrcField() != null) {
            attributes.put("href", oConvertUtils.getString(reflectHelper.getMethodValue(comboTreeModel.getSrcField())));
            tree.setAttributes(attributes);
        }
        if (inIDs == null) {
        } else {
            if (inIDs.size() > 0) {
                boolean add = inIDs.add(id);//为true 添加成功了,则说明该节点没有在被选中;为false则说明已经被选中了
                if (!add) {
                    tree.setChecked(true);
                }

            }
        }
        return tree;
    }
其中Object是你查询到的对象,这里也就是所有树的节点,ComboTreeModel是你要封装的对象模型,反射取属性的时候用的,Set集合是
已经选中的节点id的集合。他与总节点表是有关联的,就是他关联属性的set集合。

        Set pIds = new HashSet();
        Map> pIdMap = new HashMap>();
我把7个跟节点放入一个集合,把相同的pid的节点放入一个key为string,value为list<combotree>的集合,该集合有90多个这样的集合。
然后我去递归查询封装整个树。

    private void abc(ComboTree comboTree, Map> pIdMap) {
        if (pIdMap.get(comboTree.getId()) != null) {
            comboTree.setChildren(pIdMap.get(comboTree.getId()));
            if (pIdMap.get(comboTree.getId()) != null) {
                for (ComboTree tree : pIdMap.get(comboTree.getId())) {
                    abc(tree, pIdMap);
                }
            }
        }
    }
递归完成之后树中的数据就被填充满了。

补一段前面的代码,帮助理解。

        for (Object obj : all) {
            ComboTree comboTree = new ComboTree();
            comboTree = myComboTree(obj, comboTreeModel, inIDs);
            if (comboTree.getPid = null) {
                trees.add(comboTree);
            } else {
                boolean add = pIds.add(comboTree.getPid);
                if (add) {//如果pId添加进去了则说明第一次
                    pIdMap.put(comboTree.getPid, new ArrayList());
                } else {//否则这个pId之前就已经被添加过了
                    pIdMap.put(comboTree, pIdMap.get(comboTree.getPid).add(comboTree));
                }
            }
        }













发布了21 篇原创文章 · 获赞 17 · 访问量 8649
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章