需要实现的整体功能:
- 系统的登录并进行用户名的校验
- 团购信息的列表展示
- 团购信息的添加
- 团购信息的检索
1、数据库创建
CREATE DATABASE mydb;
USE mydb;
drop table if exists vaccunit;
CREATE TABLE vaccunit (
vid INT AUTO_INCREMENT PRIMARY KEY,
unitname VARCHAR(50) NOT NULL,
address VARCHAR(50) NOT NULL
);
insert into vaccunit(unitname,address) values('柏林卫生院','中原区');
insert into vaccunit(unitname,address) values('社区卫生院','金水区');
insert into vaccunit(unitname,address) values('第一医院','二七区');
drop table if exists appointment;
CREATE TABLE appointment (
aid INT AUTO_INCREMENT PRIMARY KEY,
person VARCHAR(32) NOT NULL,
phone VARCHAR(11) NOT NULL,
ipcard VARCHAR(18) NOT NULL,
birthday DATETIME NOT NULL,
sex VARCHAR(10) NOT NULL,
vid INT NOT NULL,
FOREIGN KEY (vid) REFERENCES vaccunit(vid)
);
insert into appointment values (1,'张三','1364758374','40038312345678','2020-01-01','男',1);
insert into appointment values (2,'李四','136423374','40038312345678','2020-01-01','女',2);
insert into appointment values (3,'王五','1364758374','40038312345678','2020-01-01','男',3);
select * from vaccunit;
select * from appointment;
#查询所有预约1号医院(柏林)预约医院
select * from appointment where vid=1;
#使用聚合函数统计查询数据数量
select count(*) from appointment where vid=1;
2、框架搭建,创建项目,导入jar包,创建实体类;
1.springmvc.xml
<?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:aop="http://www.springframework.org/schema/aop"
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.zhan.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/fonts/**" location="/fonts/"/>
<mvc:resources mapping="/img/**" location="/img/"/>
<mvc:annotation-driven/>
</beans>
2.spring.xml
<?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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.zhan">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="factoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.zhan.bean"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhan.dao"/>
</bean>
</beans>
3.mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
</configuration>
4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置完成后,测试是否能够运行成功;
index.jsp首页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<a href="findAll">去登录</a>
</body>
</html>
zhuye.jsp主页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>主页</title>
</head>
<body>
<h2>欢迎来到主页!</h2>
</body>
<table>
<thead>
<tr>
<th>接种单位</th>
<th>预约人</th>
<th>电话</th>
<th>身份证</th>
<th>生日</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</html>
controller层的AppointmentController
@Controller
public class AppointmentController {
@RequestMapping("/findAll")
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
System.out.println("findAll执行");
mv.setViewName("zhuye");
return mv;
}
}
运行后,测试成功;
3、当页面第一次加载时,显示所有的预约列表。 在列表中,需要显示列“接种单位”、“预约人”、“电话”、“身份证”、“生日”、“性别”、“操作”。
1.全查
dao层AppointmentDao
@Repository
public interface AppointmentDao {
List<Appointment> selectAll();
}
dao层VaccunitDao
@Repository
public interface VaccunitDao {
@Select("select * from vaccunit where vid=#{vid}")
Vaccunit selectById(int vid);
}
dao层AppointmentDao.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.zhan.dao.AppointmentDao">
<resultMap id="amMap" type="com.zhan.bean.Appointment">
<id property="aid" column="aid"/>
<result property="person" column="person"/>
<result property="phone" column="phone"/>
<result property="ipcard" column="ipcard"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<association property="vaccunit" column="vid" select="com.zhan.dao.VaccunitDao.selectById"></association>
</resultMap>
<select id="selectAll" resultMap="amMap">
select * from appointment;
</select>
</mapper>
service层AppointmentService
@Service
public interface AppointmentService {
List<Appointment> selectAll();
}
service层VaccunitService
public interface VaccunitService {
Vaccunit selectById(int vid);
}
service层impl.AppointmentServiceImpl
@Service
public class AppointmentServiceImpl implements AppointmentService {
@Autowired
AppointmentDao appointmentDao;
@Override
public List<Appointment> selectAll() {
return appointmentDao.selectAll();
}
}
service层impl.VaccunitServiceImpl
@Service
public class VaccunitServiceImpl implements VaccunitService {
@Autowired
VaccunitDao vaccunitDao;
@Override
public Vaccunit selectById(int vid) {
return vaccunitDao.selectById(vid);
}
}
controller层的AppointmentController
@Controller
public class AppointmentController {
@Autowired
AppointmentService appointmentService;
@Autowired
VaccunitService vaccunitService;
@RequestMapping("/findAll")
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
// System.out.println("findAll执行");
List<Appointment> appointmentList = appointmentService.selectAll();
// System.out.println(appointmentList);
mv.addObject("appointmentList",appointmentList);
mv.setViewName("zhuye");
return mv;
}
}
zhuye.jsp主页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>主页</title>
</head>
<body>
<h2>欢迎来到主页!</h2>
<table>
<thead>
<tr>
<th>接种单位</th>
<th>预约人</th>
<th>电话</th>
<th>身份证</th>
<th>生日</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${appointmentList}" var="appointment">
<tr>
<td><a href="findByVid?vid=${appointment.vaccunit.vid}">${appointment.vaccunit.unitname}</a>
</td>
<td>${appointment.person}</td>
<td>${appointment.phone}</td>
<td>${appointment.ipcard}</td>
<td>${appointment.birthday}</td>
<td>${appointment.sex}</td>
<td>
<a href="del?aid=${appointment.aid}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
新增
- 添加页面的接种单位需要从后台查询并使用下列列表显示。
- 当缺陷预约成功之后,跳转到列表页面。
@Repository
public interface AppointmentDao {
int insert(Appointment appointment);
}
dao层AppointmentDao.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.zhan.dao.AppointmentDao">
<resultMap id="amMap" type="com.zhan.bean.Appointment">
<id property="aid" column="aid"/>
<result property="person" column="person"/>
<result property="phone" column="phone"/>
<result property="ipcard" column="ipcard"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<association property="vaccunit" column="vid" select="com.zhan.dao.VaccunitDao.selectById"></association>
</resultMap>
<insert id="insert" parameterType="com.zhan.bean.Appointment">
insert into appointment(person,phone,ipcard,birthday,sex,vid)
values (#{person},#{phone},#{ipcard},#{birthday},#{sex},#{vaccunit.vid});
</insert>
</mapper>
service层AppointmentService
@Service
public interface AppointmentService {
int insert(Appointment appointment);
}
service层impl.AppointmentServiceImpl
@Service
public class AppointmentServiceImpl implements AppointmentService {
@Autowired
AppointmentDao appointmentDao;
@Override
public int insert(Appointment appointment) {
return appointmentDao.insert(appointment);
}
}
controller层的AppointmentController
@Controller
public class AppointmentController {
@Autowired
AppointmentService appointmentService;
@Autowired
VaccunitService vaccunitService;
@RequestMapping("/add")
public ModelAndView insert(Appointment appointment){
ModelAndView mv = new ModelAndView();
// System.out.println(appointment);
int n = appointmentService.insert(appointment);
if(n>0){
mv.setViewName("redirect:/findAll");
}else{
mv.setViewName("error");
}
return mv;
}
@RequestMapping("/del")
public ModelAndView del(int aid){
ModelAndView mv = new ModelAndView();
int n = appointmentService.delete(aid);
System.out.println(n);
mv.setViewName("redirect:/findAll");
return mv;
}
}
zhuye.jsp主页
<h2>欢迎来到主页!</h2>
<h3>
<a href="add.jsp">预约</a>
</h3>
add.jsp添加页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加页</title>
</head>
<body>
<form action="add" method="post">
接种单位:
<select id="type" name="vaccunit.vid">
<option value="1">柏林卫生院</option>
<option value="2">社区卫生院</option>
<option value="3">第一医院</option>
</select><br>
预约人:<input type="text" name="person" id="person" value=""><br>
电话:<input type="text" name="phone" id="phone" value=""><br>
身份证:<input type="text" name="ipcard" id="ipcard" value=""><br>
生日:<input type="text" name="birthday" id="birthday" value=""><br>
性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女<br>
<button type="submit" id="btn">预约</button>
</form>
</body>
</html>
删除
dao层AppointmentDao
@Repository
public interface AppointmentDao {
int delete(int aid);
}
dao层AppointmentDao.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.zhan.dao.AppointmentDao">
<resultMap id="amMap" type="com.zhan.bean.Appointment">
<id property="aid" column="aid"/>
<result property="person" column="person"/>
<result property="phone" column="phone"/>
<result property="ipcard" column="ipcard"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<association property="vaccunit" column="vid" select="com.zhan.dao.VaccunitDao.selectById"></association>
</resultMap>
<delete id="delete" parameterType="int">
delete from appointment where aid=#{aid};
</delete>
</mapper>
service层AppointmentService
@Service
public interface AppointmentService {
int delete(int aid);
}
service层impl.AppointmentServiceImpl
@Service
public class AppointmentServiceImpl implements AppointmentService {
@Autowired
AppointmentDao appointmentDao;
@Override
public int delete(int aid) {
return appointmentDao.delete(aid);
}
}
controller层的AppointmentController
@Controller
public class AppointmentController {
@Autowired
AppointmentService appointmentService;
@Autowired
VaccunitService vaccunitService;
@RequestMapping("/del")
public ModelAndView del(int aid){
ModelAndView mv = new ModelAndView();
int n = appointmentService.delete(aid);
System.out.println(n);
mv.setViewName("redirect:/findAll");
return mv;
}
}
zhuye.jsp主页
<td>
<a href="del?aid=${appointment.aid}">删除</a>
</td>
统计
- 详情界面能够正确显示接种单位和单位地址。
- 并能够正确显示预约人数。
dao层AppointmentDao
@Repository
public interface AppointmentDao {
int count(int vid);
}
dao层AppointmentDao.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.zhan.dao.AppointmentDao">
<resultMap id="amMap" type="com.zhan.bean.Appointment">
<id property="aid" column="aid"/>
<result property="person" column="person"/>
<result property="phone" column="phone"/>
<result property="ipcard" column="ipcard"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<association property="vaccunit" column="vid" select="com.zhan.dao.VaccunitDao.selectById"></association>
</resultMap>
<select id="count" parameterType="int" resultType="int">
select count(*) from appointment where vid=#{vid};
</select>
</mapper>
service层AppointmentService
@Service
public interface AppointmentService {
int count(int vid);
}
service层impl.AppointmentServiceImpl
@Service
public class AppointmentServiceImpl implements AppointmentService {
@Autowired
AppointmentDao appointmentDao;
@Override
public int count(int vid) {
return appointmentDao.count(vid);
}
}
controller层的AppointmentController
@Controller
public class AppointmentController {
@Autowired
AppointmentService appointmentService;
@Autowired
VaccunitService vaccunitService;
@RequestMapping("/findByVid")
public ModelAndView findByVid(int vid){
ModelAndView mv = new ModelAndView();
//根据Vid查询医院信息
Vaccunit vaccunit = vaccunitService.selectById(vid);
//根vid查询该医院预约信息的计数数量
int count = appointmentService.count(vid);
mv.addObject("vaccunit",vaccunit);
mv.addObject("count",count);
mv.setViewName("show");
return mv;
}
}
zhuye.jsp主页
<select id="type" name="vaccunit.vid">
<option value="1">柏林卫生院</option>
<option value="2">社区卫生院</option>
<option value="3">第一医院</option>
</select>
模糊查询
dao层AppointmentDao
@Repository
public interface AppointmentDao {
List<Appointment> seach(Appointment appointment);
}
dao层AppointmentDao.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.zhan.dao.AppointmentDao">
<resultMap id="amMap" type="com.zhan.bean.Appointment">
<id property="aid" column="aid"/>
<result property="person" column="person"/>
<result property="phone" column="phone"/>
<result property="ipcard" column="ipcard"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<association property="vaccunit" column="vid" select="com.zhan.dao.VaccunitDao.selectById"></association>
</resultMap>
<select id="seach" parameterType="com.zhan.bean.Appointment" resultMap="amMap">
select * from appointment where vid=#{vaccunit.vid} and person like concat('%',#{person},'%');
</select>
</mapper>
service层AppointmentService
@Service
public interface AppointmentService {
List<Appointment> seach(Appointment appointment);
}
service层impl.AppointmentServiceImpl
@Service
public class AppointmentServiceImpl implements AppointmentService {
@Autowired
AppointmentDao appointmentDao;
@Override
public List<Appointment> seach(Appointment appointment) {
return appointmentDao.seach(appointment);
}
}
controller层的AppointmentController
@Controller
public class AppointmentController {
@Autowired
AppointmentService appointmentService;
@Autowired
VaccunitService vaccunitService;
@RequestMapping("/seach")
public ModelAndView seach(Appointment appointment){
ModelAndView mv = new ModelAndView();
List<Appointment> appointmentList = appointmentService.seach(appointment);
System.out.println(appointmentList);
mv.addObject("appointmentList",appointmentList);
mv.setViewName("zhuye");
return mv;
}
}
zhuye.jsp主页
<form action="seach" method="post">
<select id="type" name="vaccunit.vid">
<option value="1">柏林卫生院</option>
<option value="2">社区卫生院</option>
<option value="3">第一医院</option>
</select>
<input type="text" name="person" value="" placeholder="请输入姓名">
<input type="submit" value="搜索">
</form>
页面展示