基于 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 作为开发环境。
步骤四:搭建项目结构
- 创建 Maven 项目。
- 添加必要的依赖项(Spring、Spring MVC、MyBatis、MySQL 驱动等)。
步骤五:配置文件
application.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
<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
<configuration>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper resource="mapper/DoctorMapper.xml"/>
<mapper resource="mapper/AppointmentMapper.xml"/>
</mappers>
</configuration>
步骤六:编写实体类
User.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
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
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
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)