Springboot+Mybatis-Plus写一个表简单的增删改查操作全详细流程(教程)
首先,创建我们的maven项目。
然后,导入我们的依赖:
pom.xml
<?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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybatisplus_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisplus_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--SpringBoot的web场景,用来显示数据用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--该依赖让我们每次编译都能让修改的内容立马生效-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--自动补齐yaml-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--自动为我们写set、get、构造器、toString方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--测试场景-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mybatis-plus场景依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--Thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!--Druid场景-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--不需要放到容器中的两个组件-->
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
之后配置我们的数据源以及打开我们的Hiddenmethod:
application.yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC
username: root
password: root
mvc:
hiddenmethod:
filter:
enabled: true
然后写我们的实体类:
package com.example.boot.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data //自动生成setter和getter
@ToString //自动生成toString方法
@AllArgsConstructor //自动生成全参构造
@NoArgsConstructor //自动生成无参构造
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
}
然后写我们的mapper接口,继承BaseMapper接口。
StudentMapper.java
package com.example.boot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.boot.bean.Student;
public interface StudentMapper extends BaseMapper<Student> {
}
再写我们的Service接口(继承IService接口)和ServiceImpl实现类(他实现对应的Service接口并实现ServiceImpl类,ServiceImpl类刚好动态实现了IService的所有接口)。由于后边我们要对SerciceImpl类进行自动注入,所以我们还得给该类加一个@Service标签。
StudentService.java
package com.example.boot.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.boot.bean.Student;
public interface StudentService extends IService<Student> {
}
StudentServiceImpl.java
package com.example.boot.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.boot.bean.Student;
import com.example.boot.mapper.StudentMapper;
import com.example.boot.service.StudentService;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}
之后,我们让我们的SpringBoot主程序扫描我们的mapper包
package com.example.boot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.boot.mapper")
@SpringBootApplication
public class MybatisplusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusDemoApplication.class, args);
}
}
之后写我们的前端代码:
首先是首页,为了方便,我们姑且将增删查都扔在这里。
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试</title>
<style type="text/css">
input{
margin: 8px 5px;
}
td{
text-align: center;
}
</style>
<script type="text/javascript" th:src="@{/js/jquery-3.6.0.js}"></script>
<script type="text/javascript">
$(function (){
$(".removeBtn").click(function (event){
/*获取a标签的href并赋值给form表单的action属性*/
$(".removeForm").attr("action",$(this).attr("href"));
/*提交form表单*/
$(".removeForm").submit();
//取消超链接的默认行为
event.preventDefault();
});
$(".findBtn").click(function (event){
//获取id值
let id = $(".stuId").val();
//将id值加到表单的action后边
if(id != ''){
$(".findForm").attr("action",$(".findForm").attr("action")+"/"+id);
}
$(".findForm").submit();
})
});
</script>
</head>
<body>
<h2>添加学生</h2>
<div>
<form th:action="@{/student}" method="post">
姓 名:<input type="text" name="name" class="insert_name"><br>
邮 箱:<input type="text" name="email" class="insert_email"><br>
年 龄:<input type="number" name="age" class="insert_age"><br>
<input type="submit" value="提交">
</form>
</div>
<h2>查询学生</h2>
<form class="findForm" th:action="@{/student}" method="get">
<input class="stuId" type="number" name="id">
<input class="findBtn" type="submit" value="查询" th:action="@{/student}">
</form>
<table border="1" width="80%" align="center">
<tr>
<th colspan="5">学生信息列表</th>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<tr th:each="student:${studentList}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.age}"></td>
<td th:text="${student.email}"></td>
<td>
<a class="removeBtn" th:href="@{'/student/'+${student.id}}">删除</a>
<a class="updateBtn" th:href="@{'/student/update/'+${student.id}}">修改</a>
</td>
</tr>
</table>
<!--删除表单-->
<form class="removeForm" method="post">
<input type="hidden" name="_method" value="delete">
</form>
</body>
</html>
然后是修改的页面
update.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>修改学生信息</title>
</head>
<body>
<h2>修改学生信息</h2>
<div class="addStudent">
<form th:action="@{/student}" method="post">
<input type="hidden" name="_method" value="put">
姓 名:<input type="text" name="name" th:value="${stu.name}"><br>
邮 箱:<input type="text" name="email" th:value="${stu.email}"><br>
年 龄:<input type="text" name="age" th:value="${stu.age}"><br>
<input type="submit" value="修改">
</form>
</div>
</body>
</html>
然后是我们的控制器方法:
package com.example.boot.controller;
import com.example.boot.bean.Student;
import com.example.boot.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class StudentController {
@Resource
StudentService studentService;
//添加学生
@PostMapping("/student")
public ModelAndView addStudent(Student student,
ModelAndView modelAndView){
//添加学生到数据库
studentService.save(student);
//更新表单
List<Student> studentList = studentService.list();
modelAndView.addObject("studentList",studentList);
modelAndView.setViewName("index");
return modelAndView;
}
//删除学生
@DeleteMapping("/student/{id}")
public ModelAndView removeStudent(@PathVariable("id") Integer id,
ModelAndView modelAndView){
//删除学生
studentService.removeById(id);
//更新表单
List<Student> studentList = studentService.list();
modelAndView.addObject("studentList",studentList);
modelAndView.setViewName("index");
return modelAndView;
}
//修改学生(两个步骤)
//1、先获取学生信息并保存到request域
@GetMapping("/student/update/{id}")
public ModelAndView findOneForUpdate(ModelAndView modelAndView,
@PathVariable("id") Integer id){
Student student = studentService.getById(id);
modelAndView.addObject("stu",student);
modelAndView.setViewName("update");
return modelAndView;
}
//2、删除学生并返回首页
@PutMapping("/student")
public ModelAndView updateStudent(ModelAndView modelAndView,
Student student){
studentService.updateById(student);
modelAndView.setViewName("index");
return modelAndView;
}
@GetMapping("/student/{id}")
public ModelAndView findOneById(ModelAndView modelAndView,
@PathVariable("id") Integer id){
Student student = studentService.getById(id);
modelAndView.addObject("studentList",student);
modelAndView.setViewName("index");
return modelAndView;
}
@GetMapping("/student")
public ModelAndView findAll(ModelAndView modelAndView){
List<Student> studentList = studentService.list();
modelAndView.addObject("studentList",studentList);
modelAndView.setViewName("index");
return modelAndView;
}
}
运行:
点击查询按钮:
输入1001再点击查询:
输入要添加的数据:
点击编号为2016的学生的删除按钮:
点击修改编号为2021的学生的修改按钮:
把年龄修改为18,并点击修改按钮:(这里由于一开始代码出错,所以截图就有点小偏差,现在的代码已经是正确的了。)