这个demo整合了 springboot+mybatis+mysql数据库
项目整体架构
前端显示
这是添加数据的窗口
这是查询所有学生成绩的窗口
点击删除之后可以删除信息
前端代码
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生成绩管理系统</title>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="modules/css/bootstrap.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="modules/jquery-3.2.1.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="modules/js/bootstrap.bundle.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="modules/js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<h1>学生成绩管理系统</h1>
<h4><a href="/selectAll">查询所有学生成绩</a></h4>
<div class="row">
<div class="col-md-8" >
<form class="form-horizontal" action="/addStu" method="post">
<div class="form-group">
<label class="col-sm-2 control-label">学号</label>
<div class="col-sm-10">
<input name="num" class="form-control" placeholder="请输入学号">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">学生姓名</label>
<div class="col-sm-10">
<input name="name" class="form-control" placeholder="请输入学生姓名">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">语文成绩</label>
<div class="col-sm-10">
<input name="chinese" class="form-control" placeholder="请输入语文成绩">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">数学成绩</label>
<div class="col-sm-10">
<input name="math" class="form-control" placeholder="请输入数学成绩">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">英语成绩</label>
<div class="col-sm-10">
<input name="english" class="form-control" placeholder="请输入英语成绩">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">添加</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
show.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>查看所有学生成绩</title>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="modules/css/bootstrap.css">
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="modules/jquery-3.2.1.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="modules/js/bootstrap.bundle.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="modules/js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-12">
<h1>查询所有学生成绩</h1>
<h4><a href="back">继续添加学生信息</a></h4>
<table class="table">
<tr>
<td>编号</td>
<td>学号</td>
<td>学生姓名</td>
<td>语文</td>
<td>数学</td>
<td>英语</td>
<td>总分</td>
<td>操作</td>
</tr>
<tr th:each="st: ${sts}">
<td th:text="${st.id}"></td>
<td th:text="${st.num}"></td>
<td th:text="${st.name}"></td>
<td th:text="${st.grade_chinese}"></td>
<td th:text="${st.grade_math}"></td>
<td th:text="${st.grade_english}"></td>
<td th:text="${st.grade_sum}"></td>
<!-- 这里是用theamleaf语法进行href的拼接 -->
<td><a th:href="@{deleteStu/{id}(id=${st.id})}">删除</a></td>
</tr>
</table>
</div>
<!--<div th:text="${msg}"></div>-->
</div>
</body>
</html>
pom配置信息
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learn</groupId>
<artifactId>version4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>version4</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
mybatis配置信息
application.properities
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springjdbc?serverTimezone=UTC&&characterEncoding=UTF-8
# 这里不能用 spring.datasource.data-username 这个错找了好久
spring.datasource.username=root
spring.datasource.password=root
# 整合mybatis
mybatis.type-aliases-package=com.learn.pojo
数据库设计
create table students(
id int primary key auto_increment,
num int,
name varchar(20),
grade_chinese int,
grade_math int,
grade_english int,
grade_sum int
);
映射器和控制器写法
在springboot中使用mybatis框架的步骤如下
1.写实体类
//学生实体类
public class Student {
//id是学生的唯一编号
private int id;
private int num;
private String name;
private int grade_chinese;
private int grade_math;
private int grade_english;
private int grade_sum;
public Student(int id, int num, String name, int grade_chinese, int grade_math, int grade_english, int grade_sum) {
this.id = id;
this.num = num;
this.name = name;
this.grade_chinese = grade_chinese;
this.grade_math = grade_math;
this.grade_english = grade_english;
this.grade_sum = grade_sum;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student(int num, String name, int grade_chinese, int grade_math, int grade_english) {
this.num = num;
this.name = name;
this.grade_chinese = grade_chinese;
this.grade_math = grade_math;
this.grade_english = grade_english;
this.grade_sum=grade_chinese+grade_math+grade_english;
}
public void setNum(int num) {
this.num = num;
}
public void setName(String name) {
this.name = name;
}
public void setGrade_chinese(int grade_chinese) {
this.grade_chinese = grade_chinese;
}
public void setGrade_math(int grade_math) {
this.grade_math = grade_math;
}
public void setGrade_english(int grade_english) {
this.grade_english = grade_english;
}
public void setGrade_sum(int grade_sum) {
this.grade_sum = grade_sum;
}
public int getNum() {
return num;
}
public String getName() {
return name;
}
public int getGrade_chinese() {
return grade_chinese;
}
public int getGrade_math() {
return grade_math;
}
public int getGrade_english() {
return grade_english;
}
public int getGrade_sum() {
return grade_sum;
}
@Override
public String toString() {
return "Student{" +
"num=" + num +
", name='" + name + '\'' +
", grade_chinese=" + grade_chinese +
", grade_math=" + grade_math +
", grade_english=" + grade_english +
", grade_sum=" + grade_sum +
'}';
}
}
2.写映射类mapper(接口)
@Mapper
@Component
public interface StudentMapper {
//添加一个学生成绩
@Insert("insert into students (num,name, grade_chinese, grade_math, grade_english, grade_sum) values (#{num}, #{name},#{grade_chinese}, #{grade_math}, #{grade_english}, #{grade_sum})")
int addStu(Student student);
//获取所有学生信息
@Select("select * from students")
List<Student> selectAll();
//删除一个学生信息
@Delete("delete from students where id = #{id}")
int deleteStu(int id);
}
3.写service类接口
public interface StudentService {
int addStu(Student student);
List<Student> selectAll();
int deleteStu(int id);
}
4.写service类的实现类
@Component
public class StudentServiceImpl implements StudentService{
@Autowired
StudentMapper studentMapper;
@Override
public int addStu(Student student) {
return studentMapper.addStu(student);
}
@Override
public List<Student> selectAll() {
List<Student> list = studentMapper.selectAll();
return list;
}
@Override
public int deleteStu(int id) {
return studentMapper.deleteStu(id);
}
}
5.写控制器
@Controller
public class StudentController {
@Autowired
private StudentServiceImpl studentService;
//添加一个学生信息
@RequestMapping(value = "/addStu", method = RequestMethod.POST)
public String addStu(@RequestParam("num")int num,
@RequestParam("name") String name,
@RequestParam("chinese") int chinese,
@RequestParam("math") int math,
@RequestParam("english") int english){
Student stu = new Student(num, name, chinese, math, english);
studentService.addStu(stu);
return "index";
}
//显示所有学生成绩
@RequestMapping(value = "/selectAll")
public String selectAll(Model model){
List<Student> list = studentService.selectAll();
model.addAttribute("sts",list);
return "show";
}
//删除学生信息
@RequestMapping(value = "/deleteStu/{id}", method = RequestMethod.GET)
public String deleteStu(@PathVariable("id")int id){
studentService.deleteStu(id);
return "redirect:/selectAll";
}
//返回首页
@RequestMapping("/back")
public String back(){
return "index";
}
}
这样就大功告成啦。
这个demo只是实现了基础的功能,大家可以在这个基础上再加入其它功能。
我把这个demo上传到我的github,大家自取