一:项目介绍
1、系统管理(包括系统用户登录、退出系统等)
2、班级管理(对班级基本信息的录入、更新、删除)
3、学生管理(对学生信息的录入、更新、删除)注:每个学生属于一个班级
4、学生课程成绩管理(对课程成绩的录入、更新、删除、查询)
1.实现的技术介绍:
Maven,JavaWeb,Jsp,Html,Mybatis,Mysql,Tomcat
2.功能展示:
登录和注册界面
使用系统界面:
学生信心查询界面:
学生成绩信息查询界面:
二:环境配置
1.数据库表的创建:
学生表:
班级表:
课程表;
班级表:
成绩表:
2.项目环境的搭建:
使用工具:IntelliJ IDEA 2023.1.2
1.创建Maven项目,并搭建web环境
如果是第一次使用maven则可能需要手动配置
2.配置TomCat环境
3.引入依赖
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
<scope>compile</scope>
</dependency>
4.配置文件
generatorConfig.xml(逆向工程配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!--防⽌⽣成重复代码-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"/>
<commentGenerator>
<!--是否去掉⽣成⽇期-->
<property name="suppressDate" value="true"/>
<!--是否去除注释-->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--连接数据库信息-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/manager"
userId="root"
password="abc1234">
</jdbcConnection>
<!-- ⽣成pojo包名和位置 -->
<javaModelGenerator targetPackage="com.org.mybatis.pojo" targetProject="src/main/java">
<!--是否开启⼦包-->
<property name="enableSubPackages" value="true"/>
<!--是否去除字段名的前后空⽩-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- ⽣成SQL映射⽂件的包名和位置 -->
<sqlMapGenerator targetPackage="com.org.mybatis.mapper" targetProject="src/main/resources">
<!--是否开启⼦包-->
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- ⽣成Mapper接⼝的包名和位置 -->
<javaClientGenerator
type="xmlMapper"
targetPackage="com.org.mybatis.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 表名和对应的实体类名-->
<table tableName="t_clazz" domainObjectName="Clazz"/>
<table tableName="t_course" domainObjectName="Course"/>
<table tableName="t_student" domainObjectName="Student"/>
<table tableName="t_user" domainObjectName="User"/>
<table tableName="t_score" domainObjectName="score"/>
</context>
</generatorConfiguration>
jdbc.properties(连接数据库的配置信息)
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/manager
jdbc.user=root
jdbc.password=abc1234
logback.xml(日志配置信息)
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!--定义⽇志⽂件的存储地址-->
<property name="LOG_HOME" value="/home"/>
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示⽇期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:⽇志消息,%n是换⾏符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
</pattern>
</encoder>
</appender>
<!--mybatis log configure-->
<logger name="com.apache.ibatis" level="TRACE"/>
<logger name="java.sql.Connection" level="DEBUG"/>
<logger name="java.sql.Statement" level="DEBUG"/>
<logger name="java.sql.PreparedStatement" level="DEBUG"/>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE"/>
</root>
</configuration>
mybatis-config.xml(mybatis核心配置文件)
<?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>
<properties resource="jdbc.properties"/>
<!-- 驼峰命名和延迟加载-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.org.mybatis.pojo"/>
</typeAliases>
<!--分页插件配置-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.org.mybatis.mapper"/>
</mappers>
</configuration>
5.使用逆向工程⽣成Java的pojo类,SqlMapper.xml⽂件,以及Mapper接⼝类
![](https://i-blog.csdnimg.cn/blog_migrate/a2b84038f383453bbe14e0af7bce5429.png)
3.项目结构预览
三.功能实现
1.登录界面
login.jsp
<!DOCTYPE html>
<html lang="en">
<!-- https://codepen.io/danielkvist/pen/LYNVyPL -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
:root {
/* COLORS */
--white: #e9e9e9;
--gray: #333;
--blue: #0367a6;
--lightblue: #008997;
/* RADII */
--button-radius: 0.7rem;
/* SIZES */
--max-width: 758px;
--max-height: 420px;
font-size: 16px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
body {
align-items: center;
background-color: var(--white);
background: url("https://gss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/c2fdfc039245d68837bd625aa2c27d1ed31b2495.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.form__title {
font-weight: 300;
margin: 0;
margin-bottom: 1.25rem;
}
.link {
color: var(--gray);
font-size: 0.9rem;
margin: 1.5rem 0;
text-decoration: none;
}
.container {
background-color: var(--white);
border-radius: var(--button-radius);
box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25),
0 0.7rem 0.7rem rgba(0, 0, 0, 0.22);
height: var(--max-height);
max-width: var(--max-width);
overflow: hidden;
position: relative;
width: 100%;
}
.container__form {
height: 100%;
position: absolute;
top: 0;
transition: all 0.6s ease-in-out;
}
.container--signin {
left: 0;
width: 50%;
z-index: 2;
}
.container.right-panel-active .container--signin {
transform: translateX(100%);
}
.container--signup {
left: 0;
opacity: 0;
width: 50%;
z-index: 1;
}
.container.right-panel-active .container--signup {
animation: show 0.6s;
opacity: 1;
transform: translateX(100%);
z-index: 5;
}
.container__overlay {
height: 100%;
left: 50%;
overflow: hidden;
position: absolute;
top: 0;
transition: transform 0.6s ease-in-out;
width: 50%;
z-index: 100;
}
.container.right-panel-active .container__overlay {
transform: translateX(-100%);
}
.overlay {
background-color: var(--lightblue);
background: url("https://cdn.pixabay.com/photo/2018/08/14/13/23/ocean-3605547_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
left: -100%;
position: relative;
transform: translateX(0);
transition: transform 0.6s ease-in-out;
width: 200%;
}
.container.right-panel-active .overlay {
transform: translateX(50%);
}
.overlay__panel {
align-items: center;
display: flex;
flex-direction: column;
height: 100%;
justify-content: center;
position: absolute;
text-align: center;
top: 0;
transform: translateX(0);
transition: transform 0.6s ease-in-out;
width: 50%;
}
.overlay--left {
transform: translateX(-20%);
}
.container.right-panel-active .overlay--left {
transform: translateX(0);
}
.overlay--right {
right: 0;
transform: translateX(0);
}
.container.right-panel-active .overlay--right {
transform: translateX(20%);
}
.btn {
background-color: var(--blue);
background-image: linear-gradient(90deg, var(--blue) 0%, var(--lightblue) 74%);
border-radius: 20px;
border: 1px solid var(--blue);
color: var(--white);
cursor: pointer;
font-size: 0.8rem;
font-weight: bold;
letter-spacing: 0.1rem;
padding: 0.9rem 4rem;
text-transform: uppercase;
transition: transform 80ms ease-in;
}
.form>.btn {
margin-top: 1.5rem;
}
.btn:active {
transform: scale(0.95);
}
.btn:focus {
outline: none;
}
.form {
background-color: var(--white);
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 0 3rem;
height: 100%;
text-align: center;
}
.input {
background-color: #fff;
border: none;
padding: 0.9rem 0.9rem;
margin: 0.5rem 0;
width: 100%;
}
@keyframes show {
0%,
49.99% {
opacity: 0;
z-index: 1;
}
50%,
100% {
opacity: 1;
z-index: 5;
}
}
</style>
</head>
<body>
<div class="container right-panel-active">
<!-- Sign Up -->
<div class="container__form container--signup">
<form action="/manager/signup" method="post" class="form" id="form1">
<h2 class="form__title">Sign Up</h2>
<input type="text" name="User" placeholder="User" class="input" />
<input type="password" name="Password" placeholder="Password" class="input" />
<button type="submit"class="btn" value="signup">signup</button>
</form>
</div>
<!-- Sign In -->
<div class="container__form container--signin">
<form action="/manager/signin" method="post" class="form" id="form2">
<h2 class="form__title">Sign In</h2>
<input type="User" name="User" placeholder="User" class="input" />
<input type="password" name="Password" placeholder="Password" class="input" />
<button type="submit"class="btn" value="signin">signin</button>
</form>
</div>
<!-- Overlay -->
<div class="container__overlay">
<div class="overlay">
<div class="overlay__panel overlay--left">
<button class="btn" id="signIn">Sign In</button>
</div>
<div class="overlay__panel overlay--right">
<button class="btn" id="signUp">Sign Up</button>
</div>
</div>
</div>
</div>
<script>
const signInBtn = document.getElementById("signIn");
const signUpBtn = document.getElementById("signUp");
const fistForm = document.getElementById("form1");
const secondForm = document.getElementById("form2");
const container = document.querySelector(".container");
signInBtn.addEventListener("click", () => {
container.classList.remove("right-panel-active");
});
signUpBtn.addEventListener("click", () => {
container.classList.add("right-panel-active");
});
</script>
</body>
</html>
SignInServlet
package com.org.mybatis.servlet;
/**
* 用户登录
* 目前还存在的问题:
* 1.可用添加一个十天面登录的功能
*/
import com.org.mybatis.mapper.UserMapper;
import com.org.mybatis.pojo.User;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
@WebServlet("/signin")
public class SignInServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取sqlsession和代理对象mapper
SqlSession sqlSession = SqlSessionUtils.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//获取网页post提交的数据
Integer uaco= Integer.valueOf(request.getParameter("User"));
String upas= request.getParameter("Password");
//封装成user对象
User user=new User(uaco,upas);
//通过输入的账户名称在数据库中进行查询
//如果为空则提示不存在该用户请重新输入,如果存在user则进行密码比对
if(mapper.selectByKey(uaco)==null){
request.getRequestDispatcher("/error.jsp").forward(request,response);;
}
User selectUser = mapper.selectByKey(uaco);
//进行密码配对如果密码一致则登录成功,否则提示重新登录
if(selectUser.getUpas().equals(upas)){
HttpSession session = request.getSession();
session.setAttribute("uaco",uaco);
request.getRequestDispatcher("/welcomeUse.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
}
}
SignUpServlet
package com.org.mybatis.servlet;
import com.org.mybatis.mapper.UserMapper;
import com.org.mybatis.pojo.User;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
@WebServlet("/signup")
public class SignUpServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Integer uaco= Integer.valueOf(request.getParameter("User"));
String upas= request.getParameter("Password");
User user=new User(uaco,upas);
int count = mapper.insert(user);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
HttpSession session = request.getSession();
session.setAttribute("uaco",uaco);
request.getRequestDispatcher("/welcomeUse.jsp").forward(request,response);
}
}
2.用户使用界面
welcomeUse.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎使用学生信息管理系统</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.logout{
width: 250px;
height: 55px;
position: absolute;
top: 0px;
left: 0px;
}
</style>
</head>
<body>
<h1 align="center">欢迎使用学生信息管理系统</h1>
请选择您想要进行的操作<br>
<a href="insert.jsp">增加信息</a><br>
<a href="delete.jsp">删除信息</a><br>
<a href="update.jsp">修改信息</a><br>
<a href="select.jsp">查询信息</a><br>
<a href="javascript:history.go(-1);">返回</a>
<div class="logout">
<a href="logout.jsp">退出系统</a>
</div>
</body>
</html>
3.添加界面
添加功能的主页面 insert.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>增加信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<body>
<h1 align="center">欢迎使用学生信息管理系统</h1>
请选择您想要进行的操作<br>
<a href="${pageContext.request.contextPath}/insert/insertStu.jsp">增加学生信息</a><br>
<a href="${pageContext.request.contextPath}/insert/insertCla.jsp">增加班级信息</a><br>
<a href="${pageContext.request.contextPath}/insert/insertCou.jsp">增加课程信息</a><br>
<a href="${pageContext.request.contextPath}/insert/insertSco.jsp">增加学生成绩信息</a><br>
<a href="javascript:history.go(-1);">返回</a>
</body>
</html>
insertCla.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>添加班级信息</title>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/insertCla" >
<div >
<div class = "name">添加班级信息</div>
<div class = "item">
<input type = "text" name="Cid" placeholder="请输入班级编号"/>
</div>
<div class = "item">
<input type = "text" name="Cname" placeholder="请输入班级名称"/>
</div>
<div class = "item">
<input type="text" name="Cnum" placeholder="请输入班级人数"/>
</div>
<input type = "submit" class = "btn" value = "添加"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
insertCou.jsp
<!DOCTYPE html>
<html>
<head>
<title>添加课程信息</title>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/insertCou" >
<div >
<div class = "name">添加课程信息</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输入课程编号"/>
</div>
<div class = "item">
<input type = "text" name="Couname" placeholder="请输入课程名称"/>
</div>
<input type = "submit" class = "btn" value = "添加"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
insertSco.jsp
<!DOCTYPE html>
<html>
<head>
<title>添加学生成绩信息</title>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/insertSou" >
<div >
<div class = "name">添加学生成绩信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入学生编号"/>
</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输入课程编号"/>
</div>
<div class = "item">
<input type = "text" name="Score" placeholder="请输入成绩"/>
</div>
<input type = "submit" class = "btn" value = "添加"/><br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
insertStu.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>添加学生信息</title>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/insertStu" >
<div >
<div class = "name">添加学生信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入学生编号"/>
</div>
<div class = "item">
<input type = "text" name="Sname" placeholder="请输入姓名"/>
</div>
<div class = "item">
<input type = "text" name="Ssex" placeholder="请输入性别"/>
</div>
<div class = "item">
<input type = "date" name="Sdate" placeholder="请输入出生日期"/>
</div>
<div class = "item">
<input type = "text" name="Sloc" placeholder="请输入地区"/>
</div>
<div class = "item">
<input type = "text" name="Cid" placeholder="请输入班级编号"/>
</div>
<input type = "submit" class = "btn" value = "添加"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
ClazzInsertServlet
package com.org.mybatis.servlet.insert;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.pojo.Clazz;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 添加班级信息
* 目前存在的问题:
* 1.与课程表基本相同
*/
@WebServlet("/insertCla")
public class ClazzInsertServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//查询该班级是否已经存在
SqlSession sqlSession = SqlSessionUtils.openSession();
ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
Integer cid = Integer.valueOf(request.getParameter("Cid"));
String cname = request.getParameter("Cname");
Integer cnum = Integer.valueOf(request.getParameter("Cnum"));
boolean checked = Check.checkClazz(cid);
if(checked){
Clazz clazz=new Clazz(cid,cname,cnum);
int count = mapper.insert(clazz);
if(count==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}
else {
request.getRequestDispatcher("/errorExist/errorExistCla.jsp").forward(request,response);
}
}
catch (PersistenceException e){
request.getRequestDispatcher("/errorExist/errorExistCla.jsp").forward(request,response);
}
}
}
StuInsertServlet
package com.org.mybatis.servlet.insert;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.pojo.Clazz;
import com.org.mybatis.pojo.Student;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 添加学生信息
* 目前还存在的问题:
* 1.主键为sid所以用户在输入同一个sid时肯定会报错并且还没有处理输入空值的报错
* 2.外键为cid,在用户提交cid时判断是否存在cid并且判断cid的人数是否可用容纳该学生
* 3.还没有处理用户输入错误类型数据的报错
*/
@WebServlet("/insertStu")
public class StudentInsertServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
try {
SqlSession sqlSession = SqlSessionUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Integer sid = Integer.valueOf(request.getParameter("Sid"));
String sname = request.getParameter("Sname");
String ssex = request.getParameter("Ssex");
String sdate = request.getParameter("Sdate");
String sloc = request.getParameter("Sloc");
String cid = request.getParameter("Cid");
boolean checkSid = Check.checkSid(sid);
//先判断该学生是否存在
if (checkSid) {
boolean checkClazz = Check.checkClazz(Integer.valueOf(cid));
if(!checkClazz){
Student insertStu = new Student(sid, sname, ssex, sdate, sloc, cid);
//插入该学生后班级人数要增加一
ClazzMapper mapper1 = sqlSession.getMapper(ClazzMapper.class);
Clazz clazz = mapper1.selectByCid(Integer.valueOf(cid));
Integer cnum = clazz.getCnum();
cnum = cnum + 1;
int count1 = mapper1.updateNum(Integer.valueOf(cid), cnum);
// 班级人数更新成功 (count1 为1)
int count = mapper.insert(insertStu);
if (count == 1&&count1==1) {
request.getRequestDispatcher("/Success.jsp").forward(request, response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}else {
request.getRequestDispatcher("/isNotExist/isNotExistCla.jsp").forward(request,response);
}
}
else {
request.getRequestDispatcher("/errorExist/errorExistStu.jsp").forward(request,response);
}
}
catch (PersistenceException e ){
e.printStackTrace();
}
}
}
CouzzInsertServlet
package com.org.mybatis.servlet.insert;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.CourseMapper;
import com.org.mybatis.pojo.Course;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 添加课程信息
* 目前还存在的问题:
* 1.与学生表差不多但存在不能输入同一个课程名的问题
*/
@WebServlet("/insertCou")
public class CouInsertServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//首先应该判断该课程是否还未被创建
SqlSession sqlSession = SqlSessionUtils.openSession();
CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
Integer couid = Integer.valueOf(request.getParameter("Couid"));
String couname = request.getParameter("Couname");
boolean check = Check.checkCourse(couid);
if (check) {
Course course = new Course(couid, couname);
int count = mapper.insert(course);
if (count == 1) {
request.getRequestDispatcher("/Success.jsp").forward(request, response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}
else {
request.getRequestDispatcher("/errorExist/errorExistCou.jsp").forward(request,response);
}
}
catch (PersistenceException e){
request.getRequestDispatcher("/errorExist/errorExistCou.jsp").forward(request,response);
}
}
}
ScozzInsertServlet
package com.org.mybatis.servlet.insert;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ScoreMapper;
import com.org.mybatis.pojo.Score;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 插入学生成绩
* 目前还存在的问题:
* 1.该标的 主键为复合主键,得通过查询学生表和课程表来添加学生成绩,还没有处理添加的学生或者课程不存在的情况
* 2.课程满分为100分,应该判断数据后添加
*/
@WebServlet("/insertSou")
public class ScoreInsertServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//先判断该学生该课程是否存在成绩
SqlSession sqlSession = SqlSessionUtils.openSession();
ScoreMapper mapper = sqlSession.getMapper(ScoreMapper.class);
Integer sid = Integer.valueOf(request.getParameter("Sid"));
Integer couid = Integer.valueOf(request.getParameter("Couid"));
Double score = Double.valueOf(request.getParameter("Score"));
Score scoreCheck = mapper.selectByPrimaryKey(sid, couid);
if(scoreCheck==null){
Score scoreE=new Score(sid,couid,score);
int count = mapper.insert(scoreE);
if(count==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}
else {
request.getRequestDispatcher("/errorExist/errorExistSco.jsp").forward(request,response);
}
}
catch (TooManyResultsException e){
request.getRequestDispatcher("/errorExist/errorExistSco.jsp").forward(request,response);
}
}
}
4.删除界面
删除功能的主界面 delete.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>删除信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<body>
<h1 align="center">欢迎使用学生信息管理系统</h1>
请选择您想要进行的操作<br>
<a href="${pageContext.request.contextPath}/delete/deleteStu.jsp">删除学生信息</a><br>
<a href="${pageContext.request.contextPath}/delete/deleteCla.jsp">删除班级信息</a><br>
<a href="${pageContext.request.contextPath}/delete/deleteCou.jsp">删除课程信息</a><br>
<a href="${pageContext.request.contextPath}/delete/deleteSco.jsp">删除学生成绩信息</a><br>
<a href="javascript:history.go(-1);">返回</a>
</body>
</html>
deleteCla.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>添加学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定删除该班级吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/deleteCla" >
<div >
<div class = "name">删除班级信息</div>
<div class = "item">
<input type = "text" name="Cid" placeholder="请输入班级编号"/>
</div>
<div class = "item">
<input type = "text" name="Cname" placeholder="请输入班级名称"/>
</div>
<input type="submit" onclick="openResult()" value="删除"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
deleteCou.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>添加学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定删除该课程吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/deleteCou" >
<div >
<div class = "name">删除课程信息</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输入课程编号"/>
</div>
<div class = "item">
<input type = "text" name="Couname" placeholder="请输入课程名称"/>
</div>
<input type="submit" onclick="openResult()" value="删除"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
deleteSco.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>添加学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定删除该学生的成绩吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/deleteSco" >
<div >
<div class = "name">删除学生成绩信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入学生编号"/>
</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输入课程编号"/>
</div>
<input type="submit" onclick="openResult()" value="删除"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
deleteStu.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>添加学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定删除该学生的所有信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/deleteStu" >
<div >
<div class = "name">删除学生信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入学生编号"/>
</div>
<div class = "item">
<input type = "text" name="Sname" placeholder="请输入姓名"/>
</div>
<input type="submit" onclick="openResult()" value="删除"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
ClazzDeleteServlet
package com.org.mybatis.servlet.delete;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.pojo.Student;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 删除班级信息
* 首先查询该班级是否存在
* 在删除还有学生的班级时应该提示用户先将在该班级的学生修改至其他班级再进行删除该班级的操作
*/
@WebServlet("/deleteCla")
public class ClazzDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
StudentMapper mapper1 = sqlSession.getMapper(StudentMapper.class);
//查询该班级是否存在
Integer cid = Integer.valueOf(request.getParameter("Cid"));
boolean checked = Check.checkClazz(cid);
//如果改班级存在查询该班级内是否还有学生
if(!checked){
//查询该班级内是否还存在学生,如果有则提示用户先进行更新操作再删除班级
if(mapper1.selectByCid(cid)!=null){
request.getRequestDispatcher("/isExist/isExistStu.jsp");
}
else {
//删除班级
int count = mapper.deleteByCid(cid);
if(count==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/delete.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}
}else {
request.getRequestDispatcher("/isNotExist/isNotExistCla.jsp").forward(request,response);
}
}
}
CourseDeleteServlet
package com.org.mybatis.servlet.delete;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.CourseMapper;
import com.org.mybatis.mapper.ScoreMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 删除课程
* 如果该课程还有学生在学习则提醒用户先进行更新操作再删除该课程
*/
@WebServlet("/deleteCou")
public class CourseDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
ScoreMapper mapper1 = sqlSession.getMapper(ScoreMapper.class);
//先查询该课程是否存在
Integer couid = Integer.valueOf(request.getParameter("Couid"));
boolean checked = Check.checkCourse(couid);
if(!checked){
//查询该课程是否有学生有成绩
if(mapper1.selecyByCouid(couid)!=null){
request.getRequestDispatcher("/isExist/isExistStu.jsp");
}
//没有学生有该课程的成绩,进行删除操作
int count = mapper.deleteByPrimaryKey(couid);
if(count==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/delete.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}else {
request.getRequestDispatcher("/isNotExist/isNotExistCou.jsp").forward(request,response);
}
}
}
ScoreDeleteServlet
package com.org.mybatis.servlet.delete;
import com.org.mybatis.mapper.ScoreMapper;
import com.org.mybatis.pojo.Score;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 删除学生成绩
* 查询该学生该课程是否存在成绩
*/
@WebServlet("/deleteSco")
public class ScoreDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
ScoreMapper mapper = sqlSession.getMapper(ScoreMapper.class);
//查询该学生该课程是否存在成绩
Integer sid = Integer.valueOf(request.getParameter("Sid"));
Integer couid = Integer.valueOf(request.getParameter("Couid"));
Score score = mapper.selectByPrimaryKey(sid, couid);
if(score!=null){
//存在成绩,删除该学生该课程的成绩
int count = mapper.deleteByPrimaryKey(sid, couid);
if(count==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/delete.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}
else {
request.getRequestDispatcher("/isNotExist/isNotExistSco.jsp").forward(request,response);
}
}
}
StudentDeleteServlet
package com.org.mybatis.servlet.delete;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.ScoreMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.pojo.Clazz;
import com.org.mybatis.pojo.Student;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
/**
* 删除学生信息
* 需要注意:
* 在删除时应得到学生的id和姓名,然后删除该学生的基本信息后还应该在其他表中删除该学生的信息
* 班级表的人数应该减一(需要用到查询操作和更新操作)
* 先查询学生表得到班级id后查询班级表通过班级id让num减一
* 课程表不受影响
* 成绩表也应该删除该学生有关的所有成绩
* 还存在的问题:
*/
@WebServlet("/deleteStu")
public class StudentDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
ScoreMapper mapper1 = sqlSession.getMapper(ScoreMapper.class);
ClazzMapper mapper2 = sqlSession.getMapper(ClazzMapper.class);
Integer sid = Integer.valueOf(request.getParameter("Sid"));
boolean checked = Check.checkSid(sid);
//首先查询该学生是否存在,如果存在继续操作
if(!checked){
//更新班级的人数
Student student = mapper.selectByPrimaryKey(sid);
Integer cid = Integer.valueOf(student.getCid());
Clazz clazz = mapper2.selectByCid(cid);
Integer cnum = clazz.getCnum();
cnum=cnum-1;
int count2 = mapper2.updateNum(cid,cnum);
//删除学生
int count = mapper.deleteByPrimaryKey(sid);
//更新成绩表中的数据
int count1 = mapper1.deleteBySid(sid);
if(count==1&&count1==1&&count2==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/delete.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}else {
request.getRequestDispatcher("/isNotExist/isNotExistStu.jsp").forward(request,response);
}
}
}
5.修改界面
更新主页面 update.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>更新信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<body>
<h1 align="center">欢迎使用学生信息管理系统</h1>
请选择您想要进行的操作<br>
<a href="${pageContext.request.contextPath}/update/updateStu.jsp">更新学生信息</a><br>
<a href="${pageContext.request.contextPath}/update/updateCla.jsp">更新班级信息</a><br>
<a href="${pageContext.request.contextPath}/update/updateCou.jsp">更新课程信息</a><br>
<a href="${pageContext.request.contextPath}/update/updateSco.jsp">更新学生成绩信息</a><br>
<a href="javascript:history.go(-1);">返回</a>
</body>
</html>
updateCla.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新该班级信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateCla" >
<div >
<div class = "name">更新班级信息</div>
<div class = "item">
<input type = "text" name="Cid" placeholder="请输入您想要更新的班级编号"/>
</div>
<div class = "item">
<input type = "text" name="CnameOid" placeholder="请输您想要更新的班级名称"/>
</div>
<div class = "item">
<input type = "text" name="CnameNew" placeholder="请输入更新后班级名称"/>
</div>
<div class = "item">
<input type = "text" name="Cnum" placeholder="请输入更新后班级人数"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateCou.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新课程信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateCou" >
<div >
<div class = "name">更新课程信息</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输入您想要更新的课程编号"/>
</div>
<div class = "item">
<input type = "text" name="CounameOid" placeholder="请输您想要更新的课程名称"/>
</div>
<div class = "item">
<input type = "text" name="CounameNew" placeholder="请输入更新后课程名称"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateSco.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新成绩信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateSco" >
<div >
<div class = "name">更新成金信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入您想要更新成绩的学生编号"/>
</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输您想要更新成绩的课程编号"/>
</div>
<div class = "item">
<input type = "text" name="scoreNew" placeholder="请输入更新后课程成绩"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateStu.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新该学生的所有信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateStu" >
<div >
<div class = "name">更新学生信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入您想要更新的学生编号"/>
</div>
<div class = "item">
<input type = "text" name="SnameOid" placeholder="请输您想要更新的学生姓名"/>
</div>
<div class = "item">
<input type = "text" name="SnameNew" placeholder="请输入更新后学生姓名"/>
</div>
<div class = "item">
<input type = "text" name="Ssex" placeholder="请输入更新后学生性别"/>
</div>
<div class = "item">
<input type = "date" name="Sdate" placeholder="请输入更新后学生出生日期"/>
</div>
<div class = "item">
<input type = "text" name="Sloc" placeholder="请输入更新后学生地区"/>
</div>
<div class = "item">
<input type = "text" name="Cid" placeholder="请输入更新后学生班级编号"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateCla.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新该班级信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateCla" >
<div >
<div class = "name">更新班级信息</div>
<div class = "item">
<input type = "text" name="Cid" placeholder="请输入您想要更新的班级编号"/>
</div>
<div class = "item">
<input type = "text" name="CnameOid" placeholder="请输您想要更新的班级名称"/>
</div>
<div class = "item">
<input type = "text" name="CnameNew" placeholder="请输入更新后班级名称"/>
</div>
<div class = "item">
<input type = "text" name="Cnum" placeholder="请输入更新后班级人数"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateCou.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新课程信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateCou" >
<div >
<div class = "name">更新课程信息</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输入您想要更新的课程编号"/>
</div>
<div class = "item">
<input type = "text" name="CounameOid" placeholder="请输您想要更新的课程名称"/>
</div>
<div class = "item">
<input type = "text" name="CounameNew" placeholder="请输入更新后课程名称"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateSco.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新成绩信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateSco" >
<div >
<div class = "name">更新成金信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入您想要更新成绩的学生编号"/>
</div>
<div class = "item">
<input type = "text" name="Couid" placeholder="请输您想要更新成绩的课程编号"/>
</div>
<div class = "item">
<input type = "text" name="scoreNew" placeholder="请输入更新后课程成绩"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateStu.jsp
<%--
Created by IntelliJ IDEA.
User: han696
Date: 2023/8/12
Time: 15:27
To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<html>
<head>
<title>更新学生信息</title>
<script>
function openResult(){
var r = confirm("亲,您确定更新该学生的所有信息吗?")
if (r == true) {
window.location.href="delete.jsp"
} else {
}
}
</script>
<style>
.titleDiv{
font-weight:bold;
color:white;
font-size:25px
}
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
.name{
font-size: 12px;
}
.item{
height:60px;
}
.item input{
line-height:40px;
width:245px;
border:none;
border-bottom: 1px solid #59c2c5;
margin-bottom: 20px;
margin-top: 10px;
}
.btn{
display:block;
width:255px;
height:50px;
color:#fff;
background:#59c2c5;
font-size:16px;
line-height:30px;
text-align:center;
border-radius:10px;
border:none;
margin-top: 10px;
}
a{
font-size:18px;
padding-left:200px;
padding-bottom: 50px;
color:#59c2c5;
}
</style>
</head>
<body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div >
<div class="titleDiv">学生信息管理平台</div>
<form action = "/manager/updateStu" >
<div >
<div class = "name">更新学生信息</div>
<div class = "item">
<input type = "text" name="Sid" placeholder="请输入您想要更新的学生编号"/>
</div>
<div class = "item">
<input type = "text" name="SnameOid" placeholder="请输您想要更新的学生姓名"/>
</div>
<div class = "item">
<input type = "text" name="SnameNew" placeholder="请输入更新后学生姓名"/>
</div>
<div class = "item">
<input type = "text" name="Ssex" placeholder="请输入更新后学生性别"/>
</div>
<div class = "item">
<input type = "date" name="Sdate" placeholder="请输入更新后学生出生日期"/>
</div>
<div class = "item">
<input type = "text" name="Sloc" placeholder="请输入更新后学生地区"/>
</div>
<div class = "item">
<input type = "text" name="Cid" placeholder="请输入更新后学生班级编号"/>
</div>
<input type="submit" onclick="openResult()" value="更新"/>
<br>
<a href="javascript:history.go(-1);">返回</a>
</div>
</form>
</div>
</div>
</body>
</html>
updateClaServlet
package com.org.mybatis.servlet.update;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.pojo.Clazz;
import com.org.mybatis.pojo.Student;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
/**
* 更新班级信息
* 查询需要更新的班级是否存在
* 如果存在后查询想要更新的班级id是否被占用
* 如果没被占用更新学生表中的cid
*/
@WebServlet("/updateCla")
public class updateClaServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
ClazzMapper clazzMapper = sqlSession.getMapper(ClazzMapper.class);
Integer cid = Integer.valueOf(request.getParameter("Cid"));
String cnameNew = request.getParameter("CnameNew");
Integer cnum = Integer.valueOf(request.getParameter("Cnum"));
boolean checkedOld = Check.checkClazz(cid);
if(!checkedOld){
//更新学生表中的cid后更新班级表
List<Student> students = studentMapper.selectByCid(cid);
if(students!=null){
int updateStu = 0;
for (Student s : students) {
Integer sid = s.getSid();
updateStu = studentMapper.updateByCid(sid, cid);
}
}
//更新完学生表更新班级表
Clazz newClazz=new Clazz(cid,cnameNew,cnum);
int updateCLa = clazzMapper.updateCla(newClazz);
if (updateCLa==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}else {
request.getRequestDispatcher("/isNotExist/isNotExistCla.jsp").forward(request,response);
}
}
}
updateCouServlet
package com.org.mybatis.servlet.update;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.CourseMapper;
import com.org.mybatis.mapper.ScoreMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.pojo.Course;
import com.org.mybatis.pojo.Score;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
/**
* 更新课程
* 需要更新操作的课程id是否存在
* 如果存在查询更新后的id是否被占用
*
* 更新课程和成绩
*/
@WebServlet("/updateCou")
public class updateCouServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
Integer couid = Integer.valueOf(request.getParameter("Couid"));
String counameOid = request.getParameter("CounameOid");
String counameNew = request.getParameter("CounameNew");
boolean check= Check.checkCourse(couid);
if(!check) {
//存在,进行更新
Course newCourse = new Course(couid, counameNew);
int update = courseMapper.updateByPrimaryKey(newCourse);
if (update == 1) {
request.getRequestDispatcher("/Success.jsp").forward(request, response);
} else {
request.getRequestDispatcher("/error.jsp").forward(request, response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}
else {
request.getRequestDispatcher("/isNotExist/isNotExistCou.jsp").forward(request,response);
}
}
}
updateScoServlet
package com.org.mybatis.servlet.update;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ScoreMapper;
import com.org.mybatis.pojo.Score;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 更新成绩信息
* 查询需要更新的学生该课程成绩是否存在
* 存在后更新
*/
@WebServlet("/updateSco")
public class updateScoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
ScoreMapper scoreMapper = sqlSession.getMapper(ScoreMapper.class);
Integer sid = Integer.valueOf(request.getParameter("Sid"));
Integer couid = Integer.valueOf(request.getParameter("Couid"));
Double scoreNew = Double.valueOf(request.getParameter("scoreNew"));
boolean checked = Check.checkScore(sid, couid);
if(!checked){
//成绩存在更新成绩
Score score=new Score(sid,couid,scoreNew);
int update = scoreMapper.updateByPrimaryKey(score);
if(update==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}else {
request.getRequestDispatcher("/isNotExist/isNotExistSco.jsp").forward(request,response);
}
}
}
updateStuServlet
package com.org.mybatis.servlet.update;
import com.org.mybatis.check.Check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.pojo.Clazz;
import com.org.mybatis.pojo.Student;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
/**
* 更新学生信息
* 1.首先应该查询想要更新的学生是否存在
* 2.更新学生的班级:
* 应该确认更新后的班级是否存在,如果存在则转出班级人数减一,转出班级人数加一
* 不存在则提示用户不存在这样的班级
*/
@WebServlet("/updateStu")
public class updateStuServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
StudentMapper mapperStu = sqlSession.getMapper(StudentMapper.class);
ClazzMapper mapperCla = sqlSession.getMapper(ClazzMapper.class);
//查询想要更新的学生是否存在
Integer sid= Integer.valueOf(request.getParameter("Sid"));
boolean checked = Check.checkSid(sid);
if(!checked){
//该学生确实存在后应该查询该学生想要更新的id是否被占用
Integer sidNew = Integer.valueOf(request.getParameter("SidNew"));
boolean checkedNew = Check.checkSid(sidNew);
String cid = request.getParameter("Cid");
boolean checkedCla = Check.checkClazz(Integer.valueOf(cid));
if(!checkedCla){
//该班级存在,更新班级的人数
//将旧班级的人数减一
Student studentOld = mapperStu.selectByPrimaryKey(sid);
String oldCid = studentOld.getCid();
Clazz clazzOld = mapperCla.selectByCid(Integer.valueOf(oldCid));
Integer cnumOld = clazzOld.getCnum();
cnumOld=cnumOld-1;
int updateOldNum = mapperCla.updateNum(cnumOld, cnumOld);
//新班级人数加一
Clazz clazzNew = mapperCla.selectByCid(Integer.valueOf(cid));
Integer newCnum = clazzNew.getCnum();
int updateNewNum = mapperCla.updateNum(Integer.valueOf(cid), newCnum);
//更新班级的基本信息
String snameNew = request.getParameter("SnameNew");
String ssex = request.getParameter("Ssex");
String sdate = request.getParameter("Sdate");
String sloc = request.getParameter("Sloc");
Student studentNew=new Student(sidNew,snameNew,ssex,sdate,sloc,cid);
int updateStu = mapperStu.updateByPrimaryKey(studentNew);
if(updateNewNum==1&&updateOldNum==1&&updateStu==1){
request.getRequestDispatcher("/Success.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/error.jsp").forward(request,response);
}
sqlSession.commit();
SqlSessionUtils.close(sqlSession);
}else {
request.getRequestDispatcher("/isNotExist/isNotExistCla.jsp").forward(request,response);
}
}else {
request.getRequestDispatcher("/isNotExist/isNotExistStu.jsp").forward(request,response);
}
}
}
6..查询界面
查询总页面 select.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查询信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<body>
<h1 align="center">欢迎使用学生信息管理系统</h1>
请选择您想要进行的操作<br>
<a href="<%=request.getContextPath()%>/selectStu">查询学生所有信息</a><br>
<a href="<%=request.getContextPath()%>/selectCla">查询班级信息</a><br>
<a href="<%=request.getContextPath()%>/selectCou">查询课程信息</a><br>
<a href="<%=request.getContextPath()%>/selectSco">查询学生成绩信息</a><br>
<a href="javascript:history.go(-1);">返回</a>
</body>
</html>
selectCla.jsp
<%@ page import="java.util.List" %>
<%@ page import="com.org.mybatis.pojo.Student" %>
<%@ page import="org.apache.ibatis.annotations.SelectKey" %>
<%@ page import="com.github.pagehelper.PageInfo" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>查询信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<body>
<h2>全部班级信息展示</h2>
<table border="1px" width="800px">
<!-- 第一行-->
<td>班级编号</td>
<td>班级名称</td>
<td>班级人数</td>
<td></td>
<tr>
<c:forEach items="${requestScope.clazze }" var="clazze">
<td>${clazze.cid}</td>
<td>${clazze.cname}</td>
<td>${clazze.cnum}</td>
<td><a href="${pageContext.request.contextPath}/delete/deleteCla.jsp">删除</a>
<a href="${pageContext.request.contextPath}/update/updateCla.jsp">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
selectCou.jsp
<%@ page import="java.util.List" %>
<%@ page import="com.org.mybatis.pojo.Student" %>
<%@ page import="org.apache.ibatis.annotations.SelectKey" %>
<%@ page import="com.github.pagehelper.PageInfo" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>查询信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<body>
<h2>全部课程信息展示</h2>
<table border="1px" width="800px">
<!-- 第一行-->
<td>课程编号</td>
<td>课程名称</td>
<td></td>
<tr>
<c:forEach items="${requestScope.course }" var="course">
<td>${course.couid}</td>
<td>${course.couname}</td>
<td><a href="${pageContext.request.contextPath}/delete/deleteCou.jsp">删除</a>
<a href="${pageContext.request.contextPath}/update/updateCou.jsp">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
selectSco.jsp
<%@ page import="java.util.List" %>
<%@ page import="com.org.mybatis.pojo.Student" %>
<%@ page import="org.apache.ibatis.annotations.SelectKey" %>
<%@ page import="com.github.pagehelper.PageInfo" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>查询信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<script>
function showScore(){
requestScope.scoreStu
}
</script>
<body>
<h2>全部成绩信息展示</h2>
<table border="1px" width="800px">
<!-- 第一行-->
<td>学生编号</td>
<td>学生姓名</td>
<td>课程编号</td>
<td>课程名称</td>
<td>课程成绩</td>
<td></td>
<c:forEach items="${requestScope.scoreStu }" var="scoreStu">
<tr>
<td>${scoreStu.student.sid}</td>
<td>${scoreStu.student.sname}</td>
<c:forEach items="${requestScope.scoreCou }" var="scoreCou">
<td>${scoreCou.course.couid}</td>
<td>${scoreCou.course.couname}</td>
<td>${scoreCou.score}</td>
<td><a href="${pageContext.request.contextPath}/delete/deleteCla.jsp">删除</a>
<a href="${pageContext.request.contextPath}/update/updateCla.jsp">修改</a>
</td>
</c:forEach>
</c:forEach>
</tr>
</table>
</body>
</html>
selectStu.jsp
<%@ page import="java.util.List" %>
<%@ page import="com.org.mybatis.pojo.Student" %>
<%@ page import="org.apache.ibatis.annotations.SelectKey" %>
<%@ page import="com.github.pagehelper.PageInfo" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>查询信息界面</title>
<style>
body {
align-items: center;
background-color: var(--white);
background: url("https://cdn.pixabay.com/photo/2020/05/06/06/18/blue-5136251_1280.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
display: grid;
height: 100vh;
place-items: center;
}
</style>
</head>
<body>
<h2>全部学生信息展示</h2>
<table border="1px" width="800px">
<!-- 第一行-->
<td>学生编号</td>
<td>学生姓名</td>
<td>学生性别</td>
<td>学生出生日期</td>
<td>学生籍贯</td>
<td>学生所在班级编号</td>
<td>学生所在班级名称</td>
<td></td>
<tr>
<c:forEach items="${requestScope.stus }" var="stu">
<td>${stu.sid}</td>
<td>${stu.sname}</td>
<td>${stu.ssex}</td>
<td>${stu.sdate}</td>
<td>${stu.sloc}</td>
<td>${stu.clazz.cid}</td>
<td>${stu.clazz.cname}</td>
<td><a href="${pageContext.request.contextPath}/delete/deleteStu.jsp">删除</a>
<a href="${pageContext.request.contextPath}/update/updateStu.jsp">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
selectClaServlet
package com.org.mybatis.servlet.select;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.studentAllMapper;
import com.org.mybatis.pojo.Clazz;
import com.org.mybatis.pojo.studentAll;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectCla")
public class selectCla extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
List<Clazz> clazzes = mapper.selectAll();
request.setAttribute("clazze",clazzes);
request.getRequestDispatcher("/select/selectCla.jsp").forward(request,response);
SqlSessionUtils.close(sqlSession);
}
}
selectCouServlet
package com.org.mybatis.servlet.select;
import com.org.mybatis.mapper.CourseMapper;
import com.org.mybatis.pojo.Course;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectCou")
public class selectCou extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
List<Course> courses = mapper.selectAll();
request.setAttribute("course",courses);
request.getRequestDispatcher("/select/selectCou.jsp").forward(request,response);
SqlSessionUtils.close(sqlSession);
}
}
selectScoServlet
package com.org.mybatis.servlet.select;
import com.org.mybatis.mapper.scoreAllMapper;
import com.org.mybatis.pojo.scoreAll;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectSco")
public class selectSco extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
scoreAllMapper mapper = sqlSession.getMapper(scoreAllMapper.class);
List<scoreAll> scoreAlls = mapper.selectAll();
request.setAttribute("scoreAll",scoreAlls);
request.getRequestDispatcher("/select/selectScoAll.jsp").forward(request,response);
SqlSessionUtils.close(sqlSession);
}
}
selectStuServlet
package com.org.mybatis.servlet.select;
import com.org.mybatis.mapper.studentAllMapper;
import com.org.mybatis.pojo.studentAll;
import com.org.mybatis.utils.SqlSessionUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectStu")
public class selectStu extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
SqlSession sqlSession = SqlSessionUtils.openSession();
studentAllMapper mapper = sqlSession.getMapper(studentAllMapper.class);
List<studentAll> studentAlls = mapper.selectAll();
request.setAttribute("stus",studentAlls);
request.getRequestDispatcher("/select/selectStu.jsp").forward(request,response);
SqlSessionUtils.close(sqlSession);
}
}
6.登录过滤器
loginCheckFilter
package com.org.mybatis.filter;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
public class loginCheckFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse) resp;
String servletPath = request.getServletPath();
HttpSession session=request.getSession();
if("/login.jsp".equals(servletPath)||"/signin".equals(servletPath)||"/signup".equals(servletPath)||(session!=null&&session.getAttribute("uaco")!=null)){
chain.doFilter(request,response);
}else {
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
}
7.检查类(check)
package com.org.mybatis.check;
import com.org.mybatis.mapper.ClazzMapper;
import com.org.mybatis.mapper.CourseMapper;
import com.org.mybatis.mapper.ScoreMapper;
import com.org.mybatis.mapper.StudentMapper;
import com.org.mybatis.pojo.Score;
import com.org.mybatis.pojo.Student;
import com.org.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class Check {
private Check(){};
/**
* 检查学生是否已经存在,如果不存在为true,存在为FALSE
*/
public static boolean checkSid(Integer sid){
boolean exist=false;
SqlSession sqlSession = SqlSessionUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student = mapper.selectByPrimaryKey(sid);
if(student==null){
return true;
}
return false;
}
/**
* 检查该学生课程成绩是否已经存,如果不存在为true,存在为FALSE
*/
public static boolean checkScore(Integer sid,Integer couid){
boolean exist=false;
SqlSession sqlSession = SqlSessionUtils.openSession();
ScoreMapper mapper = sqlSession.getMapper(ScoreMapper.class);
if(mapper.selectByPrimaryKey(sid, couid)==null){
return true;
}
return exist;
}
/**
* 检查该课程是否已经存在,如果不存在为true,存在为FALSE
*/
public static boolean checkCourse(Integer couid){
boolean exist=false;
SqlSession sqlSession = SqlSessionUtils.openSession();
CourseMapper mapper = sqlSession.getMapper(CourseMapper.class);
if(mapper.selectByPrimaryKey(couid)==null){
return true;
}
return exist;
}
/**
* 检查该班级是否已经存在,如果不存在为true,存在为FALSE
*/
public static boolean checkClazz(Integer cid){
boolean exist=false;
SqlSession sqlSession = SqlSessionUtils.openSession();
ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
if(mapper.selectByCid(cid)==null){
exist=true;
return exist;
}
return exist;
}
}
8.工具类
SqlSessionUtils(线程控制)
package com.org.mybatis.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
public class SqlSessionUtils {
//全局的一个服务器定义一个即可
private static ThreadLocal<SqlSession> loc = new ThreadLocal<>();
private static SqlSessionFactory sqlSessionFactory;
private SqlSessionUtils() {
}
static {
try {
sqlSessionFactory = new SqlSessionFactoryBuilder().
build(Resources.getResourceAsStream("mybatis-config.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
//获取会话对象并返回
public static SqlSession openSession() {
SqlSession sqlSession = loc.get();
if (sqlSession == null) {
//sqlSessionFactory.openSession();后得到的是一个new出来的新的sqlSession
//保证一个sqlSession在一个线程中
sqlSession = sqlSessionFactory.openSession();
loc.set(sqlSession);
}
return sqlSession;
}
//关闭sqlsession,并从当前前程中移除
public static void close(SqlSession sqlSession) {
if (sqlSession != null) {
sqlSession.close();
//移除sqlsession对象并解除和线程的绑定关系
//tomcat是支持线程池的,也就是说用过的线程池对象t1,下次还会使用t1线程
loc.remove();
}
}
}
四.学习总结
至此 ,我们一个简单的学生管理系统网页的功能就简单的实现了,主要是暑期学习的一个小练习。由于使用的技术单一导致整个代码十分繁琐,而且功能也十分的有限,还需要很大的改善。
需要提供源码或者实验报告的同学可以私我
业精于勤,荒于嬉;行成于思,毁于随。