Backend Development 24 min read

Hospital Registration System Using Java Spring MVC and MyBatis

This article presents a complete Java‑based hospital registration system built with Spring MVC, MyBatis, and JSP, detailing the development environment, database schema, source code for controllers, DAOs, services, configuration files, and web pages to enable patient search, registration, and status updates.

Java Captain
Java Captain
Java Captain
Hospital Registration System Using Java Spring MVC and MyBatis

Language and Environment

Implementation language: Java. Development environment: MyEclipse/Eclipse, Tomcat, MySQL. Core technologies: Spring MVC, Spring, MyBatis (or JSP + Servlet + JavaBean + JDBC).

Implementation Effect

The system provides fuzzy search for patient name, physician type, and department; after a user clicks the verification link, the appointment status changes to "visited". It also supports adding basic registration information through a web form.

Database Schema

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tb_patient
-- ----------------------------
DROP TABLE IF EXISTS `tb_patient`;
CREATE TABLE `tb_patient` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `sex` varchar(10) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `department` varchar(50) DEFAULT NULL,
  `type` varchar(50) DEFAULT NULL,
  `price` decimal(9,2) DEFAULT NULL,
  `state` int(11) DEFAULT NULL,
  `register_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

INSERT INTO `tb_patient` VALUES ('1','张蕾','女','12','13895463212','儿科','专家医师','25.00','1','2021-07-18 12:23:00');
INSERT INTO `tb_patient` VALUES ('2','刘德明','男','28','13345623215','骨科','普通医师','8.00','0','2021-07-18 12:23:00');
INSERT INTO `tb_patient` VALUES ('3','李将军','男','38','13578064788','内科','专家医师','25.00','1','2021-07-17 12:23:00');
INSERT INTO `tb_patient` VALUES ('4','张佩佩','女','44','18214217246','外科','副主任医师','17.00','0','2021-07-16 12:23:00');
INSERT INTO `tb_patient` VALUES ('5','程聪明','男','29','13652645964','骨科','副主任医师','17.00','0','2021-08-08 16:21:52');

Controller (HospitalContrller.java)

package com.mhys.crm.controller;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mhys.crm.dao.TbPatientMapper;
import com.mhys.crm.entity.TbPatient;

@Controller
public class HospitalContrller {
  @Resource
  private TbPatientMapper tbPatientMapper;

  @RequestMapping("/select")
  public String getList(Model model) {
    List
selctAll = tbPatientMapper.selectAlls();
    System.out.println(selctAll);
    model.addAttribute("selctAll", selctAll);
    return "info";
  }

  @RequestMapping("/list")
  public String getAll(Model model, String name, String type, String dep) {
    List
selctAll = tbPatientMapper.selectAll(name, type, dep);
    System.out.println(name+"==="+type+"==="+dep);
    model.addAttribute("selctAll", selctAll);
    return "info";
  }

  @RequestMapping("/upd")
  public String upDev(Model model,int id) {
    int update = tbPatientMapper.update(id);
    return "redirect:/select.do";
  }

  @RequestMapping("/adds")
  public String adds(Model model) {
    return "addInfo";
  }

  @RequestMapping("/insert")
  public String toaddDev(Model model,TbPatient tb) {
    tbPatientMapper.insert(tb);
    return "redirect:/select.do";
  }
}

DAO Interface (TbPatientMapper.java)

package com.mhys.crm.dao;

import com.mhys.crm.entity.TbPatient;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface TbPatientMapper {
    int deleteByPrimaryKey(Integer id);
    int insert(TbPatient record);
    TbPatient selectByPrimaryKey(Integer id);
    List
selectAlls();
    int updateByPrimaryKey(TbPatient record);
    int update(Integer id);
    List
selectAll(@Param("name")String name,@Param("type")String type,@Param("dep")String dap);
}

MyBatis Mapper XML (TbPatientMapper.xml)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mhys.crm.dao.TbPatientMapper" >
  <resultMap id="BaseResultMap" type="com.mhys.crm.entity.TbPatient" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="sex" property="sex" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
    <result column="phone" property="phone" jdbcType="VARCHAR" />
    <result column="department" property="department" jdbcType="VARCHAR" />
    <result column="type" property="type" jdbcType="VARCHAR" />
    <result column="price" property="price" jdbcType="DECIMAL" />
    <result column="state" property="state" jdbcType="INTEGER" />
    <result column="register_time" property="registerTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_patient where id = #{id,jdbcType=INTEGER}
  </delete>

  <insert id="insert" parameterType="com.mhys.crm.entity.TbPatient" >
    insert into tb_patient (id, name, sex, age, phone, department, type, price, state, register_time)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{phone,jdbcType=VARCHAR}, #{department,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, #{state,jdbcType=INTEGER}, #{registerTime,jdbcType=TIMESTAMP})
  </insert>

  <update id="updateByPrimaryKey" parameterType="com.mhys.crm.entity.TbPatient" >
    update tb_patient set name = #{name,jdbcType=VARCHAR}, sex = #{sex,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER}, phone = #{phone,jdbcType=VARCHAR}, department = #{department,jdbcType=VARCHAR}, type = #{type,jdbcType=VARCHAR}, price = #{price,jdbcType=DECIMAL}, state = #{state,jdbcType=INTEGER}, register_time = #{registerTime,jdbcType=TIMESTAMP} where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectAlls" resultMap="BaseResultMap" >
    select id, name, sex, age, phone, department, type, price, state, register_time from tb_patient
  </select>

  <select id="selectAll" resultMap="BaseResultMap" >
    select id, name, sex, age, phone, department, type, price, state, register_time from tb_patient
    <where>
      <if test="name!=null and name!=''"> and name = #{name} </if>
      <if test="type!=null and type!=''"> and type = #{type} </if>
      <if test="dep!=null and dep!=''"> and department = #{dep} </if>
    </where>
  </select>

  <update id="update" parameterType="com.mhys.crm.entity.TbPatient" >
    update tb_patient set state=1 where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

Entity Class (TbPatient.java)

package com.mhys.crm.entity;

import java.math.BigDecimal;
import java.util.Date;

public class TbPatient {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private String phone;
    private String department;
    private String type;
    private BigDecimal price;
    private Integer state;
    private Date registerTime;
    // getters and setters omitted for brevity
    @Override
    public String toString() {
        return "TbPatient [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", phone=" + phone + ", department=" + department + ", type=" + type + ", price=" + price + ", state=" + state + ", registerTime=" + registerTime + "]";
    }
}

Service Implementation (HospitalService.java)

package com.mhys.crm.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mhys.crm.dao.TbPatientMapper;
import com.mhys.crm.entity.TbPatient;

public class HospitalService {
  @Resource
  private TbPatientMapper tbPatientMapper;

  @RequestMapping("/select")
  public String getList(Model model) {
    List
selctAll = tbPatientMapper.selectAlls();
    System.out.println(selctAll);
    model.addAttribute("selctAll", selctAll);
    return "info";
  }

  @RequestMapping("/list")
  public String getAll(Model model, String name, String type, String dep) {
    List
selctAll = tbPatientMapper.selectAll(name, type, dep);
    System.out.println(name+"==="+type+"==="+dep);
    model.addAttribute("selctAll", selctAll);
    return "info";
  }

  @RequestMapping("/upd")
  public String upDev(Model model,int id) {
    int update = tbPatientMapper.update(id);
    return "redirect:/select.do";
  }

  @RequestMapping("/adds")
  public String adds(Model model) {
    return "addInfo";
  }

  @RequestMapping("/insert")
  public String toaddDev(Model model,TbPatient tb) {
    tbPatientMapper.insert(tb);
    return "redirect:/select.do";
  }
}

Spring Configuration Files

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  <context:property-placeholder location="classpath:database.properties"/>
  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="Url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
  </bean>
  <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
    <property name="dataSource" ref="dataSource"/>
  </bean>
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.mhys.crm.dao"/>
  </bean>
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>
  <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

JSP Pages

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>挂号</title>
</head>
<body>
  <form action="insert.do" method="post">
    <table>
      <tr><td>姓名</td><td><input type="text" name="name"/></td></tr>
      <tr><td>性别</td><td><input type="text" name="sex"/></td></tr>
      <tr><td>年龄</td><td><input type="text" name="age"/></td></tr>
      <tr><td>电话</td><td><input type="text" name="phone"/></td></tr>
      <tr><td>医师类别</td><td><input type="text" name="department"/></td></tr>
      <tr><td>价格</td><td><input type="text" name="price"/></td></tr>
      <tr><td>挂号时间</td><td><input type="text" name="registerTime"/></td></tr>
    </table>
    <input type="submit" value="确定"/>
  </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>医院就诊挂号系统</title>
  <style>form{padding:20px;}#warp{margin:0 auto;width:60%;}</style>
</head>
<body>
  <h1 align="center">医院就诊挂号系统</h1>
  <div id="warp">
    <form action="list.do">
      患者姓名:<input type="text" name="name"/>
      医师类别:<select name="type">
        <option value="">=不限=</option>
        <option value="专家医师">专家医师</option>
        <option value="普通医师">普通医师</option>
        <option value="副主任医师">副主任医师</option>
      </select>
      科室:<input type="text" name="dep"/>
      <input type="submit" value="查询"/>
      <input type="button" value="挂号" onclick="add()"/>
    </form>
    <table border="1" cellpadding="11" cellspacing="0" width="100%">
      <tr>
        <th>编号</th><th>姓名</th><th>性别</th><th>年龄</th><th>电话</th><th>科室</th><th>医师类别</th><th>价格</th><th>挂号时间</th><th>状态</th><th>操作</th>
      </tr>
      <c:forEach var="list" items="${selctAll }">
        <tr>
          <td>${list.id }</td>
          <td>${list.name }</td>
          <td>${list.sex }</td>
          <td>${list.age }</td>
          <td>${list.phone }</td>
          <td>${list.department }</td>
          <td>${list.type }</td>
          <td>${list.price }</td>
          <td><fmt:formatDate value="${list.registerTime }" pattern="yyyy-MM-dd"/></td>
          <td>
            <c:if test="${list.state==0}">未就诊</c:if>
            <c:if test="${list.state==1}">已就诊</c:if>
          </td>
          <td>
            <c:if test="${list.state==0}">
              <a href="javascript:if(confirm('确实要核销该挂号信息吗?'))location='upd.do?id=${list.id }'">核销</a>
            </c:if>
          </td>
        </tr>
      </c:forEach>
    </table>
    <script>function add(){location.href="adds.do";}</script>
  </div>
</body>
</html>

The complete source code, configuration files, and database scripts can be downloaded from the provided Baidu Cloud link.

JavaMyBatisWeb DevelopmentJSPSpring MVCHospital Management
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.