基于 JAVASSM(Java + Spring + Spring MVC + MyBatis)框架开发一个医院挂号系统

基于 JAVASSM(Java + Spring + Spring MVC + MyBatis)框架开发一个医院挂号系统是一个实用的项目。

步骤一:需求分析

明确系统需要实现的功能,比如:

  • 用户注册和登录
  • 查看医生列表
  • 预约挂号
  • 查看预约记录
  • 取消预约
  • 管理员管理医生信息和预约记录

步骤二:设计数据库

使用 MySQL 数据库存储系统数据。设计数据库表结构如下:

用户表(users)
  • id (INT, 主键, 自增)
  • username (VARCHAR)
  • password (VARCHAR)
  • email (VARCHAR)
  • phone (VARCHAR)
医生表(doctors)
  • id (INT, 主键, 自增)
  • name (VARCHAR)
  • department (VARCHAR)
  • introduction (TEXT)
  • schedule (TEXT)
预约表(appointments)
  • id (INT, 主键, 自增)
  • user_id (INT, 外键)
  • doctor_id (INT, 外键)
  • appointment_time (DATETIME)
  • status (VARCHAR)

步骤三:选择开发工具

使用 IntelliJ IDEA 或 Eclipse 作为开发环境。

步骤四:搭建项目结构

  1. 创建 Maven 项目。
  2. 添加必要的依赖项(Spring、Spring MVC、MyBatis、MySQL 驱动等)。

步骤五:配置文件

application.properties
properties 复制代码
spring.datasource.url=jdbc:mysql://localhost:3306/hospital_registration?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.mapper-locations=classpath:mapper/*.xml
spring-mvc.xml
xml 复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.hospital"/>
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>
mybatis-config.xml
xml 复制代码
<configuration>
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
        <mapper resource="mapper/DoctorMapper.xml"/>
        <mapper resource="mapper/AppointmentMapper.xml"/>
    </mappers>
</configuration>

步骤六:编写实体类

User.java
java 复制代码
package com.hospital.entity;

public class User {
    private int id;
    private String username;
    private String password;
    private String email;
    private String phone;

    // Getters and Setters
}
Doctor.java
java 复制代码
package com.hospital.entity;

public class Doctor {
    private int id;
    private String name;
    private String department;
    private String introduction;
    private String schedule;

    // Getters and Setters
}
Appointment.java
java 复制代码
package com.hospital.entity;

import java.util.Date;

public class Appointment {
    private int id;
    private int userId;
    private int doctorId;
    private Date appointmentTime;
    private String status;

    // Getters and Setters
}

步骤七:编写 DAO 层

UserMapper.java
java 复制代码
package com.hospital.mapper;

import com.hospital.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM users WHERE username = #{username} AND password = #{password}")
    User login(@Param("username") String username, @Param("password") String password);

    @Insert("INSERT INTO users(username, password, email, phone) VALUES(#{username}, #{password}, #{email}, #{phone})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void register(User user);
}
DoctorMapper.java
java 复制代码
package com.hospital.mapper;

