现在已经设计好了数据库,可以正式打开IDE编写代码了。相信很多同学打开IDE一通操作就是main(),这是不对的哈!
我们先创建好项目,创建好项目后,我们就可以开始写我们的第一个组件了——JavaBean。为什么要一开始写这个组件呢?我们这不是才设计完数据库嘛,现在就要依靠着我们深刻的记忆去写JavaBean,规定好需要暴露的对象。
什么是JavaBean
来源
JavaBean是Java中一种特殊的类,它可以将多个对象封装到一个对象中,它是一种写成的可重用组件。
学过面向对象程序设计的同学一定知道封装这个概念,我们知道,封装的具体作用就是在设计类的时候,为考虑安全性,我们不希望外部可以直接调用类的成员变量,所以我们可以定义成员函数去调用这些成员变量,即get()和set()方法规范诞生。那么根据实体类中我们想要的这些get()和set()相关成员函数,就可以设计一个公共类,基于Java反射机制,来使用这些函数。
但JavaBean在概念上并不是一种技术,而是一种规范,大家针对这种规范,总结了很多开发技巧、工具函数。符合这种规范的类,可以被其它的程序员或者框架使用。
JavaBean的使用规范:
- 有一个public的无参构造器。
- 属性可以通过get()、set()访问或遵循特定命名规则的其他方法存取。
- 继承序列化接口,可以实现序列化。
特点与作用
它的特点如下:
- 所有属性为Private
- 提供默认的构造方法
- getter()和setter()
- 使用包装类
- 实现Serializable接口
根据特点,我们可以实现很多非JavaBean做不到的功能:
- 可以控制它的属性、事件和方法是否暴露。
- 事件既可以产生给其它对象,也可以从其它对象接收。
- 属性可以被序列化,以供日后重用。
我该怎么写JavaBean?
想好需要设计哪些类
在开始写之前,我们需要知道要设计哪些Bean。根据我们设计的数据库,我们需要设计这些:
- 用户类:dataUsers
- 课程类:dataCourses
- 院系类:dataDepartments
- 班级类:dataClasses
- 状态类:statusUser
可以直接开始写了
什么?竟然要我写!!你知道JavaBean的内容有多长且枯燥吗!!其实是不用写的,用IDE能自动生成这一套,接下来就跟着我操作吧~
首先,创建类我就不说了,接下来我以用户类dataUsers为例说明:
创建好以后,我们需要写上我们得暴露的成员变量,说直白点就是把数据库里包含的字段全部写上去。
写好以后,我们回车,在下一行鼠标右键,选择“生成/Generate”
选择后,在出来的菜单中,我们选择创建getter()和setter()方法
在出来的窗口里,我们选中所有的变量
接下来就是见证奇迹的时刻!!它自动生成了所有成员变量的getter()和setter()方法
创建好这些还不够,我们还需要创建构造器,才能激活这个JavaBean。还是在刚才的生成菜单中,我们选择“构造器/Constructor”
在出来的窗口中,我们还是选择所有的成员变量
现在构造器也全部自动生成了
这仍然不够,我们需要创造一个超类,用来调用父类的无参构造器,可以通过super()来调用父类其他重载的构造方法,只要相应的把参数传过去就好。
还是那个菜单,我们选择“覆写方法/Override Methods”
注意,此时我们只需要选择第一个,第一个指的就是构造超类,它就会自动生成super()
但是针对于用户类,我们有三个角色,所以我们还要构造一个方法,用于存放角色:
public dataUsers(int role, String id, String username) {
if(role == 1) {
this.adminID = id;
this.adminName = username;
} else if (role == 2) {
this.teacherID = id;
this.teacherName = username;
} else if (role == 3) {
this.studentID = id;
this.studentName = username;
}
}
至此,我们的用户类JavaBean就创建成功了~
完整代码
用户类dataUsers
package com.personal.teachmanager.Beans;
public class dataUsers {
private String studentID;
private String studentName;
private String studentPassword;
private String studentClass;
private String studentAge;
private String studentSex;
private String studentTele;
private String studentEmail;
private String teacherID;
private String teacherName;
private String teacherPassword;
private String teacherDepartment;
private String teacherLevel;
private String teacherAge;
private String teacherSex;
private String teacherTele;
private String teacherEmail;
private String adminID;
private String adminName;
private String adminPassword;
private int changeFlag;
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentPassword() {
return studentPassword;
}
public void setStudentPassword(String studentPassword) {
this.studentPassword = studentPassword;
}
public String getStudentClass() {
return studentClass;
}
public void setStudentClass(String studentClass) {
this.studentClass = studentClass;
}
public String getStudentAge() {
return studentAge;
}
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
this.studentSex = studentSex;
}
public String getStudentTele() {
return studentTele;
}
public void setStudentTele(String studentTele) {
this.studentTele = studentTele;
}
public String getStudentEmail() {
return studentEmail;
}
public void setStudentEmail(String studentEmail) {
this.studentEmail = studentEmail;
}
public String getTeacherID() {
return teacherID;
}
public void setTeacherID(String teacherID) {
this.teacherID = teacherID;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public String getTeacherPassword() {
return teacherPassword;
}
public void setTeacherPassword(String teacherPassword) {
this.teacherPassword = teacherPassword;
}
public String getTeacherDepartment() {
return teacherDepartment;
}
public void setTeacherDepartment(String teacherDepartment) {
this.teacherDepartment = teacherDepartment;
}
public String getTeacherLevel() {
return teacherLevel;
}
public void setTeacherLevel(String teacherLevel) {
this.teacherLevel = teacherLevel;
}
public String getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(String teacherAge) {
this.teacherAge = teacherAge;
}
public String getTeacherSex() {
return teacherSex;
}
public void setTeacherSex(String teacherSex) {
this.teacherSex = teacherSex;
}
public String getTeacherTele() {
return teacherTele;
}
public void setTeacherTele(String teacherTele) {
this.teacherTele = teacherTele;
}
public String getTeacherEmail() {
return teacherEmail;
}
public void setTeacherEmail(String teacherEmail) {
this.teacherEmail = teacherEmail;
}
public String getAdminID() {
return adminID;
}
public void setAdminID(String adminID) {
this.adminID = adminID;
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminPassword() {
return adminPassword;
}
public void setAdminPassword(String adminPassword) {
this.adminPassword = adminPassword;
}
public int getChangeFlag() {
return changeFlag;
}
public void setChangeFlag(int changeFlag) {
this.changeFlag = changeFlag;
}
public dataUsers(String studentID, String studentName, String studentPassword, String studentClass, String studentAge, String studentSex, String studentTele, String studentEmail, String teacherID, String teacherName, String teacherPassword, String teacherDepartment, String teacherLevel, String teacherAge, String teacherSex, String teacherTele, String teacherEmail, String adminID, String adminName, String adminPassword, int changeFlag) {
this.studentID = studentID;
this.studentName = studentName;
this.studentPassword = studentPassword;
this.studentClass = studentClass;
this.studentAge = studentAge;
this.studentSex = studentSex;
this.studentTele = studentTele;
this.studentEmail = studentEmail;
this.teacherID = teacherID;
this.teacherName = teacherName;
this.teacherPassword = teacherPassword;
this.teacherDepartment = teacherDepartment;
this.teacherLevel = teacherLevel;
this.teacherAge = teacherAge;
this.teacherSex = teacherSex;
this.teacherTele = teacherTele;
this.teacherEmail = teacherEmail;
this.adminID = adminID;
this.adminName = adminName;
this.adminPassword = adminPassword;
this.changeFlag = changeFlag;
}
//用于标记角色
public dataUsers(int role, String id, String username) {
if(role == 1) {
this.adminID = id;
this.adminName = username;
} else if (role == 2) {
this.teacherID = id;
this.teacherName = username;
} else if (role == 3) {
this.studentID = id;
this.studentName = username;
}
}
public dataUsers() {
super();
}
}
课程类dataCourses
package com.personal.teachmanager.Beans;
public class dataCourses {
private String courseID;
private String courseName;
private String courseScore;
public String getCourseID() {
return courseID;
}
public void setCourseID(String courseID) {
this.courseID = courseID;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseScore() {
return courseScore;
}
public void setCourseScore(String courseScore) {
this.courseScore = courseScore;
}
public dataCourses(String courseID, String courseName, String courseScore) {
this.courseID = courseID;
this.courseName = courseName;
this.courseScore = courseScore;
}
public dataCourses() {
super();
}
}
院系类dataDepartments
package com.personal.teachmanager.Beans;
public class dataDepartments {
private int departmentID;
private String departmentName;
public int getDepartmentID() {
return departmentID;
}
public void setDepartmentID(int departmentID) {
this.departmentID = departmentID;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public dataDepartments(int departmentID, String departmentName) {
this.departmentID = departmentID;
this.departmentName = departmentName;
}
public dataDepartments() {
super();
}
}
班级类dataClasses
package com.personal.teachmanager.Beans;
public class dataClasses {
private int classID;
private String className;
public int getClassID() {
return classID;
}
public void setClassID(int classID) {
this.classID = classID;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public dataClasses(int classID, String className) {
this.classID = classID;
this.className = className;
}
public dataClasses() {
super();
}
}
状态类statusUser
package com.personal.teachmanager.Beans;
public class statusUser {
private boolean isLogin;
private boolean userID; //这一部分类型应该为int/String,晚点再改
private boolean userName; //这一部分类型应该为int/String,晚点再改
public boolean isLogin() {
return isLogin;
}
public void setLogin(boolean login) {
isLogin = login;
}
public boolean isUserID() {
return userID;
}
public void setUserID(boolean userID) {
this.userID = userID;
}
public boolean isUserName() {
return userName;
}
public void setUserName(boolean userName) {
this.userName = userName;
}
public statusUser(boolean isLogin, boolean userID, boolean userName) {
this.isLogin = isLogin;
this.userID = userID;
this.userName = userName;
}
}
小结
在本节内容里,我们创建好了需要传参的JavaBeans,这对之后编写业务逻辑有极大的帮助(没有就很难搞),依靠成熟的IDE,我们也不用手写JavaBean这种重复且枯燥的东西,希望你能有耐心看下去,后面就会越来越难了~