Repository navigation
Expand file tree
/
Copy pathOneToMany.java
More file actions
100 lines (76 loc) · 2.08 KB
/
Copy pathOneToMany.java
File metadata and controls
100 lines (76 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.java.demo03;
/**
* 数据表与简单java类 : 一对多
* 还有多对多,类似的,就没有写了
*
*
* @author jack w
*
*/
class Employee {
private Integer empId;
private String name;
private Double salary;
private Department department;
private Employee manager;
public Employee() {
}
public Employee(Integer empId, String name, Double salary) {
this.empId = empId;
this.name = name;
this.salary = salary;
}
public void setDepartment(Department department) {
this.department = department;
}
public Department getDepartment() {
return department;
}
public void setManager(Employee manager) {
this.manager = manager;
}
public Employee getManager() {
return manager;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", salary=" + salary + ", department=" + department
+ ", manager=" + manager + "]";
}
}
class Department {
private Integer deptId;
private String deptName;
private Employee[] employees;
public Department() {
}
public Department(Integer deptId, String deptName) {
this.deptId = deptId;
this.deptName = deptName;
}
public void setEmployees(Employee[] employees) {
this.employees = employees;
}
public Employee[] getEmployees() {
return employees;
}
}
public class OneToMany {
public static void main(String[] args) {
Employee emp1 = new Employee(1001, "jack", 1000.00);
Employee emp2 = new Employee(1002, "smith", 2000.00);
Employee emp3 = new Employee(1003, "tom", 4000.00);
Department dept1 = new Department(1, "技术部");
Department dept2 = new Department(2, "产品部");
emp1.setDepartment(dept1);
emp1.setManager(emp3);
emp2.setDepartment(dept2);
emp2.setManager(emp3);
emp3.setDepartment(dept2);
dept1.setEmployees(new Employee[]{emp1});
dept2.setEmployees(new Employee[]{emp2,emp3});
for (int i = 0; i < dept1.getEmployees().length; i++) {
System.out.println(dept1.getEmployees()[i].toString());
}
}
}