SpringMVC 增删改查小案例-医院挂号系统

在这里插入图片描述
在这里插入图片描述

配置文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>医疗系统</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.18.RELEASE</version>
            <!--      <optional>true</optional>-->
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.18.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.9</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

application

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.item.model" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.item.dao" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

spring-mvc

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
    <context:component-scan base-package="com.item" />

    <mvc:annotation-driven />

    <mvc:default-servlet-handler/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/vivw/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

WEB.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

创建实体类

package com.item.model;

public class Users {
    private int id;
    private String name;
    private String sex;
    private int age;
    private String phone;
    private String department;
    private String type;
    private Double price;
    private String state;
    private String register_time;

    public Users(int id, String name, String sex, int age, String phone, String department, String type, Double price, String state, String register_time) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phone = phone;
        this.department = department;
        this.type = type;
        this.price = price;
        this.state = state;
        this.register_time = register_time;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getRegister_time() {
        return register_time;
    }

    public void setRegister_time(String register_time) {
        this.register_time = register_time;
    }

    public Users() {
    }
}


创建mapper接口

package com.item.dao;

import com.item.model.Users;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserMapperInterface {
//    查询全部
    List<Users>SelectAll();
//    模糊查询
    List<Users>SelectVague(@Param("name") String name,
                           @Param("Type") String type,
                           @Param("Depa") String Depa);
    //    新增
    int Inserts  (@Param("username") String name,
                 @Param("Sex") String Sex,
                 @Param("ages") String ages,
                 @Param("phone") String phone,
                 @Param("deap") String deap, @Param("Types") String Types, @Param("Proice") String Proice);
}


创建Service接口

package com.item.service;

import com.item.model.Users;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;

public interface UserServiceInterface {
    //    查询全部
    List<Users> SelectAll();
    //    模糊查询
    List<Users>SelectVague(@Param("name") String name,
                           @Param("Type") String type,
                           @Param("Depa") String Depa);
//    新增
    int Inserts  (@Param("username") String name,
                 @Param("Sex") String Sex,
                 @Param("ages") String ages,
                 @Param("phone") String phone,
                 @Param("deap") String deap, @Param("Types") String Types, @Param("Proice") String Proice);

}


设置mapper

<?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.item.dao.UserMapperInterface">
<!--    查询全部-->
<select id="SelectAll" resultType="Users">
    select * from tb_patient
</select>
<!--    模糊查询-->
    <select id="SelectVague" resultType="Users">
        select * from tb_patient where name like "%${name}%" and type LIKE "%${Type}%" and department Like "%${Depa}%"
    </select>
<!--    新增数据-->
    <insert id="Inserts">
        INSERT INTO `hospital_db`.`tb_patient` (`id`, `name`, `sex`, `age`, `phone`, `department`, `type`, `price`, `state`, `register_time`) VALUES
        (null , '${username}', '${Sex}', '${ages}', '${phone}', '${deap}', '${Types}', '${Proice}', '未就诊', NOW());
    </insert>
</mapper>

实现service

package com.item.serviceimpl;

import com.alibaba.druid.filter.AutoLoad;
import com.item.dao.UserMapperInterface;
import com.item.model.Users;
import com.item.service.UserServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserServiceInterface {
    @Autowired
    private UserMapperInterface userMapperInterface;
    @Override
    public List<Users> SelectAll() {
        return userMapperInterface.SelectAll();
    }

    @Override
    public List<Users> SelectVague(String name, String type, String Depa) {
        return userMapperInterface.SelectVague(name,type,Depa);
    }

    @Override
    public int Inserts(String name, String Sex, String ages, String phone, String deap, String Types, String Proice) {
        return userMapperInterface.Inserts(name,Sex,ages,phone,deap,Types,Proice);

    }




}

配置控制层


