资源下载:https://download.csdn.net/download/weixin_44893902/45602690
练习点设计:模糊查询、删除、新增
一、语言和环境
- 实现语言:JAVA语言。
- 环境要求:MyEclipse/Eclipse + Tomcat + MySql。
- 使用技术:
Jsp
+Servle
t+JavaBean
或SpringMVC
+Spring
+Mybatis
。
二、实现功能
随着“朝阳会计培训学院”人数的增多,现需要制作学生管理系统,主要功能如下:
1.首页默认显示所有学员信息,如图所示。
2.鼠标悬停某行数据时,该数据显示橙色,如图所示。
3.用户输入学生姓名时,点击搜索,则完成模糊查询,显示查询结果,如图 3 所示。
4.用户点击删除,则弹出提示框,用户点击确定后,删除选中数据并显示最新数据,如图 4 和图 5 所示。
5. 用户点击“录入”链接,则打开新增页面,填写完相关信息后,如果不想录入点击“取消”回到主页,录入则点击录入按钮,增加学员信息数据到数据库,且页面跳转到列表页面展示最新数据,.
三、 数据库设计
- 创建数据库(student_db)。
- 创建数据表(student),结构如下。
四、推荐实现步骤
1.SSM 版本的实现步骤如下:
(1)创建数据库和数据表,添加测试数据(至少添加 5 条测试数据)。
(2)创建 Web 工程并创建各个包,导入工程所需的 jar 文件。
(3)添加相关 SSM 框架支持。
(4)配置项目所需要的各种配置文件(mybatis 配置文件、spring 配置文件、springMVC 配置文件)。
(5)创建实体类。
(6)创建 MyBatis 操作数据库所需的 Mapper 接口及其 Xml 映射数据库操作语句文件。 (7)创建业务逻辑相应的接口及其实现类,实现相应的业务,并在类中加入对 DAO/Mapper 的引用和注入。
(8)创建 Controller 控制器类,在 Controller 中添加对业务逻辑类的引用和注入,并配置 springMVC 配置文件。
(9)创建相关的操作页面,并使用 CSS 对页面进行美化。
(10)实现页面的各项操作功能,并在相关地方进行验证,操作要人性化。
(11)调试运行成功后导出相关的数据库文件并提交。
2.JSP 版本的实现步骤如下:
(1)按以上数据库要求建库、建表,并添加测试数据。
(2)创建 Web 工程并创建各个包,导入工程所需的 jar 文件。
(3)创建实体类。
(4)创建 Servlet 获取用户不同的请求,并将这些请求转发至业务处理层相应的业务方法。
(5)创建业务处理层,在其中定义业务方法实现系统需求,在这些业务方法中需要执行 DAO 方法。
(6)创建 BaseDAO 工具类,使用 JDBC 完成数据表数据的查询、删除和添加。
(7)编写 JSP 页面,展示数据的查询结果。
五、实现代码
1、MySQL数据库
student_db
/*
Date: 26/07/2021 19:44:05
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`studentName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`studentNo` int(11) NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
`gender` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`major` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`grade` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (2, '张美华', 135654621, 20, '0', '计算机应用', '一年级');
INSERT INTO `student` VALUES (3, '王子豪', 5315641, 20, '1', '计算机网络技术', '二年级');
INSERT INTO `student` VALUES (4, '李玉恒', 13515681, 19, '0', '计算机网络技术', '一年级');
INSERT INTO `student` VALUES (5, '王子豪', 13515681, 19, NULL, '计算机网络技术', '二年级');
SET FOREIGN_KEY_CHECKS = 1;
2、项目Java代码
目录结构
Student
JAR包:
src
com.controller
StudentController.java
package com.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 org.springframework.web.servlet.ModelAndView;
import com.entity.Student;
import com.services.StudentService;
@Controller
public class StudentController {
@Resource
StudentService studentservice;
@RequestMapping("student")
public ModelAndView select(String keyword) {
List<Student> selestudent = studentservice.selestudent(keyword);
if (keyword == null || keyword.trim().equals("")) {
keyword = "";
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("selestudent", selestudent);
modelAndView.setViewName("Student");
return modelAndView;
}
@RequestMapping("delstu")
public String delstu(Integer id) {
int delStudent = studentservice.delStudent(id);
return "redirect:/student.do";
}
@RequestMapping("addjsp")
public String addjsp() {
return "addStudent";
}
@RequestMapping("add")
public String add(Student student) {
int add = studentservice.add(student);
return "redirect:/student.do";
}
}
com.dao
StudentMapper.java
package com.dao;
import com.entity.Student;
import java.util.List;
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
Student selectByPrimaryKey(Integer id);
List<Student> selectAll();
int updateByPrimaryKey(Student record);
List<Student> likeStudents(String keyword);
}
StudentMapper.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.dao.StudentMapper" >
<resultMap id="BaseResultMap" type="com.entity.Student" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="studentName" property="studentname" jdbcType="VARCHAR" />
<result column="studentNo" property="studentno" jdbcType="INTEGER" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="gender" property="gender" jdbcType="INTEGER" />
<result column="major" property="major" jdbcType="VARCHAR" />
<result column="grade" property="grade" jdbcType="VARCHAR" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from student
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.entity.Student" >
insert into student (id, studentName, studentNo,
age, gender, major,
grade)
values (#{id,jdbcType=INTEGER}, #{studentname,jdbcType=VARCHAR}, #{studentno,jdbcType=INTEGER},
#{age,jdbcType=INTEGER}, #{gender,jdbcType=INTEGER}, #{major,jdbcType=VARCHAR},
#{grade,jdbcType=VARCHAR})
</insert>
<select id="selectAll" resultMap="BaseResultMap" >
select id, studentName, studentNo, age, gender, major, grade
from student
</select>
<select id="likeStudents" resultMap="BaseResultMap" >
select id, studentName, studentNo, age, gender, major, grade
from student where studentName like "%"#{keyword}"%"
</select>
</mapper>
com.entity
Student.java
package com.entity;
public class Student {
private Integer id;
private String studentname;
private Integer studentno;
private Integer age;
private Integer gender;
private String major;
private String grade;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname == null ? null : studentname.trim();
}
public Integer getStudentno() {
return studentno;
}
public void setStudentno(Integer studentno) {
this.studentno = studentno;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major == null ? null : major.trim();
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade == null ? null : grade.trim();
}
}
com.service.imp
StudentServiceImp.java
package com.service.imp;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dao.StudentMapper;
import com.entity.Student;
import com.services.StudentService;
@Service
public class StudentServiceImp implements StudentService {
@Resource
StudentMapper studenmapper;
@Override
public List<Student> selestudent(String keyword) {
if (keyword == null || keyword.trim().equals("")) {
List<Student> selectAll = studenmapper.selectAll();
return selectAll;
} else {
List<Student> likeStudents = studenmapper.likeStudents(keyword);
return likeStudents;
}
}
@Override
public int delStudent(Integer id) {
int deleteByPrimaryKey = studenmapper.deleteByPrimaryKey(id);
return deleteByPrimaryKey;
}
@Override
public int add(Student student) {
int insert = studenmapper.insert(student);
return insert;
}
}
com.services
StudentService.java
package com.services;
import java.util.List;
import com.entity.Student;
public interface StudentService {
List<Student> selestudent(String keyword);
int delStudent(Integer id);
int add(Student student);
}
mybatis
sqlMapConfig.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>
<!-- 别名 -->
<typeAliases>
<package name="com.entity" />
</typeAliases>
</configuration>
spring
applicationContext-dao.xml
<?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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 指定spring容器读取db.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 将连接池注册到bean容器中 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="Url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 设置MyBatis核心配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
<!-- 设置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Mapper扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 设置Mapper扫描包 -->
<property name="basePackage" value="com.dao" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解方式管理AOP事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
applicationContext-service.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 配置Service扫描 -->
<context:component-scan base-package="com" />
</beans>
spring-mvc.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<!-- 配置Controller扫描 -->
<context:component-scan base-package="com.controller" />
<!-- 配置注解驱动 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/student_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.jdbc.Driver
WebContent
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Student</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
JSP
Home.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path;
%>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<script type="text/javascript">
window.location.href="<%=basePath%>
/student.do";
</script>
</body>
</html>
addStudent.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>添加</title>
</head>
<body>
<form action="add.do" method="post">
姓名:<input type="text" name="studentname" value="${student.studentname }">
<br> 准考证号:
<input type="text" name="studentno" value="${student.studentno}">
<br> 学生年龄:
<input type="text" name="age" value="${student.age}">
<br> 学生性别:
<input type="radio" name="gender" value="1" <c:if test="${student.gender==1}">checked="checked"</c:if>>男
<input type="radio" name="gender" value="0" <c:if test="${student.gender!=1}">checked="checked"</c:if>>女
<br> 专业:
<input type="text" name="major" value="${student.major}">
<br> 年级:
<input type="text" name="grade" value="${student.grade}">
<br>
<div>
<input type="submit" value="添加">
</div>
</form>
</body>
</html>
Student.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>学生管理</title>
</head>
<body>
<form action="student.do">
<input type="text" name="keyword"><input type="submit" value="查询">
</form>
<table border="1px">
<tr>
<th>编号</th>
<th>学生姓名</th>
<th>准考证号</th>
<th>学生年龄</th>
<th>学生性别</th>
<th>专业</th>
<th>年级</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${selestudent }">
<tr>
<td>${student.id}</td>
<td>${student.studentname}</td>
<td>${student.studentno}</td>
<td>${student.age}</td>
<td>${student.gender} </td>
<td>${student.major}</td>
<td>${student.grade}</td>
<td>
<a href="delstu.do?id=${student.id }">删除</a>
<a href="addjsp.do">添加</a>
</td>
</tr>
</c:forEach>
</table>
<span>共${selestudent.size()}条数据</span>
</body>
</html>