Mybatis Plus的自定义插件

mp 插件处理

首先自定义一个接口QueryInterceptor:用于自定义过滤器的规范

1
2
3
4
5
6
7
8
9
public interface QueryInterceptor extends Ordered {

    void intercept(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql);

    @Override
    default int getOrder() {
        return 2147483647;
    }
}

实现Ordered接口是为了排序,给过滤器排上先后顺序。

PaginationInnerInterceptor是mybatisPlus的过滤器的主要类

定义一个类集成PaginationInnerInterceptor类,重写willDoQuery方法,加上我们自己的过滤器执行逻辑.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class MyPaginationInnerInterceptor extends PaginationInnerInterceptor {

    private QueryInterceptor[] queryInterceptors;


    @Override
    public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        try {
            //执行自己的逻辑
            QueryInterceptorExecutor.executor(this.queryInterceptors,executor,ms,parameter,rowBounds,resultHandler,boundSql);
            return super.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
        } catch (Throwable e) {
            throw e;
        }
    }

    public void setQueryInterceptors(QueryInterceptor[] queryInterceptors) {
        this.queryInterceptors = queryInterceptors;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class QueryInterceptorExecutor {

    public static void executor(QueryInterceptor[] queryInterceptors, Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
        if (queryInterceptors != null && queryInterceptors.length != 0) {
            QueryInterceptor[] var1 = queryInterceptors;
            int length = var1.length;
            for (int i = 0; i < length; i++) {
                QueryInterceptor queryInterceptor = var1[i];
                queryInterceptor.intercept(executor,ms,parameter,rowBounds,resultHandler,boundSql);
            }
        }
    }
}

实现自己的过滤器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
@Component
public class MyQueryInterceptor implements QueryInterceptor {
    private ConcurrentMap<String, Object> concurrentMap = new ConcurrentHashMap(8);

    @Override
    public void intercept(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
        if (ms.getSqlCommandType() == SqlCommandType.SELECT && ms.getStatementType() == StatementType.CALLABLE) {
            //原sql
            String originalSql = boundSql.getSql();

            //业务逻辑,对原sql继续加工
            //.....

            //执行sql
            PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
            mpBoundSql.sql(originalSql);
        }

    }
}

mybatis-plus的配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(ObjectProvider<QueryInterceptor[]> provider){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        MyPaginationInnerInterceptor myPaginationInnerInterceptor = new MyPaginationInnerInterceptor();
        QueryInterceptor[] queryInterceptors = provider.getIfAvailable();
        if (queryInterceptors != null){
            AnnotationAwareOrderComparator.sort(queryInterceptors);
            myPaginationInnerInterceptor.setQueryInterceptors(queryInterceptors);
        }
        mybatisPlusInterceptor.addInnerInterceptor(myPaginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }


}