Mybatis-Plus扩展接口InnerInterceptor

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

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乐观锁拦截器

拦截器生效配置:

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

参考资料:

mybatisPlus的InnerInterceptor接口讲解-CSDN博客

相关推荐

  1. MyBatis-Plus】常用的内置接口

    2024-05-01 12:32:03       44 阅读
  2. MyBatis-Plus 内置接口方法使用

    2024-05-01 12:32:03       42 阅读
  3. MyBatis-Plus之内置接口&Service接口&Mapper接口

    2024-05-01 12:32:03       35 阅读
  4. MyBatis-Plus】内置接口的使用之CRUD

    2024-05-01 12:32:03       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-01 12:32:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-01 12:32:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-01 12:32:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-01 12:32:03       20 阅读

热门阅读

  1. 深入解析Jackson的ObjectMapper:核心功能与方法指南

    2024-05-01 12:32:03       11 阅读
  2. W801学习笔记十六:掌机系统——启动第一个应用

    2024-05-01 12:32:03       13 阅读
  3. 论文辅助笔记:TEMPO 之 utils.py

    2024-05-01 12:32:03       12 阅读
  4. list模拟实现

    2024-05-01 12:32:03       10 阅读
  5. 六石管理学:有目的,再投诉

    2024-05-01 12:32:03       12 阅读
  6. Docker的介绍及与传统虚拟化技术的区别

    2024-05-01 12:32:03       12 阅读
  7. 3D,kd-tree算法原理

    2024-05-01 12:32:03       11 阅读
  8. 互联网的路由选择协议

    2024-05-01 12:32:03       11 阅读
  9. 2021 OWASP Top 10-零基础案例学习

    2024-05-01 12:32:03       10 阅读