資源下載: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>