完成java請假條管理系統:
實現web頁面的增刪改查操作;
專案結構如圖:
mapper:
點選檢視程式碼
package com.vivy.mapper;
import com.vivy.pojo.Application;
import java.util.List;
public interface ApplicationMapper {
void add(Application application);
Application selectByStudentId(String studentId);
int update(Application application);
void delete(int id);
List<Application> selectByConditionSingle(Application application);
List<Application> selectAll();
}
pojo:
點選檢視程式碼
package com.vivy.pojo;
public class Application {
int id;
String studentId;
String studentName;
String sex;
String grade;
String college;
String specialty;
String studentClass;
String reason;
String studentDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public String getStudentClass() {
return studentClass;
}
public void setStudentClass(String studentClass) {
this.studentClass = studentClass;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getStudentDate() {
return studentDate;
}
public void setStudentDate(String studentDate) {
this.studentDate = studentDate;
}
@Override
public String toString() {
return "Application{" +
"id=" + id +
", studentId='" + studentId + '\'' +
", studentName='" + studentName + '\'' +
", sex='" + sex + '\'' +
", grade='" + grade + '\'' +
", college='" + college + '\'' +
", specialty='" + specialty + '\'' +
", studentClass='" + studentClass + '\'' +
", reason='" + reason + '\'' +
", studentDate='" + studentDate + '\'' +
'}';
}
}
service:
點選檢視程式碼
package com.vivy.service;
import com.vivy.mapper.ApplicationMapper;
import com.vivy.pojo.Application;
import com.vivy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ApplicationService {
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
public void add(Application application){
SqlSession sqlSession = sqlSessionFactory.openSession();
ApplicationMapper applicationMapper = sqlSession.getMapper(ApplicationMapper.class);
applicationMapper.add(application);
sqlSession.commit();
sqlSession.close();
}
public Application selectByStudentId(String s){
SqlSession sqlSession = sqlSessionFactory.openSession();
ApplicationMapper applicationMapper = sqlSession.getMapper(ApplicationMapper.class);
Application application = applicationMapper.selectByStudentId(s);
sqlSession.close();
return application;
}
public void update(Application application){
SqlSession sqlSession = sqlSessionFactory.openSession();
ApplicationMapper applicationMapper = sqlSession.getMapper(ApplicationMapper.class);
applicationMapper.update(application);
sqlSession.commit();
sqlSession.close();
}
public void delete(int id){
SqlSession sqlSession = sqlSessionFactory.openSession();
ApplicationMapper applicationMapper = sqlSession.getMapper(ApplicationMapper.class);
applicationMapper.delete(id);
sqlSession.commit();
sqlSession.close();
}
public List<Application> selectByConditionSingle(Application application){
SqlSession sqlSession = sqlSessionFactory.openSession();
ApplicationMapper applicationMapper = sqlSession.getMapper(ApplicationMapper.class);
List<Application> applications = applicationMapper.selectByConditionSingle(application);
sqlSession.close();
return applications;
}
public List<Application> selectAll(){
SqlSession sqlSession = sqlSessionFactory.openSession();
ApplicationMapper applicationMapper = sqlSession.getMapper(ApplicationMapper.class);
List<Application> applications = applicationMapper.selectAll();
sqlSession.close();
return applications;
}
}
util:
點選檢視程式碼
package com.vivy.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;
}
}
web:
點選檢視程式碼
package com.vivy.web;
import com.vivy.pojo.Application;
import com.vivy.service.ApplicationService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
private ApplicationService service = new ApplicationService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
String studentId = request.getParameter("studentId");
String studentName = request.getParameter("studentName");
String sex = request.getParameter("sex");
String grade = request.getParameter("grade");
String college = request.getParameter("college");
String specialty = request.getParameter("specialty");
String studentClass = request.getParameter("studentClass");
String reason = request.getParameter("reason");
String studentDate = request.getParameter("studentDate");
Application application = new Application();
application.setStudentId(studentId);
application.setStudentName(studentName);
application.setSex(sex);
application.setGrade(grade);
application.setCollege(college);
application.setSpecialty(specialty);
application.setStudentClass(studentClass);
application.setReason(reason);
application.setStudentDate(studentDate);
service.add(application);
request.getRequestDispatcher("/selectAllServlet").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
點選檢視程式碼
package com.vivy.web;
import com.vivy.service.ApplicationService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/deleteOneServlet")
public class deleteOneServlet extends HttpServlet {
private ApplicationService service = new ApplicationService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
String id = request.getParameter("id");
service.delete(Integer.parseInt(id));
request.getRequestDispatcher("/selectAllServlet").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
點選檢視程式碼
package com.vivy.web;
import com.vivy.pojo.Application;
import com.vivy.service.ApplicationService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectAllServlet")
public class selectAllServlet extends HttpServlet {
private ApplicationService service = new ApplicationService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
List<Application> applications = service.selectAll();
request.setAttribute("applications",applications);
request.getRequestDispatcher("/printList.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
點選檢視程式碼
package com.vivy.web;
import com.vivy.pojo.Application;
import com.vivy.service.ApplicationService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectByConditionSingleServlet")
public class selectByConditionSingleServlet extends HttpServlet {
private ApplicationService service = new ApplicationService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
String id = request.getParameter("studentId");
String reason = request.getParameter("reason");
String studentDate = request.getParameter("studentDate");
Application application = new Application();
application.setStudentId(id);
application.setReason(reason);
application.setStudentDate(studentDate);
List<Application> applications = service.selectByConditionSingle(application);
request.setAttribute("applications",applications);
request.getRequestDispatcher("/printList.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
點選檢視程式碼
package com.vivy.web;
import com.vivy.pojo.Application;
import com.vivy.service.ApplicationService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/selectByStudentIdServlet")
public class selectByStudentIdServlet extends HttpServlet {
private ApplicationService service = new ApplicationService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
String s = request.getParameter("studentId");
Application application = service.selectByStudentId(s);
request.setAttribute("application",application);
request.getRequestDispatcher("/update.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
點選檢視程式碼
package com.vivy.web;
import com.vivy.pojo.Application;
import com.vivy.service.ApplicationService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/selectToDelete")
public class selectToDelete extends HttpServlet {
private ApplicationService service = new ApplicationService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
String id = request.getParameter("studentId");
Application application = service.selectByStudentId(id);
request.setAttribute("application",application);
request.getRequestDispatcher("/deleteOne.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
點選檢視程式碼
package com.vivy.web;
import com.vivy.pojo.Application;
import com.vivy.service.ApplicationService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/updateServlet")
public class updateServlet extends HttpServlet {
private ApplicationService service = new ApplicationService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
String id = request.getParameter("id");
String reason = request.getParameter("reason");
String studentDate = request.getParameter("studentDate");
Application application = new Application();
application.setId(Integer.parseInt(id));
application.setReason(reason);
application.setStudentDate(studentDate);
service.update(application);
request.getRequestDispatcher("/selectAllServlet").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
ApplicationMapper.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.vivy.mapper.ApplicationMapper">
<resultMap id="applicationResultMap" type="Application">
<result column="student_id" property="studentId"></result>
<result column="student_name" property="studentName"></result>
<result column="student_class" property="studentClass"></result>
<result column="student_date" property="studentDate"></result>
</resultMap>
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_application (student_id, student_name,sex, grade, college, specialty,student_class,reason,student_date)
values (#{studentId}, #{studentName},#{sex}, #{grade}, #{college}, #{specialty},#{studentClass},#{reason},#{studentDate});
</insert>
<select id="selectByStudentId" resultType="com.vivy.pojo.Application" resultMap="applicationResultMap">
select *
from tb_application
where student_id = #{studentId}
</select>
<select id="selectByConditionSingle" resultType="com.vivy.pojo.Application" resultMap="applicationResultMap">
select *
from tb_application
<where>
<choose>
<when test="studentId != null and studentId != ''">
student_id = #{studentId}
</when>
<when test="reason != null and reason != '' ">
reason like #{reason}
</when>
<when test="studentDate != null and studentDate != '' ">
student_date like #{studentDate}
</when>
</choose>
</where>
</select>
<select id="selectAll" resultType="com.vivy.pojo.Application" resultMap="applicationResultMap">
select * from tb_application;
</select>
<update id="update">
update tb_application
<set>
<if test="reason != null and reason != '' ">
reason = #{reason},
</if>
<if test="studentDate != null and studentDate != '' ">
student_date = #{studentDate},
</if>
</set>
where id = #{id};
</update>
<delete id="delete">
delete from tb_application where id = #{id}
</delete>
</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.vivy.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://localhost:3306/mybatis?useSSL=true&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--載入sql對映檔案-->
<!--<mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
<!--Mapper 代理,掃描mapper-->
<package name="com.vivy.mapper"/>
</mappers>
</configuration>
webapp:
點選檢視程式碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新增請假資訊申請</title>
</head>
<body>
<h2>新增請假資訊申請</h2>
<hr>
<form id="add-form" action="/application-demo/addServlet" method="post">
學號:<input name="studentId" type="text" id="studentId" ><br>
姓名:<input name="studentName" type="text" id="studentName" ><br>
性別:<input name="sex" type="text" id="sex" ><br>
年級:<input name="grade" type="text" id="grade" ><br>
學院:<input name="college" type="text" id="college" ><br>
專業:<input name="specialty" type="text" id="specialty" ><br>
班級:<input name="studentClass" type="text" id="studentClass" ><br>
事由:<input name="reason" type="text" id="reason" ><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>
<hr>
<form id="delete-form" action="/application-demo/selectToDelete" method="post">
學號:<input name="studentId" type="text" id="studentId" required><br>
<div class="buttons">
<input value="提交刪除" type="submit" id="delete1_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>
<hr>
<form action="${pageContext.request.contextPath}/deleteOneServlet" method="post">
<p>學號:${application.studentId}</p>
<p>姓名:${application.studentName}</p>
<p>性別:${application.sex}</p>
<p>年級:${application.grade}</p>
<p>學院:${application.college}</p>
<p>專業:${application.specialty}</p>
<p>班級:${application.studentClass}</p>
<p>事由:${application.reason}</p>
<p>日期:${application.studentDate}</p>
<%--隱藏域,提交id--%>
<input type="hidden" name="id" value="${application.id}">
<div class="buttons">
<input value="確認刪除" type="submit" id="delete2_btn">
</div>
<br class="clear">
</form>
</body>
</html>
點選檢視程式碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>請假條管理系統</title>
<script>
function add() {
window.location.href = "add.html";
}
function modify() {
window.location.href = "modify.html";
}
function deleteOne() {
window.location.href = "deleteOne.html";
}
function search() {
window.location.href = "search.html";
}
</script>
</head>
<body>
<h1>請假條管理系統</h1>
<hr>
<button onclick="add()">新增申請</button>
<button onclick="modify()">修改申請</button>
<button onclick="deleteOne()">刪除申請</button>
<button onclick="search()">查詢申請</button>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>請假條管理系統</title>
<script>
function add() {
window.location.href = "add.html";
}
function modify() {
window.location.href = "modify.html";
}
function deleteOne() {
window.location.href = "deleteOne.html";
}
function search() {
window.location.href = "search.html";
}
</script>
</head>
<body>
<h1>請假條管理系統</h1>
<hr>
<button onclick="add()">新增申請</button>
<button onclick="modify()">修改申請</button>
<button onclick="deleteOne()">刪除申請</button>
<button onclick="search()">查詢申請</button>
</body>
</html>
點選檢視程式碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改申請</title>
</head>
<body>
<h2>修改申請</h2>
<hr>
<form id="modify-form" action="/application-demo/selectByStudentIdServlet" method="post">
學號:<input name="studentId" type="text" id="studentId" required><br>
<div class="buttons">
<input value="提交修改" type="submit" id="modify_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</title>
<script>
function Back() {
window.location.href = "index.html";
}
</script>
</head>
<body>
<table border="1" cellspacing="0" width="80%">
<tr>
<th>序號</th>
<th>學號</th>
<th>姓名</th>
<th>性別</th>
<th>年級</th>
<th>學院</th>
<th>專業</th>
<th>班級</th>
<th>事由</th>
<th>日期</th>
</tr>
<c:forEach items="${applications}" var="application" varStatus="status">
<tr align="center">
<td>${status.count}</td>
<td>${application.studentId}</td>
<td>${application.studentName}</td>
<td>${application.sex}</td>
<td>${application.grade}</td>
<td>${application.college}</td>
<td>${application.specialty}</td>
<td>${application.studentClass}</td>
<td>${application.reason}</td>
<td>${application.studentDate}</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>
<hr>
<form id="search-form" action="/application-demo/selectByConditionSingleServlet" method="post">
學號:<input name="studentId" type="text" id="studentId" ><br>
事由:<input name="reason" type="text" id="reason" ><br>
日期:<input name="studentDate" type="text" id="studentDate" ><br>
<div class="buttons">
<input value="查詢申請" type="submit" id="search_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>
<hr>
<form action="${pageContext.request.contextPath}/updateServlet" method="post">
<p>學號:${application.studentId}</p>
<p>姓名:${application.studentName}</p>
<p>性別:${application.sex}</p>
<p>年級:${application.grade}</p>
<p>學院:${application.college}</p>
<p>專業:${application.specialty}</p>
<p>班級:${application.studentClass}</p>
<%--隱藏域,提交id--%>
<input type="hidden" name="id" value="${application.id}">
事由:<input name="reason" type="text" id="reason" value="${application.reason}"><br>
日期:<input name="studentDate" type="text" id="studentDate" value="${application.studentDate}"><br>
<div class="buttons">
<input value="確認修改" type="submit" id="update_btn">
</div>
<br class="clear">
</form>
</body>
</html>
pom.xml:
點選檢視程式碼
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>application-demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- tomcat 外掛 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>