Mybatis-Plus基本功能详解

📅 发布时间:2026/7/5 2:21:09 👁️ 浏览次数:
Mybatis-Plus基本功能详解
一、为什么选择 MyBatis-PlusMyBatis 虽然灵活但需要手动编写大量 XML 映射文件或注解 SQL而 MyBatis-Plus 解决了这些痛点无侵入完全兼容 MyBatis原有代码无需修改即可升级。CRUD 自动生成内置通用 Mapper/Service单表操作无需写 SQL。强大的条件构造器支持链式调用轻松构建复杂查询条件。自动分页无需手动处理分页参数分页查询一键实现。逻辑删除无需修改 SQL自动处理软删除逻辑。代码生成器一键生成 Entity、Mapper、Service、Controller 全套代码。二、环境搭建Spring Boot 整合1. 引入依赖在 pom.xml 中添加 MyBatis-Plus 核心依赖以 MySQL 为例!-- Spring Boot 父工程 -- parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version2.7.10/version relativePath/ /parent !-- 核心依赖 -- dependencies !-- MyBatis-Plus 核心包 -- dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-boot-starter/artifactId version3.5.3.1/version /dependency !-- MySQL 驱动 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId scoperuntime/scope /dependency !-- Lombok简化实体类 -- dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId optionaltrue/optional /dependency !-- Spring Boot 测试 -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-test/artifactId scopetest/scope /dependency /dependencies2. 配置数据库连接在 application.yml 中配置数据库和 MyBatis-Plus 基本信息spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mp_demo?useUnicodetruecharacterEncodingutf8useSSLfalseserverTimezoneAsia/Shanghai username: root password: 123456 # MyBatis-Plus 配置 mybatis-plus: # 实体类别名包扫描 type-aliases-package: com.example.mpdemo.entity # 配置 mapper.xml 扫描路径可选 mapper-locations: classpath:mapper/**/*.xml # 日志配置方便调试 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 全局配置 global-config: db-config: # 主键自增策略 id-type: auto # 逻辑删除字段名可选 logic-delete-field: isDeleted # 逻辑删除-未删除值 logic-not-delete-value: 0 # 逻辑删除-已删除值 logic-delete-value: 13. 启动类添加注解在 Spring Boot 启动类上添加 MapperScan扫描 Mapper 接口包import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication // 扫描 Mapper 接口所在包 MapperScan(com.example.mpdemo.mapper) public class MpDemoApplication { public static void main(String[] args) { SpringApplication.run(MpDemoApplication.class, args); } }三、核心功能实战1. 基础 CRUD 操作无需写 SQL步骤 1创建实体类以 User 表为例使用 Lombok 简化代码import com.baomidou.mybatisplus.annotation.*; import lombok.Data; import java.time.LocalDateTime; Data // Lombok 自动生成 getter/setter/toString 等 TableName(user) // 指定数据库表名若类名与表名一致可省略 public class User { // 主键对应全局配置的 auto 自增 TableId(type IdType.AUTO) private Long id; // 用户名若字段名与属性名一致可省略 TableField private String username; // 密码 private String password; // 年龄 private Integer age; // 邮箱 private String email; // 逻辑删除字段 TableLogic private Integer isDeleted; // 创建时间自动填充 TableField(fill FieldFill.INSERT) private LocalDateTime createTime; // 更新时间自动填充 TableField(fill FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; }步骤 2创建 Mapper 接口继承 BaseMapper自动获得 CRUD 方法import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.mpdemo.entity.User; import org.apache.ibatis.annotations.Mapper; Mapper public interface UserMapper extends BaseMapperUser { // 无需写任何代码BaseMapper 已包含所有单表 CRUD }步骤 3测试 CRUD 操作import com.example.mpdemo.entity.User; import com.example.mpdemo.mapper.UserMapper; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; SpringBootTest public class CrudTest { Autowired private UserMapper userMapper; // 新增 Test public void testInsert() { User user new User(); user.setUsername(张三); user.setPassword(123456); user.setAge(20); user.setEmail(zhangsantest.com); // 插入数据返回受影响行数 int rows userMapper.insert(user); System.out.println(新增成功受影响行数 rows 主键ID user.getId()); } // 查询单个 Test public void testSelectById() { User user userMapper.selectById(1L); System.out.println(user); } // 查询所有 Test public void testSelectList() { ListUser userList userMapper.selectList(null); // null 表示无查询条件 userList.forEach(System.out::println); } // 更新 Test public void testUpdateById() { User user new User(); user.setId(1L); user.setAge(21); // 只更新年龄 int rows userMapper.updateById(user); System.out.println(更新成功受影响行数 rows); } // 删除物理删除 Test public void testDeleteById() { int rows userMapper.deleteById(1L); System.out.println(删除成功受影响行数 rows); } // 逻辑删除实际执行 update将 isDeleted 设为 1 Test public void testLogicDelete() { int rows userMapper.deleteById(2L); System.out.println(逻辑删除成功受影响行数 rows); } }2. 条件构造器Wrapper构建复杂查询QueryWrapper/LambdaQueryWrapper 是 MP 最强大的功能之一支持链式构建查询条件import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.mpdemo.entity.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; SpringBootTest public class WrapperTest { Autowired private UserMapper userMapper; // 基础 QueryWrapper字符串字段名易出错 Test public void testQueryWrapper() { QueryWrapperUser wrapper new QueryWrapper(); // 年龄大于 18 且用户名包含 张 且邮箱不为空 wrapper.gt(age, 18) .like(username, 张) .isNotNull(email); ListUser userList userMapper.selectList(wrapper); userList.forEach(System.out::println); } // LambdaQueryWrapper类型安全推荐 Test public void testLambdaQueryWrapper() { LambdaQueryWrapperUser wrapper new LambdaQueryWrapper(); // 避免手写字段名防止拼写错误 wrapper.gt(User::getAge, 18) .like(User::getUsername, 张) .isNotNull(User::getEmail); ListUser userList userMapper.selectList(wrapper); userList.forEach(System.out::println); } // 分页查询 Test public void testPage() { // 1. 构建分页条件第 1 页每页 10 条 PageUser page new Page(1, 10); // 2. 构建查询条件 LambdaQueryWrapperUser wrapper new LambdaQueryWrapper(); wrapper.gt(User::getAge, 18); // 3. 执行分页查询 PageUser resultPage userMapper.selectPage(page, wrapper); // 4. 获取分页结果 System.out.println(总记录数 resultPage.getTotal()); System.out.println(总页数 resultPage.getPages()); System.out.println(当前页数据); resultPage.getRecords().forEach(System.out::println); } }3. 自动填充创建 / 更新时间实现 MetaObjectHandler自动填充创建时间和更新时间import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.time.LocalDateTime; Component // 必须交给 Spring 管理 public class MyMetaObjectHandler implements MetaObjectHandler { // 插入时填充 Override public void insertFill(MetaObject metaObject) { // 填充 createTime 和 updateTime this.strictInsertFill(metaObject, createTime, LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now()); } // 更新时填充 Override public void updateFill(MetaObject metaObject) { // 只填充 updateTime this.strictUpdateFill(metaObject, updateTime, LocalDateTime.class, LocalDateTime.now()); } }4. 代码生成器一键生成全套代码MP 提供代码生成器可快速生成 Entity、Mapper、Service、Controller减少重复工作。引入代码生成器依赖!-- MyBatis-Plus 代码生成器 -- dependency groupIdcom.baomidou/groupId artifactIdmybatis-plus-generator/artifactId version3.5.3.1/version /dependency !-- 模板引擎Freemarker -- dependency groupIdorg.freemarker/groupId artifactIdfreemarker/artifactId version2.3.31/version /dependency编写生成器代码import com.baomidou.mybatisplus.generator.FastAutoGenerator; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.Collections; public class CodeGenerator { public static void main(String[] args) { // 数据库连接配置 String url jdbc:mysql://localhost:3306/mp_demo?useUnicodetruecharacterEncodingutf8useSSLfalseserverTimezoneAsia/Shanghai; String username root; String password 123456; // 快速生成代码 FastAutoGenerator.create(url, username, password) // 全局配置 .globalConfig(builder - { builder.author(你的名字) // 设置作者 .outputDir(System.getProperty(user.dir) /src/main/java) // 输出目录 .enableSwagger() // 开启 Swagger可选 .commentDate(yyyy-MM-dd) // 注释日期格式 .disableOpenDir(); // 生成后不打开文件夹 }) // 包配置 .packageConfig(builder - { builder.parent(com.example.mpdemo) // 父包名 .moduleName() // 模块名无则空 .entity(entity) // 实体类包名 .mapper(mapper) // Mapper 包名 .service(service) // Service 包名 .controller(controller) // Controller 包名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty(user.dir) /src/main/resources/mapper)); // Mapper XML 路径 }) // 策略配置 .strategyConfig(builder - { builder.addInclude(user) // 要生成的表名多个用逗号分隔 .addTablePrefix(t_, sys_) // 忽略表前缀如 t_user → User // 实体类策略 .entityBuilder() .enableLombok() // 启用 Lombok .enableTableFieldAnnotation() // 生成字段注解 // Controller 策略 .controllerBuilder() .enableRestStyle() // 生成 RestController .enableHyphenStyle() // URL 中驼峰转连字符如 userInfo → user-info // Service 策略 .serviceBuilder() .formatServiceFileName(%sService) // Service 命名规则 .formatServiceImplFileName(%sServiceImpl); }) // 模板引擎Freemarker .templateEngine(new FreemarkerTemplateEngine()) // 执行生成 .execute(); } }四、进阶技巧1. 自定义 SQL若 MP 内置方法无法满足需求可自定义 SQL兼容 MyBatis 写法// UserMapper.java Select(SELECT * FROM user WHERE age #{age}) ListUser selectByAge(Param(age) Integer age); // 或在 UserMapper.xml 中编写 select idselectByAge resultTypecom.example.mpdemo.entity.User SELECT * FROM user WHERE age #{age} /select2. 多表关联查询MP 不直接支持多表关联但可通过 Select 注解或 XML 实现结合 Wrapper 灵活拼接条件。3. 乐观锁解决并发更新问题步骤实体类添加版本字段 VersionVersionprivate Integer version;配置乐观锁插件import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class MyBatisPlusConfig { Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor new MybatisPlusInterceptor(); // 添加乐观锁插件 interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 添加分页插件 interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } }