数据库采用多表连接查询,对应javaBean文件连接方式

来源:互联网 发布:开电话会议用什么软件 编辑:程序博客网 时间:2024/05/17 06:10

此篇文章借鉴了zhang__bing的文章,表示感谢!

在一个Web项目中,只要是存在数据库就一定会有JavaBean文件。
一个JavaBean文件会对应一张数据库中的表,供dao中的代码来调用用来存取数据。
我们都知道,在数据库设计的时候,如果A、B两张表存在一对多的关系,一定会将一那方的主键设置为多一方的外键来建立关联关系

比如HR项目中,职位表与员工表、部门表与员工表、级别表与员工表,都存在着一对多的关系(一个职位对应多个员工,一个部门对应多个员工等)
在CDM(数据模型)数据库设计中如图:

两个或多个实体中存在多对一的关系
转换成PDM(物理模型)后,如图:

在这里就出现了关联关系。

那么在Web项目中的JavaBean文件应该如何建立它们之间的关联关系呢?

一般来讲我们通常会这样做:

//员工表对应的JavaBeanpublic class Employe {private int id;private int eid;private String name;private Date bornDate;private String gender;private String email;private String phone;private Date joinDate;private short levelId;private short departmentId;private short postId;private byte zaizhi;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getEid() {return eid;}public void setEid(int eid) {this.eid = eid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBornDate() {return bornDate;}public void setBornDate(Date bornDate) {this.bornDate = bornDate;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public Date getJoinDate() {return joinDate;}public void setJoinDate(Date joinDate) {this.joinDate = joinDate;}public short getLevelId() {return levelId;}public void setLevelId(short levelId) {this.levelId = levelId;}public short getDepartmentId() {return departmentId;}public void setDepartmentId(short departmentId) {this.departmentId = departmentId;}public short getPostId() {return postId;}public void setPostId(short postId) {this.postId = postId;}public byte getZaizhi() {return zaizhi;}public void setZaizhi(byte zaizhi) {this.zaizhi = zaizhi;}}

//职位表对应的JavaBeanpublic class Post {private short postId;private String postName;public short getPostId() {return postId;}public void setPostId(short postId) {this.postId = postId;}public String getPostName() {return postName;}public void setPostName(String postName) {this.postName = postName;}}

//部门表对应的JavaBeanpublic class Department {private short departmentId;private String departmentName;public short getDepartmentId() {return departmentId;}public void setDepartmentId(short departmentId) {this.departmentId = departmentId;}public String getDepartmentName() {return departmentName;}public void setDepartmentName(String departmentName) {this.departmentName = departmentName;}}

//级别表对应的JavaBeanpublic class Level {private short levelId;private String levelName;public short getLevelId() {return levelId;}public void setLevelId(short levelId) {this.levelId = levelId;}public String getLevelName() {return levelName;}public void setLevelName(String levelName) {this.levelName = levelName;}}

现在的需求是:多表联查显示每个员工对应的职位名,部门名称,级别名称

首先说不好的解决方式:

1、可以在Employe中增加writerPostName字段来存储职位名、增加writerDepartmentName字段储存部门名称、添加writerLevelName字段储存级别名称。这样当然是可以的。

不过,我们现在的需求是需要得到postName、departmentName、levelName,我们加writerPostName、writerDepartmentName、writerLevelName字段来接收,那如果我们哪天又多了个需求,需要再得到Employe中的其它字段该怎么办呢?这显然很麻烦!

2、可以在jsp页面使用<c:if   test=""></c:if>将取到的员工的postId、departmentId、levelId值与各个从表的id值进行比对,然后输出对应的postName、departmentName、levelName但是违背了jsp只负责显示规范,而且效率不高

并且,有很重要的一点:

在基础的Web项目中,基本的文件规划应该是:

各个部分功能大致是这样的:

bean包中为JavaBean文件,用于对数据的存取操作

dao中定义一系列业务逻辑操作需要的数据库增删改查操作需要的接口