import com.hospital.entity.Doctor;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DoctorMapper {
    @Select("SELECT * FROM doctors")
    List<Doctor> getAllDoctors();

    @Select("SELECT * FROM doctors WHERE id = #{id}")
    Doctor getDoctorById(int id);

    @Insert("INSERT INTO doctors(name, department, introduction, schedule) VALUES(#{name}, #{department}, #{introduction}, #{schedule})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addDoctor(Doctor doctor);

    @Update("UPDATE doctors SET name=#{name}, department=#{department}, introduction=#{introduction}, schedule=#{schedule} WHERE id=#{id}")
    void updateDoctor(Doctor doctor);

    @Delete("DELETE FROM doctors WHERE id=#{id}")
    void deleteDoctor(int id);
}
AppointmentMapper.java
java 复制代码
package com.hospital.mapper;

import com.hospital.entity.Appointment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AppointmentMapper {
    @Select("SELECT * FROM appointments WHERE user_id = #{userId}")
    List<Appointment> getAppointmentsByUserId(int userId);

    @Select("SELECT * FROM appointments WHERE id = #{id}")
    Appointment getAppointmentById(int id);

    @Insert("INSERT INTO appointments(user_id, doctor_id, appointment_time, status) VALUES(#{userId}, #{doctorId}, #{appointmentTime}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addAppointment(Appointment appointment);

    @Update("UPDATE appointments SET appointment_time=#{appointmentTime}, status=#{status} WHERE id=#{id}")
    void updateAppointment(Appointment appointment);

    @Delete("DELETE FROM appointments WHERE id=#{id}")
    void deleteAppointment(int id);
}

步骤八:编写 Service 层

UserService.java
java 复制代码
package com.hospital.service;

import com.hospital.entity.User;
import com.hospital.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User login(String username, String password) {
        return userMapper.login(username, password);
    }

    public void register(User user) {
        userMapper.register(user);
    }
}
DoctorService.java
java 复制代码
package com.hospital.service;

import com.hospital.entity.Doctor;
import com.hospital.mapper.DoctorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;

    public List<Doctor> getAllDoctors() {
        return doctorMapper.getAllDoctors();
    }

    public Doctor getDoctorById(int id) {
        return doctorMapper.getDoctorById(id);
    }

    public void addDoctor(Doctor doctor) {
        doctorMapper.addDoctor(doctor);
    }

    public void updateDoctor(Doctor doctor) {
        doctorMapper.updateDoctor(doctor);
    }

    public void deleteDoctor(int id) {
        doctorMapper.deleteDoctor(id);
    }
}
AppointmentService.java
java 复制代码
package com.hospital.service;

import com.hospital.entity.Appointment;
import com.hospital.mapper.AppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    public List<Appointment> getAppointmentsByUserId(int userId) {
        return appointmentMapper.getAppointmentsByUserId(userId);
    }

    public Appointment getAppointmentById(int id) {
        return appointmentMapper.getAppointmentById(id);
    }

    public void addAppointment(Appointment appointment) {
        appointmentMapper.addAppointment(appointment);
    }

    public void updateAppointment(Appointment appointment) {
        appointmentMapper.updateAppointment(appointment);
    }

    public void deleteAppointment(int id) {
        appointmentMapper.deleteAppointment(id);
    }
}

步骤九:编写 Controller 层

UserController.java
java 复制代码
package com.hospital.controller;

import com.hospital.entity.User;
import com.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String showLoginForm() {
        return "login";
    }

    @PostMapping("/login")
    public String handleLogin(@RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        User user = userService.login(username, password);
        if (user != null) {
            model.addAttribute("user", user);
            return "redirect:/doctors";
        } else {
            model.addAttribute("error", "Invalid username or password");
            return "login";
        }
    }

    @GetMapping("/register")
    public String showRegisterForm() {
        return "register";
    }

    @PostMapping("/register")
    public String handleRegister(User user) {
        userService.register(user);
        return "redirect:/login";
    }
}
DoctorController.java
java 复制代码
package com.hospital.controller;

import com.hospital.entity.Doctor;
import com.hospital.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @GetMapping("/doctors")
    public String showDoctors(Model model) {
        List<Doctor> doctors = doctorService.getAllDoctors();
        model.addAttribute("doctors", doctors);
        return "doctors";
    }

    @GetMapping("/doctor/{id}")
    public String showDoctorDetails(@RequestParam("id") int id, Model model) {
        Doctor doctor = doctorService.getDoctorById(id);
        model.addAttribute("doctor", doctor);
        return "doctorDetails";
    }

    @GetMapping("/addDoctor")
    public String showAddDoctorForm() {
        return "addDoctor";
    }

    @PostMapping("/addDoctor")
    public String handleAddDoctor(Doctor doctor) {
        doctorService.addDoctor(doctor);
        return "redirect:/doctors";
    }

    @GetMapping("/editDoctor/{id}")
    public String showEditDoctorForm(@RequestParam("id") int id, Model model) {
        Doctor doctor = doctorService.getDoctorById(id);
        model.addAttribute("doctor", doctor);
        return "editDoctor";
    }

    @PostMapping("/editDoctor")
    public String handleEditDoctor(Doctor doctor) {
        doctorService.updateDoctor(doctor);
        return "redirect:/doctors";
    }

    @GetMapping("/deleteDoctor/{id}")
    public String handleDeleteDoctor(@RequestParam("id") int id) {
        doctorService.deleteDoctor(id);
        return "redirect:/doctors";
    }
}
AppointmentController.java
java 复制代码
package com.hospital.controller;

import com.hospital.entity.Appointment;
import com.hospital.entity.Doctor;
import com.hospital.service.AppointmentService;
import com.hospital.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    @Autowired
    private DoctorService doctorService;

    @GetMapping("/appointments")
    public String showAppointments(@RequestParam("userId") int userId, Model model) {
        List<Appointment> appointments = appointmentService.getAppointmentsByUserId(userId);
        model.addAttribute("appointments", appointments);
        return "appointments";
    }

    @GetMapping("/makeAppointment")
    public String showMakeAppointmentForm(@RequestParam("userId") int userId, Model model) {
        List<Doctor> doctors = doctorService.getAllDoctors();
        model.addAttribute("doctors", doctors);
        model.addAttribute("userId", userId);
        return "makeAppointment";
    }

    @PostMapping("/makeAppointment")
    public String handleMakeAppointment(@RequestParam("userId") int userId, @RequestParam("doctorId") int doctorId,
                                        @RequestParam("appointmentTime") String appointmentTime) {
        Appointment appointment = new Appointment();
        appointment.setUserId(userId);
        appointment.setDoctorId(doctorId);
        appointment.setAppointmentTime(new Date());
        appointment.setStatus("Pending");
        appointmentService.addAppointment(appointment);
        return "redirect:/appointments?userId=" + userId;
    }

    @GetMapping("/cancelAppointment/{id}")
    public String handleCancelAppointment(@RequestParam("id") int id, @RequestParam("userId") int userId) {
        appointmentService.deleteAppointment(id);
        return "redirect:/appointments?userId=" + userId;
    }
}

步骤十:前端页面

使用 JSP 创建前端页面。以下是简单的 JSP 示例:

login.jsp
jsp 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="${pageContext.request.contextPath}/login" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    <input type="submit" value="Login">
</form>
<c:if test="${not empty error}">
    <p style="color: red">${error}</p>
</c:if>
</body>
</html>
doctors.jsp
jsp 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Doctors</title>
</head>
<body>
<h2>Doctors</h2>
<table>
    <tr>
        <th>Name</th>
        <th>Department</th>
        <th>Introduction</th>
        <th>Schedule</th>
        <th>Action</th>
    </tr>
    <c:forEach items="${doctors}" var="doctor">
        <tr>
            <td>${doctor.name}</td>
            <td>${doctor.department}</td>
            <td>${doctor.introduction}</td>
            <td>${doctor.schedule}</td>
            <td>
                <a href="${pageContext.request.contextPath}/doctor/${doctor.id}">View</a>
                <a href="${pageContext.request.contextPath}/editDoctor/${doctor.id}">Edit</a>
                <a href="${pageContext.request.contextPath}/deleteDoctor/${doctor.id}">Delete</a>
            </td>
        </tr>
    </c:forEach>
</table>
<a href="${pageContext.request.contextPath}/addDoctor">Add New Doctor</a>
</body>
</html>
makeAppointment.jsp
jsp 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Make Appointment</title>
</head>
<body>
<h2>Make Appointment</h2>
<form action="${pageContext.request.contextPath}/makeAppointment" method="post">
    <input type="hidden" name="userId" value="${userId}">
    Doctor:
    <select name="doctorId">
        <c:forEach items="${doctors}" var="doctor">
            <option value="${doctor.id}">${doctor.name} (${doctor.department})</option>
        </c:forEach>
    </select><br>
    Appointment Time: <input type="datetime-local" name="appointmentTime"><br>
    <input type="submit" value="Make Appointment">
</form>
</body>
</html>

步骤十一:测试与调试

对每个功能进行详细测试,确保所有功能都能正常工作。

步骤十二:部署与发布

编译最终版本的应用程序,并准备好 WAR 文件供 Tomcat 或其他应用服务器部署。

相关推荐
suweijie7683 小时前
SpringCloudAlibaba | Sentinel从基础到进阶
java·大数据·sentinel
公贵买其鹿4 小时前
List深拷贝后,数据还是被串改
java
xlsw_7 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹8 小时前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭8 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫8 小时前
泛型(2)
java
超爱吃士力架9 小时前
邀请逻辑
java·linux·后端
南宫生9 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石9 小时前
12/21java基础
java
李小白669 小时前
Spring MVC(上)
java·spring·mvc