<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.6.2version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>day6artifactId>
<version>0.0.1-SNAPSHOTversion>
<name>day6name>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.8version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
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>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.8version>
dependency>
package com.example.day6.bean;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
//指定实体类对应的表名
@TableName("stu3")
public class Stu3 {
private int id;
private String name;
private int age;
private String sex;
private int math;
}
用于指定实体类对应的表名
//指定实体类对应的表名
@TableName("stu3")
package com.example.day6.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 分页插件配置
*/
@Configuration
public class MybatisPageConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.H2);
paginationInnerInterceptor.setMaxLimit(50L);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
https://baomidou.com/pages/8f40ae/
package com.example.day6.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.day6.bean.Stu3;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StuMapper extends BaseMapper<Stu3> {
}
package com.example.day6.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.day6.bean.Stu3;
public interface StuService extends IService<Stu3> {
}
package com.example.day6.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.day6.bean.Stu3;
import com.example.day6.mapper.StuMapper;
import com.example.day6.service.StuService;
import org.springframework.stereotype.Service;
@Service
public class StuServiceImpl extends ServiceImpl<StuMapper,Stu3> implements StuService {
}
package com.example.day6.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day6.bean.Stu3;
import com.example.day6.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;
@Controller
public class StuController {
@Autowired
StuService service;
// @ResponseBody
@GetMapping("/form")
public String stu_table(@RequestParam(value = "png",defaultValue = "1") Integer png,Model model){
List<Stu3> list = service.list();
//分页查询
Page<Stu3> page = new Page<Stu3>(png,2);
//分页查询的结果
Page<Stu3> resultPage = service.page(page, null);
//获取完整的分页信息
//获取当前页
long current = resultPage.getCurrent();
//获取总页数
long pages = resultPage.getPages();
//获取总记录数
long total = resultPage.getTotal();
//获取详细查询信息
List<Stu3> records = resultPage.getRecords();
model.addAttribute("page",page);
return "Form";
}
@GetMapping("/stu/delete/{id}")
public String deleteStu(@PathVariable("id") Long id,
@RequestParam(value = "png",defaultValue = "1") Integer png,
RedirectAttributes attributes){
//删除数据
service.removeById(id);
//重定向
attributes.addAttribute("png",png);
return "redirect:/form";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
td{
height: 30px;
width: 120px;
}
#pageNum{
height: 40px;
width: 40px;
border-radius: 50%;
background-color: silver;
line-height: 40px;
text-align: center;
}
#pageNum a{
display: inline-block;
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<h1>Form 表单</h1>
<table>
<thead>
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>sex</td>
<td>math</td>
<td>删除</td>
</tr>
</thead>
<tbody>
<tr th:each="stu,index:${page.records}">
<td th:text="${stu.id}"></td>
<td th:text="${stu.name}"></td>
<td th:text="${stu.age}"></td>
<td th:text="${stu.sex}"></td>
<td th:text="${stu.math}"></td>
<td>
<a th:href="@{/stu/delete/{id}(id=${stu.id},png=${page.current})}">删除</a>
</td>
</tr>
</tbody>
</table>
<div>当前:[[${page.current}]],共计[[${page.pages}]]页</div>
<div>共计:[[${page.total}]]条记录</div>
<div>
<ul>
<li id="pageNum" th:each="pageNum:${#numbers.sequence(1,page.pages)}">
<a th:href="@{/form(png=${pageNum})}">
[[${pageNum}]]
</a>
</li>
</ul>
</div>
</body>
</html>