作业管理系统(IDEA+Springboot+MyBatis+JSP+MySQL+Docker)
作业管理系统(IDEA+Springboot+MyBatis+JSP+MySQL+Docker)
作业管理系统(IDEA+Springboot+MyBatis+JSP+MySQL+Docker)
一、构建Springboot+MyBatis项目
1.IDEA 中创建项目,选择 Spring Initializr,点击 Next。
2.如下图,Web 中选择 Spring Web;SQL 中选择 MyBatis Framework 和 MySQL Driver。
3.配置数据库,如图所示,编写 application.properties 文件。
4.可通过直接运行 DemoApplication 类启动项目,不需要配置 Tomcat。DemoApplication 类内容如下:
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5.pom.xml 在已自动配置的依赖基础上添加访问 JSP 页面需要导入的依赖。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
6.创建 webapp 文件夹,并关联项目。
二、通过注释集成MyBatis
1. 项目目录结构如下:
2. entity
包含 Teahcer,Student,Teach,Homework,Submit 类,分别写有相应的变量、构造方法和 get、set 函数。
对应的构建了五张表,建表语句如下:
create table student (student_name char(12));
create table teacher (teacher_name char(12));
create table teach (teacher_name char(12), student_name char(12));
create table homework (homework_title char(60), teacher_name char(12));
create table submit (homework_title char(60), teacher_name char(12), student_name char(12), content char(255));
3. mapper
(1)StudentMapper 通过注释的方式增、改、查数据库。分别有以下五个接口。
@Insert("INSERT INTO STUDENT VALUES (#{student.student_name})")
void InsertStudent(@Param("student") Student student); // 学生注册后被添加到Student表中
@Select("SELECT * FROM HOMEWORK WHERE HOMEWORK.TEACHER_NAME IN (SELECT TEACH.TEACHER_NAME FROM TEACH WHERE STUDENT_NAME=#{student.student_name})")
List<Homework> QueryHomework(@Param("student") Student student); // 通过Teach表、Homework表查询当前学生的作业列表
@Select("SELECT * FROM SUBMIT")
List<Submit> QuerySubmits(); // 通过Submit表查询已提交的作业
@Insert("INSERT INTO SUBMIT VALUES (#{homework_title}, #{teacher_name}, #{student.student_name}, #{content})")
void InsertSubmit(@Param("homework_title") String homework_title, @Param("teacher_name") String teacher_name, @Param("student") Student student, @Param("content") String content); // 若该学生未提交过该作业,则添加到Submit表中
@Update("UPDATE SUBMIT SET CONTENT=#{content} WHERE HOMEWORK_TITLE=#{homework_title} AND TEACHER_NAME=#{teacher_name} AND STUDENT_NAME=#{student.student_name}")
void UpdateSubmit(@Param("homework_title") String homework_title, @Param("teacher_name") String teacher_name, @Param("student") Student student, @Param("content") String content); // 若该学生已提交过该作业,则更新Submit表
(2)TeacherMapper 通过注释的方式增、查数据库。分别有以下七个接口。
@Insert("INSERT INTO TEACHER VALUES (#{teacher.teacher_name})")
void AddTeacher(@Param("teacher") Teacher teacher); // 老师注册后被添加到Teacher表中
@Select("SELECT * FROM HOMEWORK WHERE TEACHER_NAME=#{teacher.teacher_name}")
List<Homework> QueryHomework(@Param("teacher") Teacher teacher); // 通过Homework表查询当前老师发布的作业列表
@Insert("INSERT INTO HOMEWORK VALUES (#{homework_title}, #{teacher.teacher_name})")
void AddHomework(@Param("teacher") Teacher teacher, @Param("homework_title") String homework_title); // 老师新增作业,添加到Homework表中
@Insert("INSERT INTO TEACH VALUES (#{teacher.teacher_name}, #{student_name})")
void AddStudent(@Param("teacher") Teacher teacher, @Param("student_name") String student_name); // 通过Teach表查询当前老师的学生列表
@Select("SELECT * FROM TEACH WHERE TEACHER_NAME=#{teacher.teacher_name}")
List<Student> QueryStudent(@Param("teacher") Teacher teacher); // 老师新增学生,添加到Teach表中
@Select("SELECT * FROM SUBMIT WHERE HOMEWORK_TITLE=#{homework_title} and TEACHER_NAME=#{teacher.teacher_name}")
List<Submit> QuerySubmit(@Param("homework_title") String homework_title, @Param("teacher") Teacher teacher); // 通过Submit表查询当前老师所查询作业的已提交学生列表
@Select("SELECT CONTENT FROM SUBMIT WHERE HOMEWORK_TITLE=#{homework_title} and TEACHER_NAME=#{teacher.teacher_name} and STUDENT_NAME=#{student_name}")
String CheckContent(@Param("homework_title") String homework_title, @Param("teacher") Teacher teacher, @Param("student_name") String student_name); // 通过Submit表查询当前老师所查询作业及所查询学生的作业内容
4. service
(1)StudentMapper → StudentService,Servic 封装了 Mapper 中的方法,被 Controller 调用。
@Service("studentService")
public class StudentService {
@Autowired
StudentMapper studentMapper;
@Transactional
public void InsertStudent(Student student) {
studentMapper.InsertStudent(student);
}
@Transactional
public List<Homework> QueryHomework(Student student) {
return studentMapper.QueryHomework(student);
}
@Transactional
public List<Submit> QuerySubmits() {
return studentMapper.QuerySubmits();
}
@Transactional
public void InsertSubmit(String homework_title, String teacher_name, Student student, String content) {
studentMapper.InsertSubmit(homework_title, teacher_name, student, content);
}
@Transactional
public void UpdateSubmit(String homework_title, String teacher_name, Student student, String content) {
studentMapper.UpdateSubmit(homework_title, teacher_name, student, content);
}
}
(2)TeacherMapper → TeacherService,与 StudentService 原理相同。
5. contorller(以“登录”为例)
Controller 与 Service
在 IndexController 中分别声明了 StudentService 和 TeacherService 类型的变量,从而调用其中QueryHomework方法。
Controller 与 JSP
① @RequestMapping(value = “index”) 的 “index” 对应 index.jsp 发送的请求。
② return “student_submit” / return “teacher_homework”; 表示跳转到相应 jsp页面(student_submit.jsp / teacher_homework.jsp)。
@Controller
@ComponentScan({"com.example.demo.service"})
@MapperScan("com.example.demo.mapper")
public class IndexController {
@Resource
private StudentService studentService;
@Resource
private TeacherService teacherService;
@RequestMapping(value = "index")
public String Login(@RequestParam("login_name") String login_name,
@RequestParam("login_identity") String login_identity,
Model model,
HttpServletRequest request) {
if(login_identity.equals("学生")) {
request.getSession().setAttribute("student_name", login_name);
Student student = new Student(login_name);
student.setStudent_name(login_name);
List<Homework> homework_list = studentService.QueryHomework(student);
request.setAttribute("homework_list", homework_list);
return "student_submit";
}else {
request.getSession().setAttribute("teacher_name", login_name);
Teacher teacher = new Teacher(login_name);
teacher.setTeacher_name(login_name);
List<Homework> homework_list = teacherService.QueryHomework(teacher);
model.addAttribute("homework_list", homework_list);
return "teacher_homework";
}
}
@RequestMapping(value = "login_to_register")
public String LoginToRegister() {
return "register";
}
}
6. jsp(以“登录”为例)
index.jsp 中 form 的 action 属性为 “index”,即点击登录按钮,向 IndexController 发送请求。
<div class="row lyear-wrapper">
<div class="lyear-login">
<div class="login-center">
<form action="index" method="get">
<div class="form-group has-feedback feedback-left">
<input type="text" placeholder="请输入您的用户名" class="form-control" name="login_name" id="login_name" />
</div>
<div class="form-group has-feedback feedback-left">
<input type="text" placeholder="请输入您的身份(老师/学生)" class="form-control" id="login_identity" name="login_identity" />
</div>
<div class="form-group">
<a class="btn btn-w-xl btn-default" type="button" href="/login_to_register">注册</a>
<input class="btn btn-w-xl btn-primary" type="submit" value="登录" />
</div>
</form>
<hr>
<footer class="col-sm-12 text-center">
<p class="m-b-0">Copyright © 2020 <a href="">作业管理系统</a>. All right reserved</p>
</footer>
</div>
</div>
</div>
三、Dokcer
1.Docker 安装失败后,下载安装了 Docker Toolbox,过程参考教程:win7或win10环境DockerToolbox安装教程,不再详细介绍。点击 “Docker Quickstart Terminal” 如下图即为正确安装。
2.在项目根目录下添加 Dockerfile 文件,内容如下:
FROM java:8
VOLUME /tmp
COPY target/demo-0.0.1-SNAPSHOT.jar demo.jar
RUN bash -c "touch /demo.jar"
EXPOSE 8080
ENTRYPOINT ["java","-jar","demo.jar"]
3.修改 pom.xml 文件,新增如下内容:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<!--新增-->
<artifactId>spring-boot-starter-web</artifactId>
<!--新增-->
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--新增-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory></dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!--新增-->
</plugins>
</build>
4.IDEA 中点击 Maven 中的 package,将项目打包成 jar 包,如第二张图片显示即成功。
5.点击 File → Settings…,选择 Docker,进行如下配置:
6.Run → Edit Configurations…,进行如下配置:
7.运行步骤6中配置的homework_system_docker,结果如下:
8.在 Docker Toolbox 中查看、上传:
四、功能介绍及页面展示
1. 功能介绍
2. 页面展示
(1)老师甲注册,学生甲注册。
(2)老师甲登录。
(3)老师甲查看作业列表,新增作业后显示。
(4)老师甲查看学生列表,新增学生后显示。
(5)学生甲登录后查看作业列表,点击上传作业。
(6)学生甲输入作业内容,点击确定提交作业。
(7)老师甲登录后点击查看已提交名单,可见学生甲已提交作业,点击查看作业,可见作业内容。
(8)学生甲重新提交作业。
(9)老师甲查看新作业内容。
五、总结
本次大作业是对之前小作业的整合,并新增使用 Docker 提交 Docker image 要求。在写本次作业的过程也发现了之前提交的小作业的错误,现已学会如何正确使用Springboot,MyBatis等。