dao.impl中为dao的实现类

service中为一些业务逻辑实现方法

servlet中为一些Servlet文件,用来接收jsp中传递过来的参数值,然后调用service中的方法进行业务处理,最后进行页面跳转

.jsp文件只负责显示


到现在,我们还忘记一件事情: 

在Java中有一个很重要的概念、也是一个很不好理解透彻的概念:面向对象的思想


之前的方法无法将面向对象的思想体现出来,那我们现在就换一个方法:

刚才我们考虑到,用增加字段来解决问题。

同样的,如果我们将Post、Department、Level中的所有属性都添加到的Employe中,我们为何不在Employe中建立三个Post、Department、Level类型的对象来存储这些信息呢!

所以,我们可以将Employe改善成:

public class Employe {private int id;private int eid;private String name;private Date bornDate;private String gender;private String email;private String phone;private Date joinDate;private short levelId;private short departmentId;private short postId;private byte zaizhi;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getEid() {return eid;}public void setEid(int eid) {this.eid = eid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBornDate() {return bornDate;}public void setBornDate(Date bornDate) {this.bornDate = bornDate;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public Date getJoinDate() {return joinDate;}public void setJoinDate(Date joinDate) {this.joinDate = joinDate;}public short getLevelId() {return levelId;}public void setLevelId(short levelId) {this.levelId = levelId;}public short getDepartmentId() {return departmentId;}public void setDepartmentId(short departmentId) {this.departmentId = departmentId;}public short getPostId() {return postId;}public void setPostId(short postId) {this.postId = postId;}public byte getZaizhi() {return zaizhi;}public void setZaizhi(byte zaizhi) {this.zaizhi = zaizhi;}//新添加的类型对象private Post post;private Department department;private Level level;public Post getPost() {return post;}public void setPost(Post post) {this.post = post;}public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}public Level getLevel() {return level;}public void setLevel(Level level) {this.level = level;}}

这样我们将一个Post对象、一个Department对象、一个Level对象存入到Employe中,我们就可以利用这三个对象中的任意属性了。


那Post、Department、Level中是否需要改善呢?

那当然需要了。

如果也考虑到面向对象的思想,我们应该进行这样的改善:

public class Post {private short postId;private String postName;private ArrayList<Employe> employes;public ArrayList<Employe> getEmployes() {return employes;}public void setEmployes(ArrayList<Employe> employes) {this.employes = employes;}public short getPostId() {return postId;}public void setPostId(short postId) {this.postId = postId;}public String getPostName() {return postName;}public void setPostName(String postName) {this.postName = postName;}}

该职位的所有员工以一个列表方式存放在Post中


public class Department {private short departmentId;private String departmentName;private ArrayList<Employe> employes;public ArrayList<Employe> getEmployes() {return employes;}public void setEmployes(ArrayList<Employe> employes) {this.employes = employes;}public short getDepartmentId() {return departmentId;}public void setDepartmentId(short departmentId) {this.departmentId = departmentId;}public String getDepartmentName() {return departmentName;}public void setDepartmentName(String departmentName) {this.departmentName = departmentName;}}

该部门的所有员工以一个列表方式存放在Department中


public class Level {private short levelId;private String levelName;private ArrayList<Employe> employes;public ArrayList<Employe> getEmployes() {return employes;}public void setEmployes(ArrayList<Employe> employes) {this.employes = employes;}public short getLevelId() {return levelId;}public void setLevelId(short levelId) {this.levelId = levelId;}public String getLevelName() {return levelName;}public void setLevelName(String levelName) {this.levelName = levelName;}}
该级别的所有员工以一个列表方式存放在Level中

综上所述:

在数据库表中一对多的关联是利用外键的方式

而在JavaBean中,这些文件之间一对多的关联是:

在一的一方建立一个多一方的列表

在多的一方建立一个一一方的对象


最后:在jsp页面取值

对象.post.getPostName()

对象.department.getDepartmentName()

对象.level.getLevelName()

原创粉丝点击