mysql怎么在单位表和部门表中实现树形结构查询

pingzi_com 2012-06-20 11:09:38
t_company
id name
1 单位1
2 单位2
3 单位3
4 单位4

t_department
id company_id name
1 1 部门1
2 1 部门2
3 2 部门3
4 2 部门1
实现结果如下:
单位1
部门1
部门2
单位2
部门3
部门1
求解!!!
...全文
823 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
NullPointException 2012-06-25
  • 打赏
  • 举报
回复
http://download.csdn.net/download/w8320273/4361038
正好前段时间写了个小例子,使用ztree插件,需要可以试试
爱在程序 2012-06-25
  • 打赏
  • 举报
回复
最简单的做法两个表之间建一个主外键进行关联;这样维护起来比较方便;
xueshan666 2012-06-25
  • 打赏
  • 举报
回复
懒惰大于一切,不解释
ylovep 2012-06-20
  • 打赏
  • 举报
回复
关键是数据库设计
id name code 根节点 父节点
至于显示可以 用控件
五哥 2012-06-20
  • 打赏
  • 举报
回复


import java.util.ArrayList;
import java.util.List;


public class CompanyList {

private List companys = new ArrayList() ;
public List getCompanys() {
return companys;
}

public void add(Company com){
companys.add(com) ;
}

public Company getCompany(int id){

for (int i = 0; i < companys.size(); i++){
if (((Company)companys.get(i)).getId() == id){
return (Company)companys.get(i) ;
}
}
return null ;
}
/**
* @param args
*/
public static void main(String[] args) {
CompanyList comList = new CompanyList() ;

//自己查数据库得到数据,此处手工创建数据
comList.add(new Company(1, "单位1")) ;
comList.add(new Company(2, "单位2")) ;
comList.add(new Company(3, "单位3")) ;
comList.add(new Company(4, "单位4")) ;

Company com = null ;
Department dept = new Department(1, 1, "部门1") ;
com = comList.getCompany(dept.getCompanyId()) ;
if (com != null ){
com.add(dept) ;
}

dept = new Department(1, 1, "部门2") ;
com = comList.getCompany(dept.getCompanyId()) ;
if (com != null ){
com.add(dept) ;
}
dept = new Department(1, 2, "部门3") ;
com = comList.getCompany(dept.getCompanyId()) ;
if (com != null ){
com.add(dept) ;
}
dept = new Department(1, 3, "部门1") ;
com = comList.getCompany(dept.getCompanyId()) ;
if (com != null ){
com.add(dept) ;
}

List list = comList.getCompanys() ;
for (int i = 0 ;i < list.size(); i++){
com = (Company)list.get(i) ;
System.out.println(com.getName()) ;
com.printDept() ;
}

System.out.println("***打印结束***");


}

}


import java.util.ArrayList;
import java.util.List;

public class Company {

private int id ;
private String name ;


public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List getDepartments() {
return departments;
}

public void setDepartments(List departments) {
this.departments = departments;
}


//getter and setter ;
private List departments = new ArrayList() ;

public void add(Department dept){
departments.add(dept) ;
}

public Company() {
}
public Company(int id, String name) {
this.id = id;
this.name = name;
}

public void printDept() {

List list = this.getDepartments() ;
Department dept = null ;
for (int i = 0; i <list.size(); i++){

dept = (Department ) list.get(i) ;
System.out.println(" --" + dept.getName()) ;
}

}


}


public class Department {

private int id;
private int companyId ;
public int getCompanyId() {
return companyId;
}

public void setCompanyId(int companyId) {
this.companyId = companyId;
}

private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Department() {

}

public Department(int id, int companyId, String name) {
super();
this.id = id;
this.companyId = companyId ;
this.name = name;
}

}


结果:

单位1
--部门1
--部门2
单位2
--部门3
单位3
--部门1
单位4
***打印结束***
pingzi_com 2012-06-20
  • 打赏
  • 举报
回复
要怎么建树呢?能详细给我说下吗?
五哥 2012-06-20
  • 打赏
  • 举报
回复
select c.id, c.name as cName , d.name as dName from t_company c left join t_department d on c.id = d.company_id order by c.id

只能是查出来 ,然后在构建一棵树了
五哥 2012-06-20
  • 打赏
  • 举报
回复
dhtmlXTree

自己去找例子
pingzi_com 2012-06-20
  • 打赏
  • 举报
回复
用代码存是不是太慢了,如果我要在节点上添加怎么办
pingzi_com 2012-06-20
  • 打赏
  • 举报
回复
要怎么设计,用什么控件,可以给我说下吧

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