首先,创建我们的maven项目。
然后,导入我们的依赖:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.5.5version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>mybatisplus_demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>mybatisplus_demoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.26version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.17version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
<exclude>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
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,并点击修改按钮:(这里由于一开始代码出错,所以截图就有点小偏差,现在的代码已经是正确的了。)