SpringBoot3与MyBatis-Plus

devtools/2025/2/9 5:28:34/

4.1 介绍

MyBatis-Plus(简称 MP)是一个基于 MyBatis 的增强工具,提供通用 CRUD 操作、代码生成器、条件构造器、分页插件等功能,简化开发流程,提升效率。

4.2 特点

  • 无侵入:只做增强不做修改,与 MyBatis 完全兼容。

  • CRUD 简化:内置通用 Mapper 和 Service,减少 SQL 编写。

  • 代码生成器:自动生成 Entity、Mapper、Service、Controller 代码。

  • 条件构造器:通过 QueryWrapperLambdaQueryWrapper 动态构建查询条件。

  • 分页插件:支持物理分页,自动优化 COUNT 语句。

4.3 如何使用

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.10.1</version>
</dependency>

application.yml 配置文件中添加 H2 数据库的相关配置:

# DataSource Config
spring:datasource:driver-class-name: org.h2.Driverusername: rootpassword: 123456sql:init:schema-locations: classpath:db/schema-h2.sqldata-locations: classpath:db/data-h2.sql

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
​public static void main(String[] args) {SpringApplication.run(Application.class, args);}
​
}

编码

编写实体类 User.java

@Data
@TableName("`user`")
public class User {private Long id;private String name;private Integer age;private String email;
}

编写 Mapper 接口类 UserMapper.java

public interface UserMapper extends BaseMapper<User> {
​
}

5. mp-增加

// 插入单条记录
User user = new User();
user.setName("张三");
user.setAge(25);
userMapper.insert(user);
​
// 批量插入
List<User> userList = Arrays.asList(new User("李四", 30), new User("王五", 28));
userMapper.insertBatchSomeColumn(userList);

6. mp-删除

  // 根据 ID 删除
userMapper.deleteById(1L);
​
// 根据条件删除(删除年龄大于 30 的用户)
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.gt("age", 30);
userMapper.delete(wrapper);

7. mp-修改

// 根据 ID 更新
User user = userMapper.selectById(2L);
user.setName("赵六");
userMapper.updateById(user);
​
// 条件更新(将年龄小于 20 的用户设为无效)
UpdateWrapper<User> wrapper = new UpdateWrapper<>();
wrapper.lt("age", 20).set("status", 0);
userMapper.update(null, wrapper);

8. mp-查询

8.1 根据主键查询---省略

User user = userMapper.selectById(1L);

8.2 根据各种条件查询

// 查询年龄在 20-30 之间的用户
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age", 20, 30);
List<User> users = userMapper.selectList(wrapper);
​
// 使用 Lambda 表达式(查询名字包含 "张" 的用户)
LambdaQueryWrapper<User> lambdaWrapper = new LambdaQueryWrapper<>();
lambdaWrapper.like(User::getName, "张");
List<User> users = userMapper.selectList(lambdaWrapper);

8.3 根据条件查询一条记录

QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name", "刘德华");
User user = userMapper.selectOne(wrapper);

8.4 分页查询

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>3.5.10.1</version> <!-- 确保版本和 MyBatis Plus 主包一致 -->
</dependency>
属性名类型默认值描述
overflowbooleanfalse溢出总页数后是否进行处理
maxLimitLong单页分页条数限制
dbTypeDbType数据库类型
dialectIDialect方言实现类

配置类

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
​/*** 添加分页插件*/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多个插件, 切记分页最后添加// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbTypereturn interceptor;}
}

Page<User> page = new Page<>(1, 10); // 第 1 页,每页 10 条
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("create_time");
IPage<User> userPage = userMapper.selectPage(page, wrapper);
​
System.out.println("总页数:" + userPage.getPages());
System.out.println("总记录数:" + userPage.getTotal());

8.5 联表查询也使用mp的分页。

 /*** 联表查询 使用mp的分页功能。*/@Testpublic void testLianbiao(){// @Param("ew") Wrapper<T> queryWrapperPage<User>  page=new Page<>(1,3);QueryWrapper<User> wrapper=new QueryWrapper<>();wrapper.eq("name","刘德华");IPage<User> users=userMapper.selectUserWithDept(page,wrapper);System.out.println("总页码:"+page.getPages());System.out.println("总条数:"+page.getTotal());System.out.println("当前页记录:"+page.getRecords());}

​<resultMap id="baseMaper" type="com.ykq.entity.User" autoMapping="true"><id property="id" column="id"/><association property="dept" javaType="com.ykq.entity.Dept" autoMapping="true"><id column="did" property="did"/></association></resultMap><select id="selectUserWithDept" resultMap="baseMaper">select * from user u join tbl_dept d on u.did=d.did  where isdeleted=0
​<if test="ew!=null">and ${ew.sqlSegment}</if>
​</select>

注意事项

  1. 联表查询需自定义 SQL,MP 分页插件会自动处理分页逻辑。

  2. 使用 QueryWrapper 时,条件字段名需与数据库列名一致。

  3. 分页参数 Pagecurrentsize 需明确指定。


http://www.ppmy.cn/devtools/157258.html

相关文章

树和二叉树_6

树和二叉树_6 一、leetcode-105二、题解1.引库2.代码 一、leetcode-105 从前序与中序遍历序列构造二叉树 给定两个整数数组 preorder 和 inorder &#xff0c;其中 preorder 是二叉树的先序遍历&#xff0c; inorder 是同一棵树的中序遍历&#xff0c;请构造二叉树并返回其根节…

Postman接口测试:全局变量/接口关联/加密/解密

全局变量和环境变量 全局变量&#xff1a;在postman全局生效的变量&#xff0c;全局唯一 环境变量&#xff1a;在特定环境下生效的变量&#xff0c;本环境内唯一 设置&#xff1a; 全局变量&#xff1a; pm.globals.set("variable_key", "variable_value1&q…

maven详细讲解

学习目标 那什么是mavenmaven概念以及核心思想maven构建的生命周期、阶段以及目标maven仓库有哪些&#xff1f;maven依赖 那什么是maven&#xff1f;maven概念以及核心思想&#xff0c;maven构建的生命周期、阶段以及目标&#xff1f; 那什么是maven Maven是一个项目管理和构建…

gitlab个别服务无法启动可能原因

目录 一、gitlab的puma服务一直重启 1. 查看日志 2. 检查配置文件 3. 重新配置和重启 GitLab 4. 检查系统资源 5. 检查依赖和服务状态 6. 清理和优化 7. 升级 GitLab 8. 查看社区和文档 二、 gitlab个别服务无法启动可能原因 1.服务器内存或磁盘已满 2.puma端口冲突…

http cookie的作用学习

1.介绍 HTTP Cookie 是 服务器发送给客户端&#xff08;浏览器&#xff09;的一小段数据&#xff0c;它会被客户端存储&#xff0c;并在后续请求时自动携带&#xff0c;以便服务器识别用户、保持会话状态或存储用户偏好等信息。 流程&#xff1a; 服务器发送 Cookie 服务器…

基于SpringBoot+vue高效旅游管理系统

Spring Boot后端与Vue前端融合&#xff1a;构建高效旅游管理系统 目录 一、项目简介 二、开发技术与环境配置 2.1 SpringBoot框架 2.2 Java语言简介 2.3 Vue的介绍 2.4 mysql数据库介绍 2.5 B/S架构 三、系统功能实现 四、系统项目截图 登录页面 后台管理页面 用户…

SpringBoot3 + Jedis5 + Redis集群 如何通过scan方法分页获取所有keys

背景: 由于需要升级老项目代码&#xff0c;从SpringBoot1.5.x 升级到 SpringBoot3.3.x&#xff0c;框架中引用的Jedis自动升级到了 5.x&#xff1b;正好代码中有需要获取Redis集群的所有keys的需求存在&#xff1b;代码就不适用了&#xff0c;修改如下&#xff1a; POM 由于…

pandas+openpyxl处理Excel

1. 读取多个 Excel 文件并合并 假设你有一个文件夹&#xff0c;里面包含多个 Excel 文件&#xff0c;你想将这些文件合并成一个 DataFrame。 import pandas as pd import os # 文件夹路径 folder_path path/to/your/excel/files # 获取文件夹中的所有 Excel 文件 excel_file…