现有的数据结构如图
1、组织机构表 ID,NAME,PID,SORT_ORDER
2、用户表 ID,NAME,DEPTID,SORT_ORDER
3.现有数据显示的树状结构如图:
如果是用tree的js组件,这个树很好实现。
现在,我想做一个通讯录功能,查询员工列表,按照机构顺序(包括下级机构)。
查询出来的员工列表的顺序应该和树里面的从上到下的员工顺序一样。
这个要怎么样写代码(Java或者sql)能够实现。
用js的tree组件实现机构树和人员很简单。但是我要用sql查询出来单纯的列表。不知道怎么实现了。
请各位帮忙。
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);
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。