```css
package com.item.controller;

import com.item.dao.UserMapperInterface;
import com.item.model.Users;
import com.item.service.UserServiceInterface;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class UsersController  {
    @Autowired
    private UserServiceInterface userServiceInterface;
    @GetMapping("/GetInto")
    public String GetInto(HttpServletRequest  request, Model model) {
        List<Users> o = userServiceInterface.SelectAll();
        model.addAttribute("list", o);
        return "GetInto";
    }
//    模糊查询
    @GetMapping("/GetVague")
    public String GetVague(HttpServletRequest request,Model model){
        String name = request.getParameter("username");
        String Types = request.getParameter("Types");
        if (Types.equals("空")){
            Types="";
        }
        String Deap = request.getParameter("Deap");
        List<Users> users = userServiceInterface.SelectVague(name, Types, Deap);
        model.addAttribute("list", users);
        return "/GetInto";
    }
    @GetMapping("/Insert")
    public String Insert(){
        return "Insert";
    }
//    新增
    @GetMapping("/AddInsert")
    public String Insert(HttpServletRequest request,Model model){
        String username = request.getParameter("username");
        String Sex = request.getParameter("Sex");
        String ages = request.getParameter("ages");
        String phone = request.getParameter("phone");
        String deap = request.getParameter("deap");
        String Types = request.getParameter("Types");
        String Proice="";
        if(Types.equals("普通医生")){
            Proice="8.00";
        }else if(Types.equals("副主任医生")){
            Proice="17.00";
        }else if(Types.equals("专家医生")){
            Proice="25.00";
        }
        userServiceInterface.Inserts(username,Sex,ages,phone,deap,Types,Proice);
        return "redirect:/GetInto";
    }
}

前端界面

<%@ page import="com.item.model.Users" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 30856
  Date: 2022/06/11
  Time: 1:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>挂号系统</title>
</head>
<body>
<% List<Users> list = (List<Users>) request.getAttribute("list");%>
<center>
    <h1>医院就诊挂号系统</h1>
</center>
<style>
    div {
        margin-bottom: 10px;
    }
    div span{
        font-size: 20px;

    }
    div span input{
        width: 20%;
        height: 3%;
    }
    div span select{
        width: 20%;
        height: 3%;
    }
</style>

<div>
    <form action="/GetVague" method="get">

    <span>患者姓名:<input name="username"></span>
    <span>医生类别:<select name="Types">
        <option value="空">不限</option>
        <option >专家医师</option>
        <option >普通医师</option>
        <option >副主任医师</option>
    </select></span>
    <span>科室:<input name="Deap"></span>
    <span><button type="submit">查询</button></span>
       <a href="/Insert"><span><button type="button">挂号</button></span></a>
    </form>
</div>

<table border="1" cellpadding="0" 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>

    </tr>
    <% for(Users users:list){%>
    <tr>
        <td><%=users.getId()%></td>
        <td><%=users.getName()%></td>
        <td><%=users.getSex()%></td>
        <td><%=users.getPhone()%></td>
        <td><%=users.getDepartment()%></td>
        <td><%=users.getType()%></td>
        <td><%=users.getPrice()%></td>
        <td><%=users.getRegister_time()%></td>
        <td><%=users.getState()%></td>
        <td><a>删除</a></td>
    </tr>
    <%}%>
</table>

</body>
</html>

新增界面

<%--
  Created by IntelliJ IDEA.
  User: 30856
  Date: 2022/06/11
  Time: 13:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>患者挂号</title>
</head>
<body>

<h1>患者挂号</h1>

<div>
    <form action="/AddInsert" method="get">
        <div>姓 名 <input name="username"></div>
        <div>性 别
         <input type="radio" name="Sex" value="男">男
         <input type="radio" name="Sex" value="女">女
        </div>
        <div>年 龄
            <input name="ages">
        </div>
        <div>电 话
            <input name="phone">
        </div>
        <div>科 室
            <input name="deap">
        </div>
        <div>医师类别
            <select id="selecid" name="Types">
                <option value="Nono">请选择医生类别</option>
                <option >专家医生</option>
                <option >副主任医生</option>
                <option >普通医生</option>
            </select>
            (价格:<span id="Proices">0</span>元)

        </div>
        <button οnclick="seleset()" TYPE="submit">挂号</button>
        <button type="reset">重置</button>
    </form>

</div>
<script>
    var obj =document.getElementById("selecid")
    obj.οnclick= function seleset(){
        var Proices=document.getElementById("Proices")
        var index = obj.value
        if (index=="Nono"){
            Proices.innerHTML="0"
        }else if(index=="专家医生"){
            Proices.innerHTML="25"
        }else if(index=="副主任医生"){
            Proices.innerHTML="17"
        }else if(index=="普通医生"){
            Proices.innerHTML="8"
        }
    }

</script>
</body>
</html>

目录结构

在这里插入图片描述

配置tomcat

省略。。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值