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博客

  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-Plus 是一个 Mybatis 的增强工具,在 Mybatis 的基础上进行了功能扩展和常用 CRUD 操作的封装,极大地简化了开发流程。而 InnerInterceptor 则是 Mybatis-Plus 中的一个拦截器接口,可用于实现分表等高级功能。 要实现分表,我们可以通过实现 InnerInterceptor 接口来拦截 SQL 语句并根据需要修改表名。以下是一个简单的示例: ```java public class MyInnerInterceptor implements InnerInterceptor { @Override public void beforePrepare(StatementHandler sh, Connection conn, Integer transactionTimeout) { BoundSql boundSql = sh.getBoundSql(); String sql = boundSql.getSql(); if (sql.startsWith("select")) { // 根据某个条件判断是否需要分表 if (needSharding()) { // 修改表名为实际表名 sql = sql.replace("my_table", "my_table_1"); ReflectUtil.setFieldValue(boundSql, "sql", sql); } } } } ``` 在上面的示例中,我们实现了一个内部拦截器 MyInnerInterceptor,并在其中实现了 beforePrepare 方法。该方法会在执行 SQL 语句之前被拦截,并根据需要修改 SQL 语句中的表名。在这里,我们通过判断是否需要分表来决定是否修改表名,如果需要分表则将表名修改为实际表名。 最后,将 MyInnerInterceptor 注册到 Mybatis-Plus 的拦截器链中即可: ```java @Configuration public class MybatisPlusConfig { @Bean public MyInnerInterceptor myInnerInterceptor() { return new MyInnerInterceptor(); } @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(MyInnerInterceptor myInnerInterceptor) { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(myInnerInterceptor); return interceptor; } } ``` 通过以上步骤,我们就可以使用 InnerInterceptor 来实现分表等高级功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值