JavaEE——作业管理系统
程序员文章站
2024-01-27 13:33:28
...
基于Springboot的简易学生作业管理系统
目录
3.2.4教师管理学生名单(可添加学生、更改学生信息、删除学生信息)
一、大概介绍下项目结构:
- 持久层框架使用的是:Mybatis(配置 .xml文件版)
- 使用了RESTful:通过GET,POST请求实现各种功能
- 模板引擎使用的是:Thymeleaf
- 数据库使用的是:Mysql
- 数据访问使用的是:Spring Data JPA
二、项目结构图
三、页面实现
3.1身份为学生的页面实现效果(权限=1)
3.1.1登录
3.1.2注册
3.1.3登录成功
3.1.4查看学生名单
3.1.5学生管理作业(可提交、更新、删除作业)
3.1.6学生更新作业
3.1.7学生提交作业
3.2身份为教师的页面实现效果(权限=0)
3.2.1登录
3.2.2注册
3.2.3登录成功
3.2.4教师管理学生名单(可添加学生、更改学生信息、删除学生信息)
3.2.5教师更改学生信息
3.2.6教师添加学生
3.2.7教师管理作业
3.2.8教师更改作业信息
3.2.9教师布置新作业
3.2.10教师检查已提交的作业
四、代码解析
使用学生操作进行解析(教师和用户操作与学生操作大同小异)
实体类——model:Student
4.1表结构
4.2实体类
在com.example.demo.model包中新建一个类,命名为Student.java。
package com.example.demo.model;
public class Student {
int stuid;
String stuname;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
@Override
public String toString() {
return "Student{" +
"stuid=" + stuid +
", stuname='" + stuname + '\'' +
'}';
}
}
4.3mapper类
在com.example.demo.mapper包中新建一个类,编写mapper接口(Studentmapper.java)。
package com.example.demo.mapper;
import com.example.demo.model.Student;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface Studentmapper {
List<Student> selectStudent();
public void insertStudent(@Param("stuid")int stuid, @Param("stuname")String stuname);
Student findstudentById(int stuid);
int updateStudent(Student student);
int deleteStudent(int stuid);
}
4.4mapper.xml
写了接口之后在resource下新建一个mapping文件夹,用于存放映射文件新建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.example.demo.mapper.Studentmapper">
<!--新增-->
<insert id="insertStudent">
insert into student (stuid,stuname) values (#{stuid},#{stuname});
</insert>
<select id="selectStudent" resultType="Student">
select stuid,stuname from student;
</select>
<select id="findstudentById" resultType="Student">
select * from student where stuid=#{stuid}
</select>
<update id="updateStudent" parameterType="Student">
update student set stuname=#{stuname} where stuid=#{stuid};
</update>
<delete id="deleteStudent" parameterType="int">
delete from student where stuid=#{stuid};
</delete>
</mapper>
在application.properties中添加配置扫描映射文件和实体类
mybatis.type-aliases-package=com.example.demo.model
mybatis.mapper-locations=classpath:/mapper/*.xml
4.5service类
在com.example.demo.service包中新建一个StudentService类,编写业务层了(StudentService.java),直接在新建StudentService类来写业务。
package com.example.demo.service;
import com.example.demo.mapper.Studentmapper;
import com.example.demo.model.Student;
import com.example.demo.model.StudentHomework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
Studentmapper studentmapper;
public boolean insertStudent(int stuid,String stuname){
studentmapper.insertStudent(stuid,stuname);
return true;
}
public List<Student> selectStudent(){
return studentmapper.selectStudent();
}
public Student findstudentById(int stuid){
return studentmapper.findstudentById(stuid);
}
public int updateStudent(Student student){
return studentmapper.updateStudent(student);
}
public int deleteStudent(int stuid){
return studentmapper.deleteStudent(stuid);
}
}
4.6controller类
编写完业务之后就是编写控制器:在com.example.demo.controller包中新建一个StudentController.java
使用Restful:用@GetMapping、@PostMapping取代了@RequestMapping相当于增加了method=“GET”等内容,根据请求方法映射到不同的方法。
package com.example.demo.controller;
import com.example.demo.model.Homework;
import com.example.demo.model.Student;
import com.example.demo.model.StudentHomework;
import com.example.demo.service.HomeworkService;
import com.example.demo.service.StudentHomeworkService;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@Controller
public class StudentController {
@Autowired
StudentHomeworkService studentHomeworkService;
@RequestMapping("/addstudenthomework")
public String addstudenthomework(){
return "addstudenthomework";
}
@RequestMapping("/doaddstudenthomework")
public String doaddstudenthomework(StudentHomework studentHomework, Map<String,Object> map){
studentHomeworkService.insertStudentHomework(studentHomework.getStuid(),studentHomework.getHomeworkid(),studentHomework.getHomeworktitle(),studentHomework.getHomeworkContent());
map.put("msg","添加成功");
return "student";
}
@GetMapping("/updatepage/{id}")
public String updatepage(Model model, @PathVariable int id){
StudentHomework studentHomework=studentHomeworkService.findById(id);
model.addAttribute("studenthomework",studentHomework);
return "updatepage";
}
@PostMapping("/update")
public String updatestudenthomework(StudentHomework studentHomework){
studentHomeworkService.updateStudentHomework(studentHomework);
return "student";
}
@GetMapping("/delete/{id}")
public String deletestudenthomework(@PathVariable int id){
studentHomeworkService.deleteStudentHomework(id);
return "student";
}
@Autowired
HomeworkService homeworkService;
@RequestMapping("/showhomework1")
public String selectHomework(Model model){
List<Homework> homework=homeworkService.selectHomework();
model.addAttribute("homework",homework);
List<StudentHomework> studentHomeworks=studentHomeworkService.selectStudentHomework();
model.addAttribute("studenthomework",studentHomeworks);
return "showhomework1";
}
@Autowired
StudentService studentService;
@RequestMapping("/showstu1")
public String selectStudent(Model model){
List<Student> students=studentService.selectStudent();
model.addAttribute("students",students);
return "showstu1";
}
}
4.7登录成功代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>学生界面</title>
<link rel="stylesheet" type="text/css" href="css/style.css" th:href="@{/css/style.css}"/>
</head>
<body class="back1" align="center">
<div style="margin-top: 50px"></div>
<h1 >欢迎同学进入</h1>
<div align="center">
<fieldset class="field">
<legend><h3>学生可使用的功能:  </h3></legend>
<ul type="none">
<li class="li">1、提交作业</li>
<li class="li">2、查看学生名单<br></li>
<li class="li">3、查看已布置的作业<br></li>
<li class="li">4、更新作业/删除作业<br></li>
</ul>
</fieldset>
</div>
<div>
<a href="/showstu1" target="_blank">
<button type="button" class="button">学生名单</button>
</a>
<br />
<a href="/showhomework1" target="_blank">
<button type="button" class="button">提交作业</button>
</a>
</div>
</body>
</html>
4.8学生查看学生名单
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>学生查看学生名单</title>
<link rel="stylesheet" type="text/css" href="css/style.css" th:href="@{/css/style.css}"/>
</head>
<body class="back1">
<table border="0" class="table1" align="center" cellspacing="15px">
<h3 align="center">学生名单</h3>
<tr class="tr">
<th class="th">学生学号</th>
<th class="th">学生姓名</th>
</tr>
<tr th:each="user : ${students}" class="tr">
<td th:text="${user.stuid}" class="td"></td>
<td th:text="${user.stuname}" class="td"></td>
</tr>
</table>
</body>
</html>
4.9学生管理作业
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>学生管理作业</title>
<link rel="stylesheet" type="text/css" href="css/style.css" th:href="@{/css/style.css}"/>
</head>
<body class="back1">
<div class="left">
<h3 align="center">已布置的作业</h3>
<table border="0" class="table1" cellspacing="15px">
<tr class="tr">
<th class="th">作业编号</th>
<th class="th">作业题目</th>
</tr>
<tr th:each="user : ${homework}" class="tr">
<td th:text="${user.homeworkid}" class="td"></td>
<td th:text="${user.homeworktitle}" class="td"></td>
</tr>
</table>
</div>
<div class="right">
<h3 align="center">提交作业</h3>
<table border="0" class="table1" cellspacing="15px">
<tr class="tr">
<th class="th">学生学号</th>
<th class="th" style="width: 100px">作业编号</th>
<th class="th">作业题目</th>
<th class="th">作业内容</th>
<th width="80px" class="th"></th>
</tr>
<tr th:each="user : ${studenthomework}" class="tr">
<td th:text="${user.stuid}" class="td"></td>
<td th:text="${user.homeworkid}" class="td"></td>
<td th:text="${user.homeworktitle}" class="td"></td>
<td th:text="${user.homeworkContent}" class="td"></td>
<td class="td">
<a th:href="@{'/updatepage/'+${user.id}}" >更改</a>
<a th:href="@{'/delete/'+${user.id}}">删除</a>
</td>
</tr>
</table>
<a href="/addstudenthomework">
<button type="button" class="button">提交作业</button>
</a>
</div>
</body>
</html>
4.10学生更改作业
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>学生更新作业</title>
<link rel="stylesheet" type="text/css" href="css/style.css" th:href="@{/css/style.css}"/>
</head>
<body class="back1"align="center">
<div style="margin-top: 100px">
<h1 align="center">更新作业进行中...</h1>
</div>
<div align="center" style="margin-top: 20px">
<form action="/update" method="post">
<div style="margin-top: 20px">
<label class="label">      I    D:</label>
<input name="id" type="text" th:value="${studenthomework.id}" readonly="readonly" style="height: 25px"><br>
</div>
<div style="margin-top: 20px">
<label class="label">学生学号:</label>
<input type="text" th:value="${studenthomework.stuid}" name="stuid" style="height: 25px"><br>
</div>
<div style="margin-top: 20px">
<label class="label">作业编号:</label>
<input type="text" th:value="${studenthomework.homeworkid}" name="homeworkid" style="height: 25px"><br>
</div>
<div style="margin-top: 20px">
<label class="label">作业题目:</label>
<input type="text" th:value="${studenthomework.homeworktitle}" name="homeworktitle" style="height: 25px"><br>
</div>
<div style="margin-top: 20px">
<label class="label">作业内容:</label>
<input type="text" th:value="${studenthomework.homeworkContent}" name="homeworkContent" style="height: 25px"><br>
</div>
<button type="submit" class="button">提交</button>
</form>
</div>
</body>
</html>
4.11学生提交作业
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>学生提交作业</title>
<link rel="stylesheet" type="text/css" href="css/style.css" th:href="@{/css/style.css}"/>
</head>
<body align="center" class="back1">
<form name="form" accept-charset="utf-8" action="/doaddstudenthomework">
<div style="margin-top: 100px">
<h1 align="center">提交作业进行中...</h1>
</div>
<div style="margin: 20px">
<label class="label">学生学号:</label>
<input type="text" id="stuid" name="stuid" maxlength="16" />
</div>
<div style="margin: 20px">
<label class="label">作业编号:</label>
<input type="text" id="homeworkid" name="homeworkid" maxlength="16" />
</div>
<div style="margin: 20px">
<label class="label">作业题目:</label>
<input type="text" id="homeworktitle" name="homeworktitle" maxlength="20"/>
</div>
<div style="margin: 20px">
<label class="label">作业内容:</label>
<input type="text" id="homeworkContent" name="homeworkContent" maxlength="100" />
</div>
<div style="margin-top: 20px">
<input type="submit" value="确定" class="button"/>
</div>
</form>
</body>
</html>
五、Git地址
上一篇: 15个小时----从修改程序到自己些程序_php基础
下一篇: js单词形式的运算符_基础知识