用SpringMVC+jdbc写一个课堂签到管理系统
时间: 2024-05-11 10:19:45 浏览: 174
Springmvc+jdbc简单学生管理系统
好的,下面是一个简单的课堂签到管理系统的实现,使用SpringMVC框架和JDBC进行数据库操作。
首先,我们需要创建一个数据库,其中包含两个表:学生表和签到表。学生表包括学生的学号、姓名和班级;签到表包括签到记录的id、学生的学号、签到时间和签到状态(已签到或未签到)。
1. 创建数据库:
```sql
CREATE DATABASE `class_sign` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `class_sign`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stu_no` varchar(20) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`class` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `sign` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stu_no` varchar(20) DEFAULT NULL,
`sign_time` datetime DEFAULT NULL,
`status` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
```
2. 创建Maven项目,并添加SpringMVC和JDBC的依赖:
```xml
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
```
3. 配置web.xml文件,指定DispatcherServlet的映射和路径:
```xml
<web-app>
<display-name>class-sign</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/springmvc-config.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>
</web-app>
```
4. 配置SpringMVC的配置文件springmvc-config.xml,包括视图解析器和控制器的配置:
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 控制器配置 -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.example.controller"/>
<!-- 数据源配置 -->
<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/class_sign?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- JDBC模板配置 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
```
5. 创建学生实体类Student和签到实体类Sign:
```java
public class Student {
private Integer id;
private String stuNo;
private String name;
private String clazz;
// getter和setter方法
}
public class Sign {
private Integer id;
private String stuNo;
private Date signTime;
private String status;
// getter和setter方法
}
```
6. 创建学生的DAO类StudentDao和签到的DAO类SignDao,并实现相应的方法:
```java
@Repository
public class StudentDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Student> findAll() {
String sql = "SELECT * FROM student";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
}
public Student findByStuNo(String stuNo) {
String sql = "SELECT * FROM student WHERE stu_no = ?";
List<Student> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class), stuNo);
return list.size() == 0 ? null : list.get(0);
}
public int add(Student student) {
String sql = "INSERT INTO student(stu_no, name, class) VALUES(?, ?, ?)";
return jdbcTemplate.update(sql, student.getStuNo(), student.getName(), student.getClazz());
}
public int delete(Integer id) {
String sql = "DELETE FROM student WHERE id = ?";
return jdbcTemplate.update(sql, id);
}
}
@Repository
public class SignDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public int add(Sign sign) {
String sql = "INSERT INTO sign(stu_no, sign_time, status) VALUES(?, ?, ?)";
return jdbcTemplate.update(sql, sign.getStuNo(), sign.getSignTime(), sign.getStatus());
}
public List<Sign> findByStuNo(String stuNo) {
String sql = "SELECT * FROM sign WHERE stu_no = ?";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Sign.class), stuNo);
}
public int delete(Integer id) {
String sql = "DELETE FROM sign WHERE id = ?";
return jdbcTemplate.update(sql, id);
}
}
```
7. 创建控制器类SignController,实现学生的添加和删除、签到的添加和查询功能:
```java
@Controller
@RequestMapping("/sign")
public class SignController {
@Autowired
private StudentDao studentDao;
@Autowired
private SignDao signDao;
@RequestMapping("/toAdd")
public String toAdd(Model model) {
model.addAttribute("student", new Student());
return "add";
}
@RequestMapping("/add")
public String add(Student student) {
studentDao.add(student);
return "redirect:/sign/list";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable("id") Integer id) {
studentDao.delete(id);
return "redirect:/sign/list";
}
@RequestMapping("/list")
public String list(Model model) {
List<Student> students = studentDao.findAll();
model.addAttribute("students", students);
return "list";
}
@RequestMapping("/toSign")
public String toSign(Model model) {
model.addAttribute("sign", new Sign());
return "sign";
}
@RequestMapping("/sign")
public String sign(Sign sign) {
String stuNo = sign.getStuNo();
Student student = studentDao.findByStuNo(stuNo);
if (student == null) {
return "error";
}
sign.setSignTime(new Date());
sign.setStatus("已签到");
signDao.add(sign);
return "redirect:/sign/signList/" + stuNo;
}
@RequestMapping("/signList/{stuNo}")
public String signList(@PathVariable("stuNo") String stuNo, Model model) {
List<Sign> signs = signDao.findByStuNo(stuNo);
model.addAttribute("signs", signs);
return "signList";
}
@RequestMapping("/deleteSign/{id}")
public String deleteSign(@PathVariable("id") Integer id) {
Sign sign = signDao.findById(id);
if (sign == null) {
return "error";
}
signDao.delete(id);
return "redirect:/sign/signList/" + sign.getStuNo();
}
}
```
8. 创建视图文件,包括学生添加页面add.jsp、学生列表页面list.jsp、签到页面sign.jsp和签到列表页面signList.jsp:
add.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加学生</title>
</head>
<body>
<h1>添加学生</h1>
<form action="${pageContext.request.contextPath}/sign/add" method="post">
学号:<input type="text" name="stuNo"/><br/>
姓名:<input type="text" name="name"/><br/>
班级:<input type="text" name="clazz"/><br/>
<input type="submit" value="添加"/>
</form>
<hr/>
<a href="${pageContext.request.contextPath}/sign/list">查看所有学生</a>
</body>
</html>
```
list.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生列表</title>
</head>
<body>
<h1>学生列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>学号</th>
<th>姓名</th>
<th>班级</th>
<th>操作</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.stuNo}</td>
<td>${student.name}</td>
<td>${student.clazz}</td>
<td>
<a href="${pageContext.request.contextPath}/sign/delete/${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<hr/>
<a href="${pageContext.request.contextPath}/sign/toAdd">添加学生</a>
<a href="${pageContext.request.contextPath}/sign/toSign">学生签到</a>
</body>
</html>
```
sign.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生签到</title>
</head>
<body>
<h1>学生签到</h1>
<form action="${pageContext.request.contextPath}/sign/sign" method="post">
学号:<input type="text" name="stuNo"/><br/>
<input type="submit" value="签到"/>
</form>
<hr/>
<a href="${pageContext.request.contextPath}/sign/list">查看所有学生</a>
<a href="${pageContext.request.contextPath}/sign/toSign">学生签到</a>
</body>
</html>
```
signList.jsp:
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>签到列表</title>
</head>
<body>
<h1>签到列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>学号</th>
<th>签到时间</th>
<th>签到状态</th>
<th>操作</th>
</tr>
<c:forEach items="${signs}" var="sign">
<tr>
<td>${sign.id}</td>
<td>${sign.stuNo}</td>
<td>${sign.signTime}</td>
<td>${sign.status}</td>
<td>
<a href="${pageContext.request.contextPath}/sign/deleteSign/${sign.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<hr/>
<a href="${pageContext.request.contextPath}/sign/list">查看所有学生</a>
<a href="${pageContext.request.contextPath}/sign/toSign">学生签到</a>
</body>
</html>
```
这样,我们就完成了一个简单的课堂签到管理系统的实现,使用了SpringMVC框架和JDBC进行数据库操作。
阅读全文