综合案例: 数据表与简单Java类
1.1 一对多
两张数据表:emp、dept。
- emp表:empno、ename、job、sal、comm、mgr、deptno;
- dept表:deptno、dname、loc。
要求可以通过程序描述出如下的对应关系:
- 一个部门有多个雇员,并且可以输出一个部门的完整信息(信息包括雇员信息);
- 可以根据一个雇员信息找到雇员对应的领导信息和雇员所在部门信息。
【实际开发中简单Java类设计原则】
通过Java类的开发以及数据表的使用,可以观察出两者存在对应关系:
- 简单Java类的名称 = 实体表名称;
- 简单Java类的属性 = 实体表的字段;
- 简单Java类的一个对象 = 标的一行记录;
- 对象数组 = 表的多行记录;
- 引用配置 = 外键关系。
1.先按照给定的关系将所有基础字段转化为类
代码
class Emp{
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
public Emp(){}
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 "【Emp】empno = " + this.empno + ", ename = " + this.ename + ", job = " + this.job + ", sal = " + this.sal + ", comm = " + this.comm;
}
}
class Dept{
private int deptno;
private String dname;
private String loc;
public Dept(){}
public Dept(int deptno,String dname,String loc){
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public String getInfo(){
return "【Dept】deptno = " + this.deptno + ", dname = " + this.dname + ", loc = " + this.loc;
}
}
2.随后进行关系设计,该数据表对应有如下几个关系:
- 一个雇员属于一个部门,需要追加部门引用;
- 一个部门有一个领导,领导一定是自身关联,自身引用;
- 一个部门有多雇员,需要一个对象数组来描述多个雇员信息。
增加代码
class Emp 中:
private Emp mgr; // 描述雇员领导
private Dept dept; // 描述雇员所在部门
public Emp getMgr() {
return mgr;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
class Dept 中
private Emp [] emps; // 所有雇员
public Emp[] getEmps() {
return emps;
}
public void setEmps(Emp[] emps) {
this.emps = emps;
}
此时基本类定义完成
3.实现开发需求
完整代码
class Emp{
private int empno;
private String ename;
private String job;
private double sal;
private double comm;
private Emp mgr; // 描述雇员领导
private Dept dept; // 描述雇员所在部门
public Emp getMgr() {
return mgr;
}
public void setMgr(Emp mgr) {
this.mgr = mgr;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Emp(){}
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 "【Emp】empno = " + this.empno + ", ename = " + this.ename + ", job = " + this.job + ", sal = " + this.sal + ", comm = " + this.comm;
}
}
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 Dept(){}
public Dept(int deptno,String dname,String loc){
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public String getInfo(){
return "【Dept】deptno = " + this.deptno + ", dname = " + this.dname + ", loc = " + this.loc;
}
}
public class TestDemo {
public static void main(String args[]) {
// 第一步:设置类对象之间的关系
// 1. 分别创建各自类的实例化对象
Dept dept = new Dept(10, "ACCOUNTING", "NEW YORK");
Emp ea = new Emp(7369, "SMITH", "CLERK", 800.0, 0.0);
Emp eb = new Emp(7566, "ALLEN", "MANAGER", 2450.0, 0.0);
Emp ec = new Emp(7839, "KING", "PRESIDENT", 5000.0, 0.0);
// 2.设置雇员领导的关系
ea.setMgr(eb);
eb.setMgr(ec); // ec对象没有领导
// 3.设置雇员和部门的关系
ea.setDept(dept);
eb.setDept(dept);
ec.setDept(dept);
// 4.设置部门和雇员的关系
dept.setEmps(new Emp[]{ea, eb, ec});
// 第二步:进行数据的取得
// 5.一个部门有多个雇员,并且可以输出一个部门的完整信息(信息包括雇员信息)
System.out.println(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()); // 输出雇员的领导信息
}
}
System.out.println("==========================================================================================");
// 6.根据一个雇员信息找到雇员对应的领导信息和雇员所在部门信息。
System.out.println(eb.getInfo());
if (eb.getMgr() != null) {
System.out.println("\t|-" + eb.getMgr().getInfo()); // 输出eb的领导信息
}
if (eb.getDept() != null) {
System.out.println("\t|-" + eb.getDept().getInfo()); // 输出eb所在部门
}
}
}
输出结果
【Dept】deptno = 10, dname = ACCOUNTING, loc = NEW YORK
|-【Emp】empno = 7369, ename = SMITH, job = CLERK, sal = 800.0, comm = 0.0
|-【Emp】empno = 7566, ename = ALLEN, job = MANAGER, sal = 2450.0, comm = 0.0
|-【Emp】empno = 7566, ename = ALLEN, job = MANAGER, sal = 2450.0, comm = 0.0
|-【Emp】empno = 7839, ename = KING, job = PRESIDENT, sal = 5000.0, comm = 0.0
|-【Emp】empno = 7839, ename = KING, job = PRESIDENT, sal = 5000.0, comm = 0.0
==========================================================================================
【Emp】empno = 7566, ename = ALLEN, job = MANAGER, sal = 2450.0, comm = 0.0
|-【Emp】empno = 7839, ename = KING, job = PRESIDENT, sal = 5000.0, comm = 0.0
|-【Dept】deptno = 10, dname = ACCOUNTING, loc = NEW YORK
1.2 多对多
定义一个学生选课的操作表:三张数据表
- 学生表:学生编号、姓名、年龄;
- 课程表:课程编号、课程名称、学分;
- 学生-课程关系表:学生编号、课程编号、成绩。
要求,可以实现如下信息输出:
- 可以找到一门课程,以及参加此课程的学生和他的成绩;
- 可以根据一个学生找到他所参加的每门课程及成绩。
1.定义基本类,暂时不考虑所有关系
类的创建
class Student{
private int stuid;
private String name;
private int age;
public Student(){}
public Student(int stuid,String name,int age){
this.stuid = stuid;
this.name = name;
this.age = age;
}
public String getInfo() {
return "【Student】 stuid = " + this.stuid + ", name = " + this.name + ", age = " + this.age;
}
}
class Course{
private int cid;
private String name;
private int credit;
public Course(){}
public Course(int cid,String name,int credit){
this.cid = cid;
this.name = name;
this.credit = credit;
}
public String getInfo(){
return "【Course】 cid = " + this.cid + ", name = " + this.name + ", credit = " + this.credit;
}
}
2.一个学生有多门课,一门课有多个学生,所以应该互相保存有各自的对象数组。
代码
class Student{
private int stuid;
private String name;
private int age;
private Course [] courses;
public Course[] getCourses() {
return courses;
}
public void setCourses(Course[] courses) {
this.courses = courses;
}
public Student(){}
public Student(int stuid,String name,int age){
this.stuid = stuid;
this.name = name;
this.age = age;
}
public String getInfo() {
return "【Student】 stuid = " + this.stuid + ", name = " + this.name + ", age = " + this.age;
}
}
class Course{
private int cid;
private String name;
private int credit;
private Student [] students;
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public Course(){}
public Course(int cid,String name,int credit){
this.cid = cid;
this.name = name;
this.credit = credit;
}
public String getInfo(){
return "【Course】 cid = " + this.cid + ", name = " + this.name + ", credit = " + this.credit;
}
}
3.学生和每门课之间都会有成绩,关系表中不光有关系字段还有一个普通字段,一次要再建立一个类。
代码
class Student{
private int stuid;
private String name;
private int age;
// private Course [] courses;
private StudentCourse studentCourse [];
public StudentCourse[] getStudentCourse() {
return this.studentCourse;
}
public void setStudentCourse(StudentCourse[] studentCourse) {
this.studentCourse = studentCourse;
}
public Student(){}
public Student(int stuid,String name,int age){
this.stuid = stuid;
this.name = name;
this.age = age;
}
public String getInfo() {
return "【Student】 stuid = " + this.stuid + ", name = " + this.name + ", age = " + this.age;
}
}
class Course{
private int cid;
private String name;
private int credit;
// private Student [] students;
private StudentCourse studentCourse [];
public StudentCourse[] getStudentCourse() {
return this.studentCourse;
}
public void setStudentCourse(StudentCourse[] studentCourse) {
this.studentCourse = studentCourse;
}
public Course(){}
public Course(int cid,String name,int credit){
this.cid = cid;
this.name = name;
this.credit = credit;
}
public String getInfo(){
return "【Course】 cid = " + this.cid + ", name = " + this.name + ", credit = " + this.credit;
}
}
class StudentCourse{ // 学生选课
private Student student;
private Course course;
private double score;
public Student getStudent() {
return student;
}
public Course getCourse() {
return course;
}
public double getScore() {
return score;
}
public StudentCourse(){ }
public StudentCourse(Student student,Course course,double score){
this.student = student;
this.course = course;
this.score = score;
}
}
4.操作实现
完整代码
class Student{
private int stuid;
private String name;
private int age;
// private Course [] courses;
private StudentCourse studentCourse [];
public StudentCourse[] getStudentCourse() {
return this.studentCourse;
}
public void setStudentCourse(StudentCourse[] studentCourse) {
this.studentCourse = studentCourse;
}
public Student(){}
public Student(int stuid,String name,int age){
this.stuid = stuid;
this.name = name;
this.age = age;
}
public String getInfo() {
return "【Student】 stuid = " + this.stuid + ", name = " + this.name + ", age = " + this.age;
}
}
class Course{
private int cid;
private String name;
private int credit;
// private Student [] students;
private StudentCourse studentCourse [];
public StudentCourse[] getStudentCourse() {
return this.studentCourse;
}
public void setStudentCourse(StudentCourse[] studentCourse) {
this.studentCourse = studentCourse;
}
public Course(){}
public Course(int cid,String name,int credit){
this.cid = cid;
this.name = name;
this.credit = credit;
}
public String getInfo(){
return "【Course】 cid = " + this.cid + ", name = " + this.name + ", credit = " + this.credit;
}
}
class StudentCourse{ // 学生选课
private Student student;
private Course course;
private double score;
public Student getStudent() {
return student;
}
public Course getCourse() {
return course;
}
public double getScore() {
return score;
}
public StudentCourse(){ }
public StudentCourse(Student student,Course course,double score){
this.student = student;
this.course = course;
this.score = score;
}
}
public class TestDemo {
public static void main(String args[]) {
// 第一步:设置类对象之间的关系
// 1. 分别创建各自类的实例化对象
Student stu1 = new Student(1,"Jack",18);
Student stu2 = new Student(2,"Tom",19);
Student stu3 = new Student(3,"Sue",18);
Course ca = new Course(1001,"Java",3);
Course cb = new Course(1002,"C++",3);
// 2.设置学生和课程的关系,这里要准备出成绩
stu1.setStudentCourse(new StudentCourse[] { new StudentCourse(stu1,ca,89.2), new StudentCourse(stu1,cb,87)});
stu2.setStudentCourse(new StudentCourse[] { new StudentCourse(stu2,ca,75)});
stu3.setStudentCourse(new StudentCourse[] { new StudentCourse(stu3,cb,96)} );
// 3.设置课程和学生关系
ca.setStudentCourse(new StudentCourse[] { new StudentCourse(stu1,ca,89.2), new StudentCourse(stu2,ca,75)});
cb.setStudentCourse(new StudentCourse[] { new StudentCourse(stu1,cb,87), new StudentCourse(stu3,cb,96)});
// 第二步:进行数据的取得
// 4.找到一门课程,以及参加此课程的学生和他的成绩
System.out.println(ca.getInfo());
for (int x = 0; x < ca.getStudentCourse().length ; x++) {
System.out.print("\t|- " + ca.getStudentCourse()[x].getStudent().getInfo());
System.out.println(", score = " + ca.getStudentCourse()[x].getScore());
}
System.out.println("============================================================================");
// 5.根据一个学生找到他所参加的每门课程及成绩
System.out.println(stu1.getInfo());
for ( int x = 0 ; x < stu1.getStudentCourse().length ; x++) {
System.out.print("\t|-" + stu1.getStudentCourse()[x].getCourse().getInfo());
System.out.println(", score = " + stu1.getStudentCourse()[x].getScore());
}
}
}
输出结果
【Course】 cid = 1001, name = Java, credit = 3
|- 【Student】 stuid = 1, name = Jack, age = 18, score = 89.2
|- 【Student】 stuid = 2, name = Tom, age = 19, score = 75.0
============================================================================
【Student】 stuid = 1, name = Jack, age = 18
|-【Course】 cid = 1001, name = Java, credit = 3, score = 89.2
|-【Course】 cid = 1002, name = C++, credit = 3, score = 87.0
1.3 角色与权限
1.进行单独类的描述
单独类代码
class Dept{
private int did;
private String dname;
public Dept(int did,String dname){
this.did = did;
this.dname = dname;
}
public String getInfo(){
return "【部门】 did = " + this.did + ", dname = " + this.dname;
}
}
class Emp{
private int eid;
private String ename;
public Emp(int eid,String ename){
this.eid = eid;
this.ename = ename;
}
public String getInfo() {
return "【雇员】 eid = " + this.eid + ", ename = " + this.ename;
}
}
class Role{
private int rid;
private String title;
public Role(int rid, String title){
this.rid = rid;
this.title = title;
}
public String getInfo() {
return "【角色】 rid = " + this.rid + ", title = " + this.title;
}
}
class Action{
private int aid;
private String title;
private String flag;
public Action(int aid,String title,String flag){
this.aid = aid;
this.title = title;
this.flag = flag;
}
public String getInfo(){
return "【权限】 aid = " + this.aid + ", title = " + this.title + ", flag = " + this.flag;
}
}
2.进行关系的描述
代码
class Dept{ // 部门信息
private int did;
private String dname;
private Emp emps [] ; // 一个部门由多个雇员
private Role role; // 一个部门有一个角色
public Emp[] getEmps() {
return emps;
}
public void setEmps(Emp[] emps) {
this.emps = emps;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Dept(int did, String dname){
this.did = did;
this.dname = dname;
}
public String getInfo(){
return "【部门】 did = " + this.did + ", dname = " + this.dname;
}
}
class Emp{ // 雇员信息
private int eid;
private String ename;
private Dept dept; // 一个雇员属于一个部门
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Emp(int eid, String ename){
this.eid = eid;
this.ename = ename;
}
public String getInfo() {
return "【雇员】 eid = " + this.eid + ", ename = " + this.ename;
}
}
class Role{ // 角色信息
private int rid;
private String title;
private Dept [] depts; // 一个角色有多个部门
private Action [] actions; // 一个角色有多个权限
public Dept[] getDepts() {
return depts;
}
public void setDepts(Dept[] depts) {
this.depts = depts;
}
public Action[] getActions() {
return actions;
}
public void setActions(Action[] actions) {
this.actions = actions;
}
public Role(int rid, String title){
this.rid = rid;
this.title = title;
}
public String getInfo() {
return "【角色】 rid = " + this.rid + ", title = " + this.title;
}
}
class Action{ // 权限信息
private int aid;
private String title;
private String flag;
private Role [] roles;
public Role[] getRoles() {
return roles;
}
public void setRoles(Role[] roles) {
this.roles = roles;
}
public Action(int aid, String title, String flag){
this.aid = aid;
this.title = title;
this.flag = flag;
}
public String getInfo(){
return "【权限】 aid = " + this.aid + ", title = " + this.title + ", flag = " + this.flag;
}
}
3.根据关系进行测试数据的编写以及完成指定的输出
代码
class Dept{ // 部门信息
private int did;
private String dname;
private Emp emps [] ; // 一个部门由多个雇员
private Role role; // 一个部门有一个角色
public Emp[] getEmps() {
return emps;
}
public void setEmps(Emp[] emps) {
this.emps = emps;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Dept(int did, String dname){
this.did = did;
this.dname = dname;
}
public String getInfo(){
return "【部门】 did = " + this.did + ", dname = " + this.dname;
}
}
class Emp{ // 雇员信息
private int eid;
private String ename;
private Dept dept; // 一个雇员属于一个部门
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Emp(int eid, String ename){
this.eid = eid;
this.ename = ename;
}
public String getInfo() {
return "【雇员】 eid = " + this.eid + ", ename = " + this.ename;
}
}
class Role{ // 角色信息
private int rid;
private String title;
private Dept [] depts; // 一个角色有多个部门
private Action [] actions; // 一个角色有多个权限
public Dept[] getDepts() {
return depts;
}
public void setDepts(Dept[] depts) {
this.depts = depts;
}
public Action[] getActions() {
return actions;
}
public void setActions(Action[] actions) {
this.actions = actions;
}
public Role(int rid, String title){
this.rid = rid;
this.title = title;
}
public String getInfo() {
return "【角色】 rid = " + this.rid + ", title = " + this.title;
}
}
class Action{ // 权限信息
private int aid;
private String title;
private String flag;
private Role [] roles;
public Role[] getRoles() {
return roles;
}
public void setRoles(Role[] roles) {
this.roles = roles;
}
public Action(int aid, String title, String flag){
this.aid = aid;
this.title = title;
this.flag = flag;
}
public String getInfo(){
return "【权限】 aid = " + this.aid + ", title = " + this.title + ", flag = " + this.flag;
}
}
public class TestDemo {
public static void main(String args[]) {
// 第一步:设置数据之间的关系
// 1. 创建部门数据
Dept d10 = new Dept(10,"财务部");
Dept d20 = new Dept(20,"市场部");
// 2. 创建雇员数据
Emp e7369 = new Emp(7369,"Smith");
Emp e7566 = new Emp(7566,"Allen");
Emp e7902 = new Emp(7902,"Ford");
Emp e7839 = new Emp(7839,"King");
Emp e7788 = new Emp(7788,"Scott");
// 3. 创建角色信息
Role r100 = new Role(100,"管理者");
Role r200 = new Role(200,"职员层");
// 4. 创建权限数据
Action a1000 = new Action(1000,"雇员入职","emp:add");
Action a2000 = new Action(2000,"雇员晋升","emp:edit");
Action a3000 = new Action(3000,"发布公告","new:add");
Action a6000 = new Action(6000,"查看客户信息","cus:list");
Action a7000 = new Action(7000,"回访记录","cus:add");
// 5. 设置角色与权限的关系
r100.setActions(new Action[] {a1000,a2000,a3000});
r200.setActions(new Action[] {a6000,a7000});
// 6. 设置权限和角色的关系
a1000.setRoles(new Role[] {r100});
a2000.setRoles(new Role[] {r100});
a3000.setRoles(new Role[] {r100});
a6000.setRoles(new Role[] {r200});
a7000.setRoles(new Role[] {r200});
// 7. 设置部门和角色的关系
d10.setRole(r100);
d20.setRole(r200);
// 8. 设置角色与部门的关系
r100.setDepts(new Dept[] {d10});
r200.setDepts(new Dept[] {d20});
// 9. 设置雇员与部门的关系
e7369.setDept(d10);
e7566.setDept(d10);
e7902.setDept(d20);
e7839.setDept(d20);
e7788.setDept(d20);
// 10. 设置部门与雇员的关系
d10.setEmps(new Emp[] {e7369,e7566});
d20.setEmps(new Emp[] {e7902,e7839,e7788});
// 第二步:取出相应的数据
// 11. 要求根据一个员工找到他所在的部门,以及该部门对应的角色,以及每个角色对应的所有权限。
System.out.println(e7369.getInfo());
System.out.println("\t|-" + e7369.getDept().getInfo());
System.out.println("\t\t|-" + e7369.getDept().getRole().getInfo());
for (int x = 0 ; x < e7369.getDept().getRole().getActions().length ; x++){
System.out.println("\t\t\t|-" + e7369.getDept().getRole().getActions()[x].getInfo());
}
System.out.println("================================================================================");
// 12. 可以根据一个角色找到具备的此角色的所有部门,以及该部门下的所有员工。
System.out.println(r200.getInfo());
for (int x = 0 ; x < r200.getDepts().length ; x++){
System.out.println("\t|-" + r200.getDepts()[x].getInfo());
for (int y = 0 ; y < r200.getDepts()[x].getEmps().length ; y++) {
System.out.println("\t\t|-" + r200.getDepts()[x].getEmps()[y].getInfo());
}
}
System.out.println("================================================================================");
// 13. 根据一个权限列出具备有该权限的所有角色以及每一个角色对应的所有部门,以及每个部门中的所有员工。
System.out.println(a3000.getInfo());
for (int x = 0 ; x < a3000.getRoles().length ; x++){
System.out.println("\t|-" + a3000.getRoles()[x].getInfo());
for (int y = 0 ; y < a3000.getRoles()[x].getDepts().length ; y++) {
System.out.println("\t\t|-" + a3000.getRoles()[x].getDepts()[y].getInfo());
for (int z = 0 ; z < a3000.getRoles()[x].getDepts()[y].getEmps().length ; z++){
System.out.println("\t\t\t|-" + a3000.getRoles()[x].getDepts()[y].getEmps()[z].getInfo());
}
}
}
}
}
输出结果
【雇员】 eid = 7369, ename = Smith
|-【部门】 did = 10, dname = 财务部
|-【角色】 rid = 100, title = 管理者
|-【权限】 aid = 1000, title = 雇员入职, flag = emp:add
|-【权限】 aid = 2000, title = 雇员晋升, flag = emp:edit
|-【权限】 aid = 3000, title = 发布公告, flag = new:add
================================================================================
【角色】 rid = 200, title = 职员层
|-【部门】 did = 20, dname = 市场部
|-【雇员】 eid = 7902, ename = Ford
|-【雇员】 eid = 7839, ename = King
|-【雇员】 eid = 7788, ename = Scott
================================================================================
【权限】 aid = 3000, title = 发布公告, flag = new:add
|-【角色】 rid = 100, title = 管理者
|-【部门】 did = 10, dname = 财务部
|-【雇员】 eid = 7369, ename = Smith
|-【雇员】 eid = 7566, ename = Allen