案例:请假条管理系统2024-11-22
整体框架
pojo:Student:
点击查看代码
package com.QixunQiu.pojo;
public class Student {
int id ;
String StudentId ;
String StudentName ;
String StudentSex ;
String StudentLeave ;
String StudentCollege ;
String StudentSpecialty ;
String StudentClass ;
String StudentReason ;
String StudentDate ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public String getStudentId() {
return StudentId;
}
public void setStudentId(String studentId) {
StudentId = studentId;
}
public String getStudentSex() {
return StudentSex;
}
public void setStudentSex(String studentSex) {
StudentSex = studentSex;
}
public String getStudentClass() {
return StudentClass;
}
public void setStudentClass(String studentClass) {
StudentClass = studentClass;
}
public String getStudentLeave() {
return StudentLeave;
}
public void setStudentLeave(String studentLeave) {
StudentLeave = studentLeave;
}
public String getStudentCollege() {
return StudentCollege;
}
public void setStudentCollege(String studentCollege) {
StudentCollege = studentCollege;
}
public String getStudentSpecialty() {
return StudentSpecialty;
}
public void setStudentSpecialty(String studentSpecialty) {
StudentSpecialty = studentSpecialty;
}
public String getStudentReason() {
return StudentReason;
}
public void setStudentReason(String studentReason) {
StudentReason = studentReason;
}
public String getStudentDate() {
return StudentDate;
}
public void setStudentDate(String studentDate) {
StudentDate = studentDate;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", StudentId='" + StudentId + '\'' +
", StudentName='" + StudentName + '\'' +
", StudentSex='" + StudentSex + '\'' +
", StudentLeave='" + StudentLeave + '\'' +
", StudentCollege='" + StudentCollege + '\'' +
", StudentSpecialty='" + StudentSpecialty + '\'' +
", StudentClass='" + StudentClass + '\'' +
", StudentReason='" + StudentReason + '\'' +
", StudentDate='" + StudentDate + '\'' +
'}';
}
}
点击查看代码
package com.QixunQiu.service;
import com.QixunQiu.mapper.StudentMapper;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class StudentService {
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
public void add(Student student){
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
System.out.println("wjhddj");
studentMapper.addStudent(student);
System.out.println("hi的");
sqlSession.commit();
sqlSession.close();
}
public List<Student> selectAll(){
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> StudentSelectAll=studentMapper.selectAll();
sqlSession.close();
return StudentSelectAll;
}
public Student selectById(String StudentId){
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
System.out.println(StudentId);
Student student=studentMapper.selectById(StudentId);
sqlSession.close();
return student;
}
public List<Student> selectBythree(Student student){
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> cls=studentMapper.selectBythree(student);
sqlSession.close();
return cls;
}
public void update(Student student){
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
System.out.println("提交前");
studentMapper.updateStudent(student);
System.out.println("提交后期");
sqlSession.commit();
sqlSession.close();
}
public void delete(int id ){
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
studentMapper.deleteById(id);
sqlSession.commit();
sqlSession.close();
}
}
点击查看代码
package com.QixunQiu.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
//静态代码块会随着类的加载而自动执行,且执行一次
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
点击查看代码
package com.QixunQiu.mapper;
import com.QixunQiu.pojo.Student;
import java.util.List;
public interface StudentMapper {
void addStudent(Student student);
Student selectById(String StudentId);
int updateStudent(Student student);
void deleteById(int id);
List<Student> selectBythree(Student student);
List<Student> selectAll();
}
点击查看代码
<?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.QixunQiu.mapper.StudentMapper">
<insert id="addStudent">
insert into student (StudentId, StudentName, StudentSex, StudentLeave,
StudentCollege,StudentSpecialty,StudentClass,StudentReason,StudentDate)
values (#{StudentId}, #{StudentName}, #{StudentSex}, #{StudentLeave}, #{StudentCollege},
#{StudentSpecialty}, #{StudentClass}, #{StudentReason}, #{StudentDate});
</insert>
<update id="updateStudent">
update student
<set>
<if test="StudentReason != null and StudentReason != '' ">
StudentReason = #{StudentReason},
</if>
<if test="StudentDate != null and StudentDate != '' ">
StudentDate = #{StudentDate},
</if>
</set>
where id = #{id};
</update>
<delete id="deleteById">
delete from student where id = #{id}
</delete>
<select id="selectById" resultType="com.QixunQiu.pojo.Student">
select *
from student
where StudentId = #{StudentId}
</select>
<select id="selectBythree" resultType="com.QixunQiu.pojo.Student">
select *
from student
<where>
<choose>
<when test="StudentId != null and StudentId != ''">
StudentId = #{StudentId}
</when>
<when test="StudentReason != null and StudentReason != '' ">
StudentReason like #{StudentReason}
</when>
<when test="StudentDate != null and StudentDate != '' ">
StudentDate like #{StudentDate}
</when>
</choose>
</where>
</select>
<select id="selectAll" resultType="com.QixunQiu.pojo.Student">
select * from student;
</select>
</mapper>
点击查看代码
<?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.QixunQiu.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<!-- 数据库名称-->
<property name="url" value="jdbc:mysql:///student1122?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true&userServerPreStmts=true"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--扫描mapper-->
<package name="com.QixunQiu.mapper"/>
</mappers>
</configuration>
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.service.StudentService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/addStudentServlet")
public class addStudentServlet extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String StudentId = request.getParameter("StudentId");
String StudentName = request.getParameter("StudentName");
String StudentSex=request.getParameter("StudentSex");
String StudentLeave=request.getParameter("StudentLeave");
String StudentCollege=request.getParameter("StudentCollege");
String StudentSpecialty=request.getParameter("StudentSpecialty");
String StudentClass=request.getParameter("StudentClass");
String StudentReason=request.getParameter("StudentReason");
String StudentDate = request.getParameter("StudentDate");
Student student = new Student();
student.setStudentId(StudentId);
student.setStudentName(StudentName);
student.setStudentSex(StudentSex);
student.setStudentLeave(StudentLeave);
student.setStudentCollege(StudentCollege);
student.setStudentSpecialty(StudentSpecialty);
student.setStudentClass(StudentClass);
student.setStudentReason(StudentReason);
student.setStudentDate(StudentDate);
System.out.println("dnwidji");
service.add(student);
request.getRequestDispatcher("/selectAllStudent").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.service.StudentService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/deleteStudentServlet")
public class deleteStudentServlet extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
service.delete(Integer.parseInt(id));
request.getRequestDispatcher("/selectAllStudent").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
selectAllStudent:
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.service.StudentService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectAllStudent")
public class selectAllStudent extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
List<Student> cls = service.selectAll();
//存入request域中
request.setAttribute("cls",cls);
//转发
request.getRequestDispatcher("/printList.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
selectStudentByIdServlet:
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.service.StudentService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/selectStudentByIdServlet")
public class selectStudentByIdServlet extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("StudentId");
Student student=service.selectById(id);
request.setAttribute("Student", student);
request.getRequestDispatcher("/update.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.service.StudentService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectStudentBythreeServlet")
public class selectStudentBythreeServlet extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String StudentReason = request.getParameter("StudentReason");
String StudentDate=request.getParameter("StudentDate");
String StudentId =request.getParameter("StudentId");
Student student = new Student();
student.setStudentReason(StudentReason);
student.setStudentDate(StudentDate);
student.setStudentId(StudentId);
List<Student> cls=service.selectBythree(student);
System.out.println(cls);
request.setAttribute("cls",cls);
request.getRequestDispatcher("/printList.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.service.StudentService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/selectToStudent")
public class selectToStudent extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String StudentId = request.getParameter("StudentId");
Student student=service.selectById(StudentId);
request.setAttribute("Student", student);
request.getRequestDispatcher("/delete.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
点击查看代码
package com.QixunQiu.web;
import com.QixunQiu.pojo.Student;
import com.QixunQiu.service.StudentService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/updateStudentServlet")
public class updateStudentServlet extends HttpServlet {
private StudentService service = new StudentService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String StudentReason=request.getParameter("StudentReason");
String StudentDate = request.getParameter("StudentDate");
//封装对象
Student student = new Student();
student.setId(Integer.parseInt(id));
student.setStudentReason(StudentReason);
student.setStudentDate(StudentDate);
service.update(student);
request.getRequestDispatcher("/selectAllStudent").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>假条申请</title>
</head>
<body>
<h2>假条申请</h2>
<form id="add-form" action="/Student1122/addStudentServlet" method="post">
学号:<input name="StudentId" type="text" id="StudentId" ><br>
姓名:<input name="StudentName" type="text" id="StudentName" ><br>
性别:<input name="StudentSex" type="text" id="StudentSex" ><br>
年级:<input name="StudentLeave" type="text" id="StudentLeave" ><br>
学院:<input name="StudentCollege" type="text" id="StudentCollege" ><br>
专业:<input name="StudentSpecialty" type="text" id="StudentSpecialty" ><br>
班级:<input name="StudentClass" type="text" id="StudentClass" ><br>
请假理由:<input name="StudentReason" type="text" id="StudentReason" ><br>
请假时间:<input name="StudentDate" type="text" id="StudentDate" ><br>
<div class="buttons">
<input value="添加申请" type="submit" id="add_btn">
</div>
<br class="clear">
</form>
</body>
</html>
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>删除申请</title>
</head>
<body>
<h2>删除申请</h2>
<form id="delete-form" action="/Student1122/selectToStudent" method="post">
学号:<input name="StudentId" type="text" id="StudentId" required><br>
<div class="buttons">
<input value="删除申请" type="submit" id="delete_btn">
</div>
<br class="clear">
</form>
</body>
</html>
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改申请</title>
</head>
<body>
<h2>修改申请</h2>
<form id="modify-form" action="/Student1122/selectStudentByIdServlet" method="post">
课程编号:<input name="StudentId" type="text" id="StudentId" required><br>
<div class="buttons">
<input value="修改申请" type="submit" id="delete_btn">
</div>
<br class="clear">
</form>
</body>
</html>
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>请假条管理系统</title>
<script>
function addStudent() {
window.location.href = "addStudent.html";
}
function fixStudent() {
window.location.href = "fixStudent.html";
}
function deleteStudent() {
window.location.href = "deleteStudent.html";
}
function searchStudent() {
window.location.href = "searchStudent.html";
}
</script>
</head>
<body>
<h1>请假条管理系统</h1>
<button onclick="addStudent()">假条申请</button>
<button onclick="fixStudent()">修改申请</button>
<button onclick="deleteStudent()">删除申请</button>
<button onclick="searchStudent()">查询申请</button>
</body>
</html>
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function Back() {
window.location.href = "index.html";
}
</script>
</head>
<body>
<hr>
<table border="1" cellspacing="0" width="80%">
<tr>
<th>序号</th>
<th>姓名</th>
<th>学院</th>
<th>专业</th>
<th>班级</th>
<th>请假时间</th>
<th>请假理由</th>
</tr>
<c:forEach items="${cls}" var="Student" varStatus="status">
<tr align="center">
<td>${status.count}</td>
<td>${Student.getStudentName()}</td>
<td>${Student.getStudentCollege()}</td>
<td>${Student.getStudentSpecialty()}</td>
<td>${Student.getStudentClass()}</td>
<td>${Student.getStudentDate()}</td>
<td>${Student.getStudentReason()}</td>
</tr>
</c:forEach>
</table>
<button onclick="Back()">返回首页</button>
</body>
</html>
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询申请</title>
</head>
<body>
<h2>查询申请</h2>
<form id="search-form" action="/Student1122/selectStudentBythreeServlet" method="post">
请假时间:<input name="StudentReason" type="text" id="StudentReason" ><br>
请假理由:<input name="StudentDate" type="text" id="StudentDate" ><br>
学号:<input name="StudentId" type="text" id="StudentId" ><br>
<div class="buttons">
<input value="查询申请" type="submit" id="add_btn">
</div>
<br class="clear">
</form>
</body>
</html>
点击查看代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改申请</title>
</head>
<body>
<h2>修改申请</h2>
<form action="${pageContext.request.contextPath}/updateStudentServlet" method="post">
<%--隐藏域,提交id--%>
<input type="hidden" name="id" value="${Student.id}">
请假理由:<input name="StudentReason" type="text" id="StudentReason" value="${Student.getStudentReason()}"><br>
请假时间:<input name="StudentDate" type="text" id="StudentDate" value="${Student.getStudentDate()}"><br>
<div class="buttons">
<input value="修改申请" type="submit" id="add_btn">
</div>
<br class="clear">
</form>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)