因为2018年河北大学教务处公开考试信息查询,所以数据均从教务处获得,为保护隐私截图只展现字段。
Navacat导出SQL结构如下:
/*
Navicat MySQL Data Transfer
Source Server :
Source Server Version :
Source Host :
Source Database : exam_arrange
Target Server Type : MYSQL
Target Server Version :
File Encoding :
Date: 2019-05-27 21:36:53
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for exam
-- ----------------------------
DROP TABLE IF EXISTS `exam`;
CREATE TABLE `exam` (
`student_id` varchar(255) DEFAULT NULL,
`student_name` varchar(255) DEFAULT NULL,
`academy` varchar(255) DEFAULT NULL,
`major` varchar(255) DEFAULT NULL,
`course_id` varchar(255) DEFAULT NULL,
`course_num` varchar(255) DEFAULT NULL,
`course_name` varchar(255) DEFAULT NULL,
`campus` varchar(255) DEFAULT NULL,
`buildings` varchar(255) DEFAULT NULL,
`room` varchar(255) DEFAULT NULL,
`date` varchar(255) DEFAULT NULL,
`time` varchar(255) DEFAULT NULL,
`seat` varchar(255) DEFAULT NULL,
`tips` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
index.jsp 内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
首页
显示所有考试信息
因为有一个超链接所以需要一个servlet,创建servlet
import java.util.List;
import com.exam.domain.Exam;
public interface ExamDao {
/*
* 查询所有信息
* @return List
*/
List findAll();
}
import java.sql.SQLException;
import java.util.List;
import com.exam.domain.Exam;
/*
* 这是考试信息的业务处理规范
*/
public interface ExamService {
/*
* 查询所有信息
* @return List
*/
List findAll() throws SQLException;
}
import java.sql.SQLException;
import java.util.List;
import com.exam.dao.ExamDao;
import com.exam.dao.impl.ExamDaoImpl;
import com.exam.domain.Exam;
import com.exam.service.ExamService;
/*
* 这是考试信息业务实现
*/
public class ExamServiceImpl implements ExamService {
@Override
public List findAll() throws SQLException {
ExamDao dao = new ExamDaoImpl();
return dao.findAll();
}
}
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.exam.dao.ExamDao;
import com.exam.domain.Exam;
import com.exam.uitl.JDBCUtil02;
/*
* 这是ExamDao的实现,针对前面定义的规范做出具体的实现
*/
public class ExamDaoImpl implements ExamDao {
/**
* 查询所有考试
* @throws SQLException
*/
@Override
public List findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
return runner.query("select * from exam where student_id = '20161101163'", new BeanListHandler(Exam.class));
}
}
其他功能尚未补充,仅仅提供查询功能
1.View层的编写
在这里我们制作一个前端界面,只有一个按钮即可,View层主要是由jsp、HTML编写而成。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
首页
显示所有考试信息
2.Controller层的编写
Controller层主要编写接受View层请求的Servlet。示例Servlet如下:
package com.exam.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.exam.domain.Exam;
import com.exam.service.ExamService;
import com.exam.service.impl.ExamServiceImpl;
/**
* Servlet implementation class ExamListServlet
*/
public class ExamListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/*
* 负责查询所有考试信息展现到list.jsp上
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.查询出所有考试
ExamService service = new ExamServiceImpl();
List list = service.findAll();
//2. 先把数据存储到作用域中
request.setAttribute("list", list);
//3. 跳转页面
request.getRequestDispatcher("list.jsp").forward(request, response);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3.Model层的编写
Model层在具体的编写中包括dao层和bean层还有service层。
(1)bean层:bean层中编写数据模型类。
示例Bean如下:
package com.exam.domain;
public class Exam {
private String student_id;
private String student_name;
private String academy;
private String major;
private String course_id;
private String course_num;
private String course_name;
private String campus;
private String buildings;
private String room;
private String date;
private String time;
private String seat;
private String tips;
public Exam() {
super();
// TODO Auto-generated constructor stub
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getAcademy() {
return academy;
}
public void setAcademy(String academy) {
this.academy = academy;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getCourse_id() {
return course_id;
}
public void setCourse_id(String course_id) {
this.course_id = course_id;
}
public String getCourse_num() {
return course_num;
}
public void setCourse_num(String course_num) {
this.course_num = course_num;
}
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
public String getCampus() {
return campus;
}
public void setCampus(String campus) {
this.campus = campus;
}
public String getBuildings() {
return buildings;
}
public void setBuildings(String buildings) {
this.buildings = buildings;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
public String getTips() {
return tips;
}
public void setTips(String tips) {
this.tips = tips;
}
}
(2)dao层负责与数据库进行交互。
示例dao层如下:第一个是Dao接口,后面的是实现过程
package com.exam.dao;
import java.sql.SQLException;
import java.util.List;
import com.exam.domain.Exam;
public interface ExamDao {
/*
* 查询所有信息
* @return List
*/
List findAll() throws SQLException;
}
package com.exam.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.exam.dao.ExamDao;
import com.exam.domain.Exam;
import com.exam.uitl.JDBCUtil02;
/*
* 这是ExamDao的实现,针对前面定义的规范做出具体的实现
*/
public class ExamDaoImpl implements ExamDao {
/**
* 查询所有考试
* @throws SQLException
*/
@Override
public List findAll() throws SQLException {
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSource());
return runner.query("select * from exam where student_id = '20161101163'", new BeanListHandler(Exam.class));
}
}
(3)service层:service层负责编写具体的业务逻辑,由于我们的示例中的业务逻辑比较简单,故此处编写的代码较少。对于大型项目来说,业务逻辑层一般需要进行很多的业务处理。同样也是前面是接口后面是实现过程
package com.exam.service;
import java.sql.SQLException;
import java.util.List;
import com.exam.domain.Exam;
/*
* 这是考试信息的业务处理规范
*/
public interface ExamService {
/*
* 查询所有信息
* @return List
*/
List findAll() throws SQLException;
}
package com.exam.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.exam.dao.ExamDao;
import com.exam.dao.impl.ExamDaoImpl;
import com.exam.domain.Exam;
import com.exam.service.ExamService;
/*
* 这是考试信息业务实现
*/
public class ExamServiceImpl implements ExamService {
@Override
public List findAll() throws SQLException {
ExamDao dao = new ExamDaoImpl();
return dao.findAll();
}
}
4.Demo总结
博文中贴出的代码是用户注册时需要的步骤。由于多个类和层间存在调用关系,所以大家在看贴出的源码时可能会有理解的问题。所有类都已经在最开始的项目结构图中体现了,相信对比着项目结构图大家都会很容易明白MVC设计模式的基本调用历程。