2024/11/20日工作总结
完成java课堂测试:课程管理系统
用前端页面实现数据库增删改查;
mapper:
点击查看代码
package com.vivy.mapper;
import com.vivy.pojo.Classes;
import java.util.List;
public interface ClassesMapper {
void add(Classes classes);
List<Classes> selectAll();
List<Classes> selectByConditionSingle(Classes classes);
Classes selectByClassId(String classId);
int update(Classes classes);
void delete(int id);
}
pojo:
点击查看代码
package com.vivy.pojo;
public class Classes {
int id;
String classId;
String className;
int number;
String teacherName;
String position;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "Class{" +
"id=" + id +
", classId='" + classId + '\'' +
", className='" + className + '\'' +
", number=" + number +
", teacherName='" + teacherName + '\'' +
", position='" + position + '\'' +
'}';
}
}
service:
点击查看代码
package com.vivy.service;
import com.vivy.mapper.ClassesMapper;
import com.vivy.pojo.Classes;
import com.vivy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ClassesService {
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
public List<Classes> selectAll(){
SqlSession sqlSession = sqlSessionFactory.openSession();
ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
List<Classes> cls = classesMapper.selectAll();
sqlSession.close();
return cls;
}
public List<Classes> selectByConditionSingle(Classes classes){
SqlSession sqlSession = sqlSessionFactory.openSession();
ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
List<Classes> cls = classesMapper.selectByConditionSingle(classes);
sqlSession.close();
return cls;
}
public Classes selectByClassId(String s){
SqlSession sqlSession = sqlSessionFactory.openSession();
ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
Classes classes = classesMapper.selectByClassId(s);
sqlSession.close();
return classes;
}
public void add(Classes classes){
SqlSession sqlSession = sqlSessionFactory.openSession();
ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
classesMapper.add(classes);
sqlSession.commit();
sqlSession.close();
}
public void update(Classes classes){
SqlSession sqlSession = sqlSessionFactory.openSession();
ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
classesMapper.update(classes);
sqlSession.commit();
sqlSession.close();
}
public void delete(int id){
SqlSession sqlSession = sqlSessionFactory.openSession();
ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
classesMapper.delete(id);
sqlSession.commit();
sqlSession.close();
}
}
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.mapper.ClassesMapper;
import com.vivy.pojo.Classes;
import com.vivy.service.ClassesService;
import com.vivy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@WebServlet("/addCourseServlet")
public class addCourseServlet extends HttpServlet {
private ClassesService service = new ClassesService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String classId = request.getParameter("classId");
String className = request.getParameter("className");
String number = request.getParameter("number");
String teacherName = request.getParameter("teacherName");
String position = request.getParameter("position");
//# = new String(classId.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
Classes classes = new Classes();
classes.setClassId(classId);
classes.setClassName(className);
classes.setNumber(Integer.parseInt(number));
classes.setTeacherName(teacherName);
classes.setPosition(position);
service.add(classes);
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.ClassesService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/deleteCourseServlet")
public class deleteCourseServlet extends HttpServlet {
private ClassesService service = new ClassesService();
@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("/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.Classes;
import com.vivy.service.ClassesService;
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 ClassesService service = new ClassesService();
@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<Classes> 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);
}
}
点击查看代码
package com.vivy.web;
import com.vivy.pojo.Classes;
import com.vivy.service.ClassesService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/selectByClassIdServlet")
public class selectByClassIdServlet extends HttpServlet {
private ClassesService service = new ClassesService();
@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("classId");
Classes classes = service.selectByClassId(id);
request.setAttribute("classes",classes);
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.Classes;
import com.vivy.service.ClassesService;
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 ClassesService service = new ClassesService();
@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 className = request.getParameter("className");
String teacherName = request.getParameter("teacherName");
String position = request.getParameter("position");
Classes classes = new Classes();
classes.setClassName(className);
classes.setTeacherName(teacherName);
classes.setPosition(position);
List<Classes> cls = service.selectByConditionSingle(classes);
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.vivy.web;
import com.vivy.pojo.Classes;
import com.vivy.service.ClassesService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/selectToDelete")
public class selectToDelete extends HttpServlet {
private ClassesService service = new ClassesService();
@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("classId");
Classes classes = service.selectByClassId(id);
request.setAttribute("classes",classes);
request.getRequestDispatcher("/delete.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.Classes;
import com.vivy.service.ClassesService;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/updateCourseServlet")
public class updateCourseServlet extends HttpServlet {
private ClassesService service = new ClassesService();
@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 className = request.getParameter("className");
String number = request.getParameter("number");
String teacherName = request.getParameter("teacherName");
String position = request.getParameter("position");
Classes classes = new Classes();
classes.setId(Integer.parseInt(id));
classes.setClassName(className);
classes.setNumber(Integer.parseInt(number));
classes.setTeacherName(teacherName);
classes.setPosition(position);
service.update(classes);
request.getRequestDispatcher("/selectAllServlet").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
mapper.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.ClassesMapper">
<resultMap id="classesResultMap" type="classes">
<result column="class_id" property="classId"></result>
<result column="class_name" property="className"></result>
<result column="teacher_name" property="teacherName"></result>
</resultMap>
<select id="selectAll" resultType="classes" resultMap="classesResultMap">
select * from tb_class;
</select>
<select id="selectByConditionSingle" resultType="com.vivy.pojo.Classes" resultMap="classesResultMap">
select *
from tb_class
<where>
<choose>
<when test="className != null and className != ''">
class_name = #{className}
</when>
<when test="teacherName != null and teacherName != '' ">
teacher_name like #{teacherName}
</when>
<when test="position != null and position != '' ">
position like #{position}
</when>
</choose>
</where>
</select>
<select id="selectByClassId" resultType="com.vivy.pojo.Classes" resultMap="classesResultMap">
select *
from tb_class
where class_id = #{classId}
</select>
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_class (class_id, class_name, number, teacher_name, position)
values (#{classId}, #{className}, #{number}, #{teacherName}, #{position});
</insert>
<update id="update">
update tb_class
<set>
<if test="className != null and className != '' ">
class_name = #{className},
</if>
<if test="number != null ">
number = #{number},
</if>
<if test="teacherName != null and teacherName != '' ">
teacher_name = #{teacherName},
</if>
<if test="position != null and position != '' ">
position = #{position},
</if>
</set>
where id = #{id};
</update>
<delete id="delete">
delete from tb_class where id = #{id}
</delete>
</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.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>
<form id="add-form" action="/course-demo/addCourseServlet" method="post">
课程编号:<input name="classId" type="text" id="classId" ><br>
课程名称:<input name="className" type="text" id="className" ><br>
选课人数:<input name="number" type="number" id="number" ><br>
任课教师:<input name="teacherName" type="text" id="teacherName" ><br>
上课地点:<input name="position" type="text" id="position" ><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}/deleteCourseServlet" method="post">
<hr>
<table border="1" cellspacing="0" width="80%">
<tr>
<th>课程编号</th>
<th>课程名称</th>
<th>选课人数</th>
<th>教师名称</th>
<th>上课地点</th>
</tr>
<tr align="center">
<td>${classes.classId}</td>
<td>${classes.className}</td>
<td>${classes.number}</td>
<td>${classes.teacherName}</td>
<td>${classes.position}</td>
</tr>
</table>
<%--隐藏域,提交id--%>
<input type="hidden" name="id" value="${classes.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>
</head>
<body>
<h2>删除课程</h2>
<form id="delete-form" action="/course-demo/selectToDelete" method="post">
课程编号:<input name="classId" type="text" id="classId" required><br>
<div class="buttons">
<input value="提交删除" type="submit" id="delete1_btn">
</div>
<br class="clear">
</form>
</body>
</html>
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>课程管理系统</title>
<script>
function addCourse() {
window.location.href = "addCourse.html";
}
function modifyCourse() {
window.location.href = "modifyCourse.html";
}
function deleteCourse() {
window.location.href = "deleteCourse.html";
}
function searchCourses() {
window.location.href = "searchCourses.html";
}
function selectAllCourses() {
window.location.href = "/course-demo/selectAllServlet";
}
</script>
</head>
<body>
<h1>课程管理系统</h1>
<button onclick="addCourse()">新增课程</button>
<button onclick="modifyCourse()">修改课程</button>
<button onclick="deleteCourse()">删除课程</button>
<button onclick="searchCourses()">查询课程</button>
<button onclick="selectAllCourses()">浏览课程</button>
</body>
</html>
点击查看代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改课程</title>
</head>
<body>
<h2>修改课程</h2>
<form id="modify-form" action="/course-demo/selectByClassIdServlet" method="post">
课程编号:<input name="classId" type="text" id="classId" 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>
<hr>
<table border="1" cellspacing="0" width="80%">
<tr>
<th>序号</th>
<th>课程编号</th>
<th>课程名称</th>
<th>选课人数</th>
<th>教师名称</th>
<th>上课地点</th>
</tr>
<c:forEach items="${cls}" var="classes" varStatus="status">
<tr align="center">
<td>${status.count}</td>
<td>${classes.classId}</td>
<td>${classes.className}</td>
<td>${classes.number}</td>
<td>${classes.teacherName}</td>
<td>${classes.position}</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="/course-demo/selectByConditionSingleServlet" method="post">
课程名称:<input name="className" type="text" id="className" ><br>
任课教师:<input name="teacherName" type="text" id="teacherName" ><br>
上课地点:<input name="position" type="text" id="position" ><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>
<form action="${pageContext.request.contextPath}/updateCourseServlet" method="post">
<%--隐藏域,提交id--%>
<input type="hidden" name="id" value="${classes.id}">
课程名称:<input name="className" type="text" id="className" value="${classes.className}"><br>
选课人数:<input name="number" type="number" id="number" value="${classes.number}"><br>
任课教师:<input name="teacherName" type="text" id="teacherName" value="${classes.teacherName}"><br>
上课地点:<input name="position" type="text" id="position" value="${classes.position}"><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>course-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>