案例:请假条管理系统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 + '\'' +
                '}';
    }
}

StudentService:
点击查看代码
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();
    }


}

SqlSessionFactoryUtils:
点击查看代码
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;
    }
}

StudentMapper:
点击查看代码
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();
}

StudentMapper.xml:
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.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>
mybatis-config.xml:
点击查看代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
<!--        记得修改名称-->
        <package name="com.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&amp;serverTimezone=Hongkong&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;userServerPreStmts=true"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--扫描mapper-->
        <package name="com.QixunQiu.mapper"/>
    </mappers>
</configuration>
addStudentServlet:
点击查看代码
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);
    }
}

deleteStudentServlet:
点击查看代码
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);
    }
}

selectStudentBythreeServlet:
点击查看代码
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);
    }
}

selectToStudent:
点击查看代码
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);
    }
}

updateStudentServlet:
点击查看代码
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);
    }
}

web前端: addStudent.html
点击查看代码
<!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>

delete.jsp:
点击查看代码
<!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>
fixStudent.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>
index.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>
printList.jsp:
点击查看代码
<%@ 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>

searchStudent.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>
update.jsp:
点击查看代码
<%@ 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>
posted @   QixunQiu  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示