增加Day5任务
This commit is contained in:
parent
778810e0ed
commit
7a6eda7e74
192
doc/day5.md
192
doc/day5.md
@ -0,0 +1,192 @@
|
||||
本次实验的主要任务是完成期末案例的**业务逻辑层**(Service Layer)开发。业务逻辑层是连接图形界面(GUI)和数据访问层(DAO)的桥梁,它包含了所有的业务规则和流程控制。
|
||||
|
||||
## 任务一:完成DAOFactory
|
||||
|
||||
在开始service层开发之前,我们需要先完成`DAOFactory`类。这个类使用了单例模式和工厂模式,用于统一管理和获取DAO层的实例。
|
||||
|
||||
```java
|
||||
package dao;
|
||||
|
||||
import dao.impl.StudentDAOImpl;
|
||||
import dao.impl.TeacherDAOImpl;
|
||||
import dao.impl.LeaveRequestDAOImpl;
|
||||
|
||||
/**
|
||||
* DAO工厂类,用于获取各种DAO的实例
|
||||
*/
|
||||
public class DAOFactory {
|
||||
private static final StudentDAO studentDAO = new StudentDAOImpl();
|
||||
private static final TeacherDAO teacherDAO = new TeacherDAOImpl();
|
||||
private static final LeaveRequestDAO leaveRequestDAO = new LeaveRequestDAOImpl();
|
||||
|
||||
/**
|
||||
* 私有构造函数,防止实例化
|
||||
*/
|
||||
private DAOFactory() {
|
||||
// 私有构造函数,阻止实例化
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取StudentDAO实例
|
||||
* @return StudentDAO实例
|
||||
*/
|
||||
public static StudentDAO getStudentDAO() {
|
||||
return studentDAO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取TeacherDAO实例
|
||||
* @return TeacherDAO实例
|
||||
*/
|
||||
public static TeacherDAO getTeacherDAO() {
|
||||
return teacherDAO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取LeaveRequestDAO实例
|
||||
* @return LeaveRequestDAO实例
|
||||
*/
|
||||
public static LeaveRequestDAO getLeaveRequestDAO() {
|
||||
return leaveRequestDAO;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`DAOFactory`类的主要特点:
|
||||
1. 使用私有构造函数防止外部实例化
|
||||
2. 使用静态字段保存DAO实例,确保全局唯一
|
||||
3. 提供静态方法获取DAO实例
|
||||
|
||||
后期你可以这样使用它来获取DAO实例:
|
||||
|
||||
```java
|
||||
// 获取StudentDAO实例
|
||||
StudentDAO studentDAO = DAOFactory.getStudentDAO();
|
||||
|
||||
// 获取TeacherDAO实例
|
||||
TeacherDAO teacherDAO = DAOFactory.getTeacherDAO();
|
||||
|
||||
// 获取LeaveRequestDAO实例
|
||||
LeaveRequestDAO leaveRequestDAO = DAOFactory.getLeaveRequestDAO();
|
||||
```
|
||||
|
||||
## 任务二:开发Service层类
|
||||
|
||||
在`src/service`目录下创建以下服务类:
|
||||
|
||||
### 2.1 学生服务类(StudentService.java)
|
||||
|
||||
```java
|
||||
package service;
|
||||
|
||||
import dao.DAOFactory;
|
||||
import dao.StudentDAO;
|
||||
import model.Student;
|
||||
|
||||
/**
|
||||
* 学生信息服务类
|
||||
*/
|
||||
public class StudentService {
|
||||
private final StudentDAO studentDAO = DAOFactory.getStudentDAO();
|
||||
|
||||
/**
|
||||
* 根据ID查询学生
|
||||
* @param id 学生ID
|
||||
* @return 学生信息
|
||||
*/
|
||||
public Student getStudentById(int id) {
|
||||
return studentDAO.findById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 学生登录
|
||||
* @param studentId 学号
|
||||
* @param password 密码
|
||||
* @return 登录成功返回学生信息,失败返回null
|
||||
*/
|
||||
public Student login(String studentId, String password) {
|
||||
Student student = studentDAO.findByStudentId(studentId);
|
||||
if (student != null && password.equals(student.getPassword())) {
|
||||
return student;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2.2 教师服务类(TeacherService.java)
|
||||
|
||||
```java
|
||||
package service;
|
||||
|
||||
import dao.DAOFactory;
|
||||
import dao.TeacherDAO;
|
||||
import model.Teacher;
|
||||
|
||||
/**
|
||||
* 教师信息服务类
|
||||
*/
|
||||
public class TeacherService {
|
||||
private final TeacherDAO teacherDAO = DAOFactory.getTeacherDAO();
|
||||
|
||||
/**
|
||||
* 教师登录
|
||||
* @param teacherId 教师工号
|
||||
* @param password 密码
|
||||
* @return 登录成功返回教师信息,失败返回null
|
||||
*/
|
||||
public Teacher login(String teacherId, String password) {
|
||||
Teacher teacher = teacherDAO.findByTeacherId(teacherId);
|
||||
if (teacher != null && password.equals(teacher.getPassword())) {
|
||||
return teacher;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2.3 请假服务类(LeaveRequestService.java)
|
||||
|
||||
[LeaveRequestService.java](https://static.seahi.me/2024/12/LeaveRequestService.java)
|
||||
|
||||
## 任务三:创建Service工厂类
|
||||
|
||||
为了方便获取Service实例,创建一个ServiceFactory类:
|
||||
|
||||
```java
|
||||
package service;
|
||||
|
||||
/**
|
||||
* Service工厂类,用于获取各种Service的实例
|
||||
*/
|
||||
public class ServiceFactory {
|
||||
private static final StudentService studentService = new StudentService();
|
||||
private static final TeacherService teacherService = new TeacherService();
|
||||
private static final LeaveRequestService leaveRequestService = new LeaveRequestService();
|
||||
|
||||
private ServiceFactory() {
|
||||
// 私有构造函数,防止实例化
|
||||
}
|
||||
|
||||
public static StudentService getStudentService() {
|
||||
return studentService;
|
||||
}
|
||||
|
||||
public static TeacherService getTeacherService() {
|
||||
return teacherService;
|
||||
}
|
||||
|
||||
public static LeaveRequestService getLeaveRequestService() {
|
||||
return leaveRequestService;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 任务四:单元测试
|
||||
|
||||
在`test/service`目录下创建对应的测试类:
|
||||
|
||||
1. [StudentServiceTest.java](https://static.seahi.me/2024/12/StudentServiceTest.java) - 测试学生登录功能
|
||||
2. [TeacherServiceTest.java](https://static.seahi.me/2024/12/TeacherServiceTest.java) - 测试教师登录功能
|
||||
3. [LeaveRequestServiceTest.java](https://static.seahi.me/2024/12/LeaveRequestServiceTest.java) - 测试请假申请和审批功能
|
||||
|
Loading…
x
Reference in New Issue
Block a user