Mybatis-Plus扩展接口InnerInterceptor

news/2024/10/21 11:58:57/

InnerInterceptor 接口就是 MyBatis-Plus 提供的一个拦截器接口,用于实现一些常用的 SQL 处理逻辑,处理 MyBatis-Plus 的特定功能,例如PaginationInnerInterceptor、OptimisticLockerInnerInterceptor 等,都实现了 InnerInterceptor 接口,并添加了各自特定的功能。

java">public interface InnerInterceptor {/*** 当执行查询操作时,MyBatis 会调用该方法,判断是否需要执行查询操作。默认返回true,表示继续执行查询操作,* 如果需要阻止查询操作,则可以在实现该方法时返回false。** 判断是否执行 {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)}* <p>* 如果不执行query操作,则返回 {@link Collections#emptyList()}** @param executor      Executor(可能是代理对象)* @param ms            MappedStatement* @param parameter     parameter* @param rowBounds     rowBounds* @param resultHandler resultHandler* @param boundSql      boundSql* @return 新的 boundSql*/default boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {return true;}/*** 在执行查询操作之前,MyBatis 会调用该方法。通过实现该方法,可以在查询之前进行一些必要的操作,例如设置数据范围、修改 SQL 等。** {@link Executor#query(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)} 操作前置处理* <p>* 改改sql啥的** @param executor      Executor(可能是代理对象)* @param ms            MappedStatement* @param parameter     parameter* @param rowBounds     rowBounds* @param resultHandler resultHandler* @param boundSql      boundSql*/default void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {// do nothing}/*** 当执行更新操作时,MyBatis 会调用该方法,判断是否需要执行更新操作。默认返回true,表示继续执行更新操作,* 如果需要阻止更新操作,则可以在实现该方法时返回false。** 判断是否执行 {@link Executor#update(MappedStatement, Object)}* <p>* 如果不执行update操作,则影响行数的值为 -1** @param executor  Executor(可能是代理对象)* @param ms        MappedStatement* @param parameter parameter*/default boolean willDoUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {return true;}/*** 在执行更新操作之前,MyBatis 会调用该方法。通过实现该方法,可以在更新之前进行一些必要的操作,例如设置更新时间、加密数据等。** {@link Executor#update(MappedStatement, Object)} 操作前置处理* <p>* 改改sql啥的** @param executor  Executor(可能是代理对象)* @param ms        MappedStatement* @param parameter parameter*/default void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {// do nothing}/*** 在执行 SQL 之前,MyBatis 会调用该方法。通过实现该方法,可以在 SQL 执行之前进行一些必要的操作,例如设置事务隔离级别、设置查询超时时间等。* * {@link StatementHandler#prepare(Connection, Integer)} 操作前置处理* <p>* 改改sql啥的** @param sh                 StatementHandler(可能是代理对象)* @param connection         Connection* @param transactionTimeout transactionTimeout*/default void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {// do nothing}/*** 在获取 BoundSql 对象之前,MyBatis 会调用该方法。通过实现该方法,可以在获取 BoundSql 对象之前进行一些必要的操作,例如设置参数、修改 SQL 等。** {@link StatementHandler#getBoundSql()} 操作前置处理* <p>* 只有 {@link BatchExecutor} 和 {@link ReuseExecutor} 才会调用到这个方法** @param sh StatementHandler(可能是代理对象)*/default void beforeGetBoundSql(StatementHandler sh) {// do nothing}/*** 设置拦截器属性。该方法在创建拦截器实例时调用,用于设置拦截器的属性。*/default void setProperties(Properties properties) {// do nothing}

应用案例:

Mybatis-Plus分页拦截器

Mybatis-Plus乐观锁拦截器

拦截器生效配置:

java">@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();// 乐观锁mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());// 分页插件mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());return mybatisPlusInterceptor;}
}

参考资料:

mybatisPlus的InnerInterceptor接口讲解-CSDN博客


http://www.ppmy.cn/news/1446772.html

相关文章

FSNotes for Mac v6.7.1中文激活:轻量级笔记管理工具

FSNotes for Mac&#xff0c;一款专为Mac用户打造的轻量级笔记管理工具&#xff0c;让您的笔记管理变得简单而高效。 FSNotes for Mac v6.7.1中文激活版下载 它采用Markdown文件格式&#xff0c;让您轻松创建和编辑富文本笔记&#xff0c;无需担心格式问题。同时&#xff0c;FS…

吐槽3家知名的AI智能体

关注卢松松&#xff0c;会经常给你分享一些我的经验和观点。 我花了2天时间&#xff0c;把松松最近1年的爆款文案关键词情绪口头禅整理出来&#xff0c;4000多字的Prompt&#xff0c;都是一点点打出来的&#xff0c;再投喂到AI大模型里。使用的平台包括&#xff1a;通义千问、…

关于apache+php用户验证

一.直接在apache配置配置用户信息 1.apache配置可以参考外部文档 https://developer.aliyun.com/article/507049 2.上面配置好在php获取用户信息&#xff08;登录apache会拦截&#xff09; $userName $_SERVER[PHP_AUTH_USER];$password $_SERVER[PHP_AUTH_PW]; 二.上面直…

音视频入门基础:像素格式专题(1)——RGB简介

一、像素格式简介 像素格式&#xff08;pixel format&#xff09;指像素色彩按分量的大小和排列。这种格式以每个像素所使用的总位数以及用于存储像素色彩的红、绿、蓝和 alpha 分量的位数指定。在音视频领域&#xff0c;常用的像素格式包括RGB格式和YUV格式&#xff0c;本文…

C语言-嵌入式-STM32:FreeRTOS说明和详解

Free即免费的&#xff0c;RTOS的全称是Real time operating system&#xff0c;中文就是实时操作系统。 注意&#xff1a;RTOS不是指某一个确定的系统&#xff0c;而是指一类操作系统。比如&#xff1a;uc/OS&#xff0c;FreeRTOS&#xff0c;RTX&#xff0c;RT-Thread 等这些都…

手写一个uart协议——rs232(未完)

先了解一下关于uart和rs232的基础知识 文章目录 一、RS232的回环测试1.1模块整体架构1.2 rx模块设计1.2.1 波形设计1.2.2代码实现与tb1.2.4 仿真 1.3 tx模块设计1.3.1波形设计 本篇内容&#xff1a; 一、RS232的回环测试 上位机由串口助手通过 rx 线往 FPGA 发 8 比特数据&a…

【氮化镓】一种新型的p-GaN/p-AlGaN/AlGaN/GaN异质结场效应晶体管

文章由韩国首尔弘益大学电子与电气工程学院的Dong-Guk Kim等人撰写&#xff0c;题为“P-GaN/p-AlGaN/AlGaN/GaN heterojunction field-effect transistor with a threshold voltage of 6 V”&#xff0c;发表在IEEE Electron Device Letters上。文章提出了一种新型的p-GaN/p-Al…

react函数式组件每次setstate内部变量重新赋值问题

react函数式组件每次setstate内部变量重新赋值问题 背景解决办法 背景 react函数式组件每次setstate内部变量重新初始化赋值问题&#xff0c;导致不能够对变量正常赋值和读值。 const App: FC ({ }) > {let a:string 0const [status, setStatus] useState<string>…