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注解:简单实现乐观锁控制
自动版本号管理:更新时自动检测数据版本
带大家简单的的入个门,通过SpringBoot整合MybatisPlus
根据Spring Initializr生成模块
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.5.0
com.example
mp01
0.0.1-SNAPSHOT
mp01
mp01
17
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
8.0.33
org.springframework.boot
spring-boot-starter
com.baomidou
mybatis-plus-spring-boot3-starter
3.5.7
com.alibaba
druid-spring-boot-3-starter
1.2.23
org.springframework.boot
spring-boot-test
test
org.projectlombok
lombok
1.18.24
provided
junit
junit
test
org.apache.maven.plugins
maven-compiler-plugin
org.projectlombok
lombok
1.18.24
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
server:
port: 8080
spring:
datasource:
username: root
password: 1234
url: jdbc:mysql:///db1
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
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;
}
接口需要继承BaseMapper
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 {
}
package com.example.mp01.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.mp01.pojo.Emp;
public interface EmpService extends IService {
}
ServiceImpl: 这是 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 implements EmpService {
}
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 {
@Autowired
private EmpService empService;
@Test
public void crud() {
// 查询
Emp emp = empService.getById(1001l);
System.out.println(emp);
// 查询所有
// List 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
MybatisPlus具有非常丰富的功能,
物理分页:自动生成数据库特定的分页语句(如 MySQL 的 LIMIT
)。
逻辑分页:支持内存分页(小数据量时灵活处理)。
多表联查分页:通过自定义 SQL 或 @InterceptorIgnore
注解处理复杂查询。
无缝对接 CRUD:在 selectPage()
、selectMapsPage()
等方法中直接传入 Page
对象,与条件构造器(QueryWrapper
)结合使用,代码简洁。
兼容原生 MyBatis:保留 XML 或注解方式的自定义 SQL,通过 IPage
接口实现分页
实现步骤如下:
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 {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
// 可添加其他拦截器,例如乐观锁插件
// interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
配置完分页拦截器,分页功能就实现了
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 {
@Autowired
private EmpService empService;
// 分页查询
@Test
public void page() {
// 创建分页对象,当前页为第1页,每页显示3条数据
Page 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入门就完成了~伙伴们赶快去实现一下吧