MyBatis-Plus 的核心插件及其使用介绍
wptr33 2024-12-15 17:13 22 浏览
MyBatis-Plus 是基于 MyBatis 的增强工具,为简化 MyBatis 的开发提供了诸多功能扩展。它的目标是减少重复代码、提高开发效率,提供了 CRUD(Create, Read, Update, Delete)操作的简化方法以及多种实用插件。以下是 MyBatis-Plus 的核心插件及其使用介绍:
1.分页插件(PaginationInterceptor)
分页是开发中常见的需求,MyBatis-Plus 提供了简单易用的分页插件。
配置分页插件:
在 Spring Boot 项目中,配置分页插件很简单:
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
分页查询示例:
// 使用Page对象进行分页查询
Page<User> page = new Page<>(1, 10); // 第1页,每页10条数据
IPage<User> userPage = userMapper.selectPage(page, null);
selectPage方法通过 Page 对象自动封装了分页的参数。
2.乐观锁插件(OptimisticLockerInterceptor)
乐观锁用于在更新数据时避免脏数据的出现,MyBatis-Plus 支持乐观锁插件,它主要通过版本号 version 来控制。
配置乐观锁插件:
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
}
使用乐观锁:
在实体类中增加 @Version 注解标记乐观锁字段,通常是 version 字段。
import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
@Version
private Integer version; // 版本号
}
更新时自动处理版本号:
// 假设当前version为1
User user = userMapper.selectById(1L);
user.setName("New Name");
userMapper.updateById(user); // 执行后,version会自动更新
MyBatis-Plus 会在更新时自动检查 version,如果 version 不匹配,则更新失败。
3.逻辑删除插件(LogicDelete)
逻辑删除是一种常见的数据处理方式,MyBatis-Plus 支持通过逻辑删除插件将删除操作转换为更新操作,使数据不会真正从数据库中删除。
配置逻辑删除插件:
MyBatis-Plus 默认已经支持逻辑删除,无需额外插件配置。只需要在实体类中配置 @TableLogic 注解。
使用逻辑删除:
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
@TableLogic
private Integer deleted; // 逻辑删除字段,1表示已删除,0表示未删除
}
调用逻辑删除:
// 调用逻辑删除
userMapper.deleteById(1L); // 实际上是更新deleted字段为1,而不是物理删除
4.自动填充插件(MetaObjectHandler)
自动填充插件用于在插入或更新数据时,自动设置一些特定字段的值(如创建时间、更新时间)。
实现自动填充功能:
首先需要自定义一个类,实现 MetaObjectHandler 接口,定义填充逻辑。
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
// 插入时自动填充字段
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
// 更新时自动填充字段
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
}
}
实体类中配置自动填充字段:
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class User {
private Long id;
private String name;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime; // 插入时自动填充
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime; // 插入和更新时自动填充
}
5.SQL 性能分析插件(SqlExplainInterceptor)
为了提高开发效率和排查 SQL 问题,MyBatis-Plus 提供了 SQL 性能分析插件,可以在开发环境中输出执行的 SQL 及其消耗时间。
配置 SQL 性能分析插件:
import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public SqlExplainInterceptor sqlExplainInterceptor() {
return new SqlExplainInterceptor();
}
}
该插件主要用于开发环境,不建议在生产环境中使用。
6.防止全表更新与删除插件(BlockAttackInterceptor)
MyBatis-Plus 提供了防止全表更新或删除的插件,防止误操作导致整个表的数据被更新或删除。
配置防止全表更新与删除插件:
import com.baomidou.mybatisplus.extension.plugins.BlockAttackInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public BlockAttackInterceptor blockAttackInterceptor() {
return new BlockAttackInterceptor();
}
}
启用后,当执行 update(null) 或 delete(null)(即没有 where 条件)时会抛出异常。
7.多租户插件(TenantLineInnerInterceptor)
多租户插件允许你在多租户环境中为每个 SQL 自动添加租户 ID,以实现数据隔离。
配置多租户插件:
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public TenantLineInnerInterceptor tenantLineInnerInterceptor() {
return new TenantLineInnerInterceptor(new TenantLineHandler() {
@Override
public Expression getTenantId() {
// 实现返回当前租户 ID 的逻辑
return new LongValue(1L); // 例如租户 ID 为1
}
@Override
public boolean ignoreTable(String tableName) {
// 可以指定某些表不进行多租户处理
return "user".equals(tableName);
}
});
}
}
通过这些插件,MyBatis-Plus 可以显著简化开发过程,减少重复代码,提高效率,同时保障安全性和性能。如果需要使用更多插件或自定义功能,MyBatis-Plus 还提供了丰富的扩展接口供开发者使用。
相关推荐
- 【推荐】一款开源免费、美观实用的后台管理系统模版
-
如果您对源码&技术感兴趣,请点赞+收藏+转发+关注,大家的支持是我分享最大的动力!!!项目介绍...
- Android架构组件-App架构指南,你还不收藏嘛
-
本指南适用于那些已经拥有开发Android应用基础知识的开发人员,现在想了解能够开发出更加健壮、优质的应用程序架构。首先需要说明的是:AndroidArchitectureComponents翻...
- 高德地图经纬度坐标批量拾取(高德地图批量查询经纬度)
-
使用方法在桌面上新建一个index.txt文件,把下面的代码复制进去保存,再把文件名改成index.html保存,双击运行打开即可...
- flutter系列之:UI layout简介(flutter ui设计)
-
简介对于一个前端框架来说,除了各个组件之外,最重要的就是将这些组件进行连接的布局了。布局的英文名叫做layout,就是用来描述如何将组件进行摆放的一个约束。...
- Android开发基础入门(一):UI与基础控件
-
Android基础入门前言:...
- iOS的布局体系-流式布局MyFlowLayout
-
iOS布局体系的概览在我的CSDN博客中的几篇文章分别介绍MyLayout布局体系中的视图从一个方向依次排列的线性布局(MyLinearLayout)、视图层叠且停靠于父布局视图某个位置的框架布局(M...
- TDesign企业级开源设计系统越发成熟稳定,支持 Vue3 / 小程序
-
TDesing发展越来越好了,出了好几套组件库,很成熟稳定了,新项目完全可以考虑使用。...
- WinForm实现窗体自适应缩放(winform窗口缩放)
-
众所周知,...
- winform项目——仿QQ即时通讯程序03:搭建登录界面
-
上两篇文章已经对CIM仿QQ即时通讯项目进行了需求分析和数据库设计。winform项目——仿QQ即时通讯程序01:原理及项目分析...
- App自动化测试|原生app元素定位方法
-
元素定位方法介绍及应用Appium方法定位原生app元素...
- 61.C# TableLayoutPanel控件(c# tabcontrol)
-
摘要TableLayoutPanel在网格中排列内容,提供类似于HTML元素的功能。TableLayoutPanel控件允许你将控件放在网格布局中,而无需精确指定每个控件的位置。其单元格...
- 12个python数据处理常用内置函数(python 的内置函数)
-
在python数据分析中,经常需要对字符串进行各种处理,例如拼接字符串、检索字符串等。下面我将对python中常用的内置字符串操作函数进行介绍。1.计算字符串的长度-len()函数str1='我爱py...
- 如何用Python程序将几十个PDF文件合并成一个PDF?其实只要这四步
-
假定你有一个很无聊的任务,需要将几十个PDF文件合并成一个PDF文件。每一个文件都有一个封面作为第一页,但你不希望合并后的文件中重复出现这些封面。即使有许多免费的程序可以合并PDF,很多也只是简单的将...
- Python入门知识点总结,Python三大数据类型、数据结构、控制流
-
Python基础的重要性不言而喻,是每一个入门Python学习者所必备的知识点,作为Python入门,这部分知识点显得很庞杂,内容分支很多,大部分同学在刚刚学习时一头雾水。...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
面试官:git pull是哪两个指令的组合?
-
git pull命令使用实例 git pull--rebase
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git 执行pull错误如何撤销 git pull fail
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mysql max (33)
- vba instr (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)