MyBatisPlus的测试案例(更新中)
创建config文件夹-创建MybatisPlusConfig,配置分页查询的拦截器
package com.test.springboot_test.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mpInterceptor() {
//1.定义mybatisplus的拦截器
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
//2.在mybatisplus的拦截器中添加小的拦截器
mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mpInterceptor;
}
}
测试案例:代码
//2025-4-21对mp进行复盘
@Test
void review() {
//1.分页查询
/* QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
IPage page = new Page(1,4);
mapper.selectPage(page, employeeQueryWrapper);
System.out.println("当前页码" + page.getCurrent());
System.out.println("当前页面显示数" + page.getSize());
System.out.println("当前页码的数据" + page.getRecords());
System.out.println("总记录数" + page.getTotal());
System.out.println("总页码" + page.getPages());*/
//2.等值查询
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getId, 1);
List<Employee> employees = mapper.selectList(queryWrapper);
for (Employee employee : employees) {
System.out.println(employee);
}
*/
//等值查询:selectOne,根据id查询只有一条数据
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getId, 1);
Employee employee = mapper.selectOne(queryWrapper);
System.out.println(employee);
*/
//3.1模糊查询 王%
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
//这里查询的是:%在右边,王字开头的数据,日志里面是:王%
queryWrapper.likeRight(Employee::getName, "王");
List<Employee> list = mapper.selectList(queryWrapper);
for (Employee employee : list) {
System.out.println(employee);
}
*/
//3.2 %五 以五结尾
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.likeLeft(Employee::getName, "五");
List<Employee> employees = mapper.selectList(queryWrapper);
for (Employee employee : employees) {
System.out.println(employee);
}
*/
//4.1范围查询 between 查询1-3之间的数据 结果是 id=1,2,3的三条记录
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.between(Employee::getId, 1, 3);
List<Employee> list = mapper.selectList(queryWrapper);
for (Employee employee : list) {
System.out.println(employee);
}
*/
//4.2 lt gt 的使用 结果是id = 2,3,4的三条记录
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.lt(Employee::getId, 5);//id<5
queryWrapper.gt(Employee::getId, 1);//id>1
List<Employee> employees = mapper.selectList(queryWrapper);
for (Employee employee : employees) {
System.out.println(employee);
}
*/
//4.3 le ge 的使用 结果是id = 1,2,3,4,5的五条记录(根据数据库里面的id来决定)
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.le(Employee::getId, 5);//id<=5
queryWrapper.ge(Employee::getId, 1);//id>=1
List<Employee> list = mapper.selectList(queryWrapper);
for (Employee employee : list) {
System.out.println(employee);
}
*/
//5条件非空判断
/*
EmployeeQuery employeeQuery = new EmployeeQuery();
employeeQuery.setId(1);
employeeQuery.setId2(3);
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.le(null!=employeeQuery.getId2(),Employee::getId, employeeQuery.getId2());//id<=3
queryWrapper.ge(null!=employeeQuery.getId(),Employee::getId, employeeQuery.getId());//id>=1
List<Employee> employees = mapper.selectList(queryWrapper);
for (Employee employee : employees) {
System.out.println(employee);
}
*/
//6链式编程
/*
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.lt(Employee::getId, 4).gt(Employee::getId, 2);
List<Employee> list = mapper.selectList(queryWrapper);
for (Employee employee : list) {
System.out.println(employee);
}
*/
//7.用QueryWrapper,查询具体字段(查询投影)
/*
QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id,name,email");//这里不查询password
List<Employee> list = mapper.selectList(queryWrapper);
for (Employee employee : list) {
System.out.println(employee);//所以数据库查出来是null
}
*/
//8.用QueryWrapper,使用聚合函数(查询投影),统计表中的记录数,再进行分组,(这里是QueryWrapper独特的使用场景,没有使用Lambda格式)
/*
QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
queryWrapper.select("count(*) as count,email");
queryWrapper.groupBy("email");
//查询分完组的每个模块的总记录数
//SELECT count(*) as count,email FROM employee GROUP BY email
List<Employee> employees = mapper.selectList(queryWrapper);
for (Employee employee : employees) {
System.out.println(employee);
}
*/
}
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>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>springboot_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_test</name>
<description>springboot_test</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!--解决HttpServletRequest爆红问题-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!--参数验证valication-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!--分页查询插件-->
<!--下面的配置是可以实现一个mybatisplus单表增删改查的的配置-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
数据库
/*
Navicat Premium Dump SQL
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80040 (8.0.40)
Source Host : localhost:3306
Source Schema : springboot_test
Target Server Type : MySQL
Target Server Version : 80040 (8.0.40)
File Encoding : 65001
Date: 16/04/2025 15:12:17
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`email` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES (1, '王一', '123', '');
INSERT INTO `employee` VALUES (2, '李四', NULL, NULL);
INSERT INTO `employee` VALUES (3, '王五', NULL, NULL);
INSERT INTO `employee` VALUES (4, '王六', NULL, NULL);
INSERT INTO `employee` VALUES (5, '王七', NULL, NULL);
INSERT INTO `employee` VALUES (6, '王八', NULL, NULL);
INSERT INTO `employee` VALUES (7, '王九', NULL, NULL);
INSERT INTO `employee` VALUES (8, '王十', NULL, NULL);
INSERT INTO `employee` VALUES (9, '王五', '123123', '123@qq.com');
INSERT INTO `employee` VALUES (10, '王五', '1231231', '123@qq.com');
SET FOREIGN_KEY_CHECKS = 1;
application.properties
spring.application.name=springboot_test
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_test?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
# 配置mybatisplus的日志,一般在遇见问题的时候会打开查看里面的日志sql数据
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#关闭spring和mybatis在控制台的视图展示,主要是为了页面整洁
spring.main.banner-mode=off
mybatis-plus.global-config.banner=false