MyBatisPlus框架
1. MyBatisPlus简介
MyBatis-Plus作为MyBatis的增强工具,在国内Java企业级开发中已成为事实上的标准选择之一。它显著提升了开发效率,降低了样板代码量,同时保持了MyBatis的灵活性。虽然也存在一些局限,但在大多数业务场景下,其优势远大于缺点。
MyBatisPlus核心优势
1. 强大的CRUD操作简化
-
内置通用Mapper:通过继承
BaseMapper
接口,无需编写简单CRUD的XML/SQL -
ActiveRecord模式支持:实体类继承
Model
类可直接进行数据库操作 -
Lambda表达式支持:类型安全的查询条件构造,避免字段名硬编码
2. 高效的分页插件
-
物理分页:自动转换为数据库方言的分页语句
-
无侵入设计:只需简单配置即可使用
-
多种分页方式:支持PageHelper式分页和IPage接口分页
3. 代码生成器
-
一键生成:实体类、Mapper接口、Service、Controller等
-
高度可定制:支持自定义模板、过滤表等
-
多数据库支持:MySQL、Oracle、PostgreSQL等
4. 优秀的条件构造器
-
QueryWrapper/LambdaQueryWrapper:灵活构建复杂查询条件
-
UpdateWrapper/LambdaUpdateWrapper:便捷构造更新操作
-
防SQL注入:自动处理参数绑定,提高安全性
5. 多租户支持
-
开箱即用:通过简单配置实现多租户数据隔离
-
多种策略:Schema级、数据行级等隔离方式
6. 乐观锁支持
-
@Version注解:简单实现乐观锁控制
-
自动版本号管理:更新时自动检测数据版本
2. MybatisPlus入门
带大家简单的的入个门,通过SpringBoot整合MybatisPlus
2.1创建模块
根据Spring Initializr生成模块
2.2 导依赖
<?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.5.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>mp01</artifactId><version>0.0.1-SNAPSHOT</version><name>mp01</name><description>mp01</description><url/><properties><java.version>17</java.version></properties><!-- 依赖--><dependencies><!-- springmvc依赖,内置tomcat--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><!-- springboot 整合jdbc--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!-- mybatis --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-spring-boot3-starter</artifactId><version>3.5.7</version></dependency><!-- druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-3-starter</artifactId><version>1.2.23</version></dependency><!-- junit测试 +springboot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><!-- 指定版本号 --><version>1.18.24</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></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><version>1.18.24</version></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>
2.3 yml配置文件配置
server:port: 8080
spring:datasource:username: rootpassword: 1234url: jdbc:mysql:///db1type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driver
2.4 数据库,pojo实体类
package com.example.mp01.pojo;import lombok.Data;import java.util.Date;@Data
public class Emp {private int id;private String ename;private int mgr;private Date joindate;private double salary;private double bonus;
// private int jobId;
// private int deptId;}
2.5 EmpMapper接口
接口需要继承BaseMapper<Emp>,BaseMapper提供了针对Emp实体类的CRUD操作方法 将实体类中的字段进行一一匹配
package com.example.mp01.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mp01.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;/*** 员工信息Mapper接口,继承自MyBatis Plus的BaseMapper* BaseMapper提供了针对Emp实体类的CRUD操作方法 将实体类中的字段进行一一匹配*/
@Mapper
public interface EmpMapper extends BaseMapper<Emp> {}
2.6 EmpService
package com.example.mp01.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.example.mp01.pojo.Emp;public interface EmpService extends IService<Emp> {}
2.7 EmpServiceImpl实现类
ServiceImpl<EmpMapper, Emp>:这是 MyBatis Plus 提供的一个通用 Service 实现类,封装了常见的数据库CRUD操作方法。 EmpMapper 是数据访问层接口,Emp 是实体类,对应数据库中的员工表。implements EmpService:表示此类实现了 EmpService 接口中定义的所有方法。 EmpService 继承自 MyBatis Plus 的 IService 接口,可以在此基础上扩展业务方法。
package com.example.mp01.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mp01.mapper.EmpMapper;
import com.example.mp01.pojo.Emp;
import com.example.mp01.service.EmpService;
import org.springframework.stereotype.Service;@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {}
2.8 创建测试类测试
package com.example.mp01.service.impl;;import com.example.mp01.pojo.Emp;
import com.example.mp01.service.EmpService;
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
class EmpServiceImplTest {@Autowiredprivate EmpService empService;@Testpublic void crud() {
// 查询Emp emp = empService.getById(1001l);System.out.println(emp);
// 查询所有
// List<Emp> list = empService.list();
// System.out.println(list);// 新增emp.setId(1009);emp.setEname("弼马温333");emp.setBonus(10000);
// boolean save = empService.save(emp);
// System.out.println(save);// 修改
// boolean b = empService.updateById(emp);
// System.out.println(b);// 删除boolean b = empService.removeById(1009);System.out.println(b);}}
到这里就带大家入门完成了~他的官网在这,大家可以自行去翻一翻:简介 | MyBatis-Plus
3. MybatisPlus分页查询
MybatisPlus具有非常丰富的功能,
-
物理分页:自动生成数据库特定的分页语句(如 MySQL 的
LIMIT
)。 -
逻辑分页:支持内存分页(小数据量时灵活处理)。
-
多表联查分页:通过自定义 SQL 或
@InterceptorIgnore
注解处理复杂查询。
-
无缝对接 CRUD:在
selectPage()
、selectMapsPage()
等方法中直接传入Page
对象,与条件构造器(QueryWrapper
)结合使用,代码简洁。 -
兼容原生 MyBatis:保留 XML 或注解方式的自定义 SQL,通过
IPage
接口实现分页
实现步骤如下:
3.1 配置分页拦截器
package com.example.mp01.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MpConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 添加分页插件interceptor.addInnerInterceptor(new PaginationInnerInterceptor());// 可添加其他拦截器,例如乐观锁插件// interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}
}
配置完分页拦截器,分页功能就实现了
3.2 执行分页查询
package com.example.mp01.service.impl;;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mp01.pojo.Emp;
import com.example.mp01.service.EmpService;
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
class EmpServiceImplTest {@Autowiredprivate EmpService empService;// 分页查询@Testpublic void page() {// 创建分页对象,当前页为第1页,每页显示3条数据Page<Emp> empPage = new Page<>(1, 3);// 执行分页查询empService.page(empPage);// 输出分页信息System.out.println("当前页码: " + empPage.getCurrent());System.out.println("当前页数据: " + empPage.getRecords());System.out.println("总页数: " + empPage.getPages());System.out.println("总条数: " + empPage.getTotal());System.out.println("当前页条数: " + empPage.getSize()); }}
好了,到这里MybatisPlus入门就完成了~伙伴们赶快去实现一下吧