package cn.Test.lzh;
/**
* dept(部门表): deptno(部门编号)、 dname(部门名称)、 loc(位置) emp(雇员表):
* empno(雇员编号)、ename(雇员名称)、job(职位)、sal(工资)、 comm(佣金)、 depno(部门编号)、 mgr(领导)
*
* 关系: 1):一个部门有多个雇员
* 2):一个雇员有可以一个或零个领导
*
* 步骤:1):实现基本字段的转换,写出Dept和Emp的属性和方法
* 2):解决外键关系:
* 一个雇员属于一个部分,在雇员里面保存部分信息,在Emp类中增加Dept dept对象及相应的set/get方法
* 一个部分有多个雇员,使用数组来完成功能,在Dept类中增加Emp emps[]对象数组及相应的set/get方法 一个雇员除KING外都有一个领导
* 3):设置并取得数据
* 根据结构设置数据:
* 根据结构取出数据
*/
/**
* 部门表设计
*/
class Dept {
private int deptno;
private String dname;
private String loc;
private Emp[] emps;
public Emp[] getEmps() {
return emps;
}
public void setEmps(Emp[] emps) {
this.emps = emps;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Dept() {
super();
}
public Dept(int deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public String getInfo() {
return "部门编号:" + this.deptno + ",部门名称:" + this.dname + ",位置:" + this.loc;
}
}
/**
* 雇员表设计
*/
class Emp {
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
private Dept dept;
private Emp mgr;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Emp getMgr() {
return mgr;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public Emp() {
super();
}
public Emp(int empno, String ename, String job, double sal, double comm) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public String getInfo() {
return "雇员编号:" + this.empno + ",雇员名称:" + this.ename + ",职位:" + this.job + ",工资:" + this.sal + ",佣金:"
+ this.comm;
}
}
/**
* 测试:数据表与简单Java类映射
* 依靠代码链进行读取操作
* @author lzh
*
*/
public class DataLinkDemo {
public static void main(String[] args) {
// 设置部门和雇员的测试数据
Dept dept = new Dept(10, "ACCOUNTING", "New York"); // 部门信息
Emp ea = new Emp(7369, "SMITH", "CLERK", 800.0, 0.00); // 雇员ea信息
Emp eb = new Emp(7902, "FORD", "MANAGER", 2450.0, 0.00); // 雇员eb信息
Emp ec = new Emp(7369, "KING", "PRESIDENT", 5000.0, 0.00); // 雇员ec信息
// 设置雇员和领导关系
ea.setMgr(eb);
eb.setMgr(ec);
// 设置雇员与对应的部门信息
ea.setDept(dept);
eb.setDept(dept);
ec.setDept(dept);
// 设置部门与雇员对应的信息
dept.setEmps(new Emp[] { ea, eb, ec });
// 测试取出数据
// 输出雇员信息
System.out.println("ea雇员信息:" + ea.getInfo());
// 根据雇员查询所对应的领导信息
System.out.println("ea雇员领导的信息:" + ea.getMgr().getInfo());
// 根据雇员查询所对应的部门信息
System.out.println("ea雇员部门的信息:" + ea.getDept().getInfo());
// 取出部门信息
System.out.println("dept部门信息:" + dept.getInfo());
// 根据部门取出所有雇员及每个雇员的领导信息
for (int x = 0; x < dept.getEmps().length; x++) {
System.out.println("\t|-部门雇员:" + dept.getEmps()[x].getInfo());
if (dept.getEmps()[x].getMgr() != null) {
System.out.println("\t\t|-对应领导:" + dept.getEmps()[x].getMgr().getInfo());
}
}
}
}
数据表与简单Java类映射-依靠代码链
最新推荐文章于 2022-05-06 22:38:45 发布