Hospital Registration System Using Java Spring MVC, MyBatis, and MySQL
This article provides a step‑by‑step tutorial for building a hospital registration web application with Java, Spring MVC, MyBatis, JSP pages, and MySQL, covering environment setup, database schema, DAO interfaces, service implementation, controller logic, and front‑end pages.
The tutorial demonstrates how to develop a hospital registration system using Java as the implementation language, Spring MVC for the web framework, MyBatis for data persistence, and MySQL as the database.
1. Language and Environment
Implementation language: Java. Development environment: MyEclipse/Eclipse, Tomcat, MySQL. Technologies used: Spring MVC + Spring + MyBatis or JSP + Servlet + JavaBean + JDBC.
2. Implementation Effect
The system supports fuzzy search of patient name, physician type, and department, and allows users to mark a registration as completed, changing its status to "visited".
Key UI screenshots illustrate the registration list and the add‑registration form.
3. Code Implementation
Database Schema (tb_patient)
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');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 dep);
}MyBatis XML Mapper (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>
... (SQL statements for insert, update, select, delete) ...
</mapper>Service Implementation (HospitalService.java)
package com.mhys.crm.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.ui.Model;
import com.mhys.crm.dao.TbPatientMapper;
import com.mhys.crm.entity.TbPatient;
public class HospitalService {
@Resource
private TbPatientMapper tbPatientMapper;
public String getList(Model model) {
List
selctAll = tbPatientMapper.selectAlls();
model.addAttribute("selctAll", selctAll);
return "info";
}
public String getAll(Model model, String name, String type, String dep) {
List
selctAll = tbPatientMapper.selectAll(name, type, dep);
model.addAttribute("selctAll", selctAll);
return "info";
}
public String upDev(Model model, int id) {
tbPatientMapper.update(id);
return "redirect:/select.do";
}
public String adds(Model model) {
return "addInfo";
}
public String toaddDev(Model model, TbPatient tb) {
tbPatientMapper.insert(tb);
return "redirect:/select.do";
}
}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();
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);
model.addAttribute("selctAll", selctAll);
return "info";
}
@RequestMapping("/upd")
public String upDev(Model model, int id) {
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
applicationContext‑dao.xml configures the Druid data source, SqlSessionFactory, Mapper scanning, and transaction manager. applicationContext‑service.xml enables component scanning and transaction annotations. spring‑mvc.xml sets up the DispatcherServlet, view resolver, and controller scanning.
JSP Pages
addInfo.jsp provides a form for adding a new registration. info.jsp displays the registration list with search filters, status display, and a "核销" link to mark a registration as completed. index.jsp redirects to the main list page.
The article also includes QR codes and promotional text for obtaining the source code via a WeChat public account.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.