Simple Java class definitions derived from the structure of the data table,
For example: employee information table, is the employee or department department information table describes information,
In actual development, the mapping between the data tables and simple java classes as follows:
1, data entity class = design definition table;
2, members of the attribute table field = class;
3, a table in a row = instantiated object class;
4, multi-row table = discipline array of objects;
5, the foreign key table associated = reference;
Suppose the table and a department employee:
A department has more employees;
An employee belongs to a department;
An employee has a leader;
The above data table below into the form of a simple Java class,
First, according to the data now to get the following information department:
1, a complete information department;
2, complete information within a department of all employees;
3, corresponding to a leading employee information;
Second, based on the employee to obtain the following information:
1. An information department where employees;
2. a leading information corresponding to the employee;
The first step: put aside all of the associated field does not look like to write basic components, then configure the associated field by reference relations
Package Mapping_transformation; class DEMP { Private Long DEPTNO; Private String DNAME; Private String LOC; public DEMP ( Long DEPTNO, DNAME String, String LOC) { the this .deptno = DEPTNO; the this .dname = DNAME; the this .loc = LOC; } // setter, getter, no-argument constructor little public String getInfo () { return "[department] information department number:" + the this .deptno + ", department name:" + the this.dname +"、部门位置:"+this.loc ; } } class Emp { private long empno ; private String ename ; private String job ; private double sal ; private double comm ; public Emp(long empno ,String ename ,String job , double sal ,double comm) { this.empno = empno ; this.ename = ename ; this.job = job ; this= .sal SAL; the this .comm = COMM; } // setter, getter, no-argument constructor little public String getInfo () { return "[information] Employees Number of employees:" + the this .empno + ", employee name:" + the this .ename + ", employees work:" + the this .job + ", employees' salaries:" + the this .sal + ", a commission employee" + the this .comm; } }
Step Two: Configure all related fields
package Mapping_transformation; class Dept { private long deptno ; private String dname ; private String loc ; private Emp emps[] ; //多个雇员信息 public Dept(long deptno , String dname ,String loc) { this.deptno = deptno ; this.dname = dname ; this.loc = loc ; } public void setEmps(Emp[] emps) { this.emps = emps; } public Emp [] getEmps () { return the this .emps; } // setter, getter, no-argument constructor little public String getInfo () { return "[department] information department number:" + the this .deptno + ", department name:" + the this .dname + ", department positions:" + the this .loc; } } class Emp { Private Long EMPNO; Private String ename; Private String Job; Private Double SAL; Private Double COMM; PrivateThe dept the dept; // Sector Private Emp MGR; // leadership belongs to public Emp ( Long empno, ename String, String the Job, Double SAL, Double COMM) { the this .empno = empno; the this .ename = ename; the this .job = Job; the this .sal = SAL; the this .comm = COMM; } // the setter, getters, slightly constructor with no arguments public String getInfo () { return "[] employee number employee information:" + the this.empno + ", employee name:" + the this .ename + ", employees work:" + the this .job + ", employees' salaries:" + the this .sal + ", a commission employee" + the this .comm; } public void setDept (Dept's Dept) { the this .dept = Dept; } public void setMgr (Emp MGR) { the this .mgr = MGR; } public Dept's getDept () { return the this .dept; } public Emp getMgr () { return the this .mgr; } }
The third step: the class definition is based on the relationship, is set as an object association
public class Demo { public static void main (String [] args) { Dept's Dept = new new Dept's (10, "Finance Department", "Changsha" ); Emp empB = new new Emp (001, "A", "Java Engineer", 800.00,0.0 ); Emp EMPA = new new Emp (002, "B", "manager", 1000.00,0.0 ); Emp-EMPC = new new Emp (003, "C", "BOSS", 1200.00,0.0 ); empA.setDept (the dept); empB.setDept (the dept); // set the associated employees and departments empC.setDept (the dept); EMPA. setMgr(empB); //Provided with leading employee association empB.setMgr (-EMPC); dept.setEmps ( new new Emp [] {EMPA, empB,-EMPC}); // sector employee } }
Finally: Get data required data
System.out.println (dept.getInfo () + "\ n-"); // sector information for ( int I = 0; I <dept.getEmps () length;. I ++ ) { System.out.println (Dept. getEmps () [I] .getInfo ()); // employee information IF ! (dept.getEmps () [I] .getMgr () = null ) System.out.println (dept.getEmps () [I] .getMgr () .getInfo ()); // employee's leading information System.out.println (); } System.out.println ( "-------------------- ---------------------- " ); System.out.println (empA.getDept () getInfo ());. // employees get information department System.out.println (empA.getMgr () getInfo ().); // Employees gain leadership information
[Segment Information] department number: 10 , department name: Ministry of Finance, Department Location: Changsha [employee information] employee number: 2. Name of employee: B, Employee Work: manager, employee salaries: 1000.0, employee commission 0.0 [employee information] employees number: 1 employee name: A, employees work: java engineer, employee salaries: 800.0, employee commission 0.0 [employee information] employees number: 1 employee name: A, employees work: java engineer, employee salaries: 800.0, employees commissions 0.0 [employee information] employees number: 3 employee name: C, employees work: boss, employees' salaries: 1200.0, employee commission 0.0 [employee information] employees number: 3 employee name: C, employees work: boss, employee salaries : 1200.0, employee commission 0.0 ------------------------------------------ [department information] department number: 10 , department name: Ministry of Finance, Department location: Changsha [information] employees number of employees: 1. name of employee: A, employees work: java engineer, employee salaries: 800.0, 0.0 commission employees