开发者社区> 问答> 正文

如何实现查询结果的树排序?

小旋风柴进 2016-03-11 14:26:06 830

现有的数据结构如图

1、组织机构表 ID,NAME,PID,SORT_ORDER
screenshot
2、用户表 ID,NAME,DEPTID,SORT_ORDER
screenshot
3.现有数据显示的树状结构如图:
screenshot
如果是用tree的js组件,这个树很好实现。

现在,我想做一个通讯录功能,查询员工列表,按照机构顺序(包括下级机构)。

查询出来的员工列表的顺序应该和树里面的从上到下的员工顺序一样。
screenshot
这个要怎么样写代码(Java或者sql)能够实现。

用js的tree组件实现机构树和人员很简单。但是我要用sql查询出来单纯的列表。不知道怎么实现了。

请各位帮忙。

分享到
取消 提交回答
全部回答(1)
  • 小旋风柴进
    2019-07-17 18:59:15
    import contact.Dept;
    import contact.Emp;
    import contact.EmpComparator;
     
    import java.util.*;
     
    public class Program {
        private static Map<Integer, Dept> deptMap;
        private static Map<Integer, Emp>  empMap;
     
        private static void init() {
            Dept newDept = null;
            Emp  newEmp  = null;
     
            deptMap = new HashMap<>();
            empMap  = new HashMap<>();
     
            newDept = new Dept(1, "公司",   0, 99); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(2, "技术部", 1, 90); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(3, "市场部", 1, 80); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(4, "测试组", 2, 90); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(5, "研发组", 2, 95); deptMap.put(newDept.Id, newDept);
     
            newEmp = new Emp(1,  "老板",         1, 99); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(2,  "技术部经理",   2, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(3,  "技术部副经理", 2, 89); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(4,  "市场部经理",   3, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(5,  "测试组长",     4, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(6,  "研发组长",     5, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(7,  "测试组员1",    4, 80); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(8,  "测试组员2",    4, 50); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(9,  "研发组员1",    5, 80); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(10, "研发组员2",    5, 70); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(11, "秘书",         1, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(12, "市场专员1",    3, 50); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(13, "市场专员2",    3, 50); empMap.put(newEmp.Id, newEmp);
        }
     
        private static List<Emp> getContacts() {
            List<Emp> result = new ArrayList<>(empMap.values());
            Collections.sort(result, new EmpComparator(deptMap));
            return result;
        }
     
        public static void main(String[] args) {
            init();
     
            for (Emp emp : getContacts()) {
                System.out.println(emp);
            }
        }
    }import contact.Dept;
    import contact.Emp;
    import contact.EmpComparator;
     
    import java.util.*;
     
    public class Program {
        private static Map<Integer, Dept> deptMap;
        private static Map<Integer, Emp>  empMap;
     
        private static void init() {
            Dept newDept = null;
            Emp  newEmp  = null;
     
            deptMap = new HashMap<>();
            empMap  = new HashMap<>();
     
            newDept = new Dept(1, "公司",   0, 99); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(2, "技术部", 1, 90); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(3, "市场部", 1, 80); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(4, "测试组", 2, 90); deptMap.put(newDept.Id, newDept);
            newDept = new Dept(5, "研发组", 2, 95); deptMap.put(newDept.Id, newDept);
     
            newEmp = new Emp(1,  "老板",         1, 99); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(2,  "技术部经理",   2, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(3,  "技术部副经理", 2, 89); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(4,  "市场部经理",   3, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(5,  "测试组长",     4, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(6,  "研发组长",     5, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(7,  "测试组员1",    4, 80); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(8,  "测试组员2",    4, 50); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(9,  "研发组员1",    5, 80); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(10, "研发组员2",    5, 70); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(11, "秘书",         1, 90); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(12, "市场专员1",    3, 50); empMap.put(newEmp.Id, newEmp);
            newEmp = new Emp(13, "市场专员2",    3, 50); empMap.put(newEmp.Id, newEmp);
        }
     
        private static List<Emp> getContacts() {
            List<Emp> result = new ArrayList<>(empMap.values());
            Collections.sort(result, new EmpComparator(deptMap));
            return result;
        }
     
        public static void main(String[] args) {
            init();
     
            for (Emp emp : getContacts()) {
                System.out.println(emp);
            }
        }
    }
    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题