Search in sources :

Example 1 with InnerInterceptor

use of com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor in project solon by noear.

the class MybatisPlusInterceptorTest method setProperties.

@Test
void setProperties() {
    Properties properties = new Properties();
    properties.setProperty("@page", "com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor");
    properties.setProperty("page:maxLimit", "10");
    properties.setProperty("page:dbType", "h2");
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.setProperties(properties);
    List<InnerInterceptor> interceptors = interceptor.getInterceptors();
    assertThat(interceptors).isNotEmpty();
    assertThat(interceptors.size()).isEqualTo(1);
    InnerInterceptor page = interceptors.get(0);
    assertThat(page).isInstanceOf(PaginationInnerInterceptor.class);
    PaginationInnerInterceptor pii = (PaginationInnerInterceptor) page;
    assertThat(pii.getMaxLimit()).isEqualTo(10);
    assertThat(pii.getDbType()).isEqualTo(DbType.H2);
}
Also used : PaginationInnerInterceptor(com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor) Properties(java.util.Properties) PaginationInnerInterceptor(com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor) InnerInterceptor(com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor) Test(org.junit.jupiter.api.Test)

Example 2 with InnerInterceptor

use of com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor in project solon by noear.

the class MybatisPlusInterceptor method setProperties.

/**
 * 使用内部规则,拿分页插件举个栗子:
 * <p>
 * - key: "@page" ,value: "com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"
 * - key: "page:limit" ,value: "100"
 * <p>
 * 解读1: key 以 "@" 开头定义了这是一个需要组装的 `InnerInterceptor`, 以 "page" 结尾表示别名
 * value 是 `InnerInterceptor` 的具体的 class 全名
 * 解读2: key 以上面定义的 "别名 + ':'" 开头指这个 `value` 是定义的该 `InnerInterceptor` 属性需要设置的值
 * <p>
 * 如果这个 `InnerInterceptor` 不需要配置属性也要加别名
 */
@Override
public void setProperties(Properties properties) {
    PropertyMapper pm = PropertyMapper.newInstance(properties);
    Map<String, Properties> group = pm.group(StringPool.AT);
    group.forEach((k, v) -> {
        InnerInterceptor innerInterceptor = ClassUtils.newInstance(k);
        innerInterceptor.setProperties(v);
        addInnerInterceptor(innerInterceptor);
    });
}
Also used : PropertyMapper(com.baomidou.mybatisplus.solon.toolkit.PropertyMapper) InnerInterceptor(com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor)

Example 3 with InnerInterceptor

use of com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor in project solon by noear.

the class MybatisPlusInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object target = invocation.getTarget();
    Object[] args = invocation.getArgs();
    if (target instanceof Executor) {
        final Executor executor = (Executor) target;
        Object parameter = args[1];
        boolean isUpdate = args.length == 2;
        MappedStatement ms = (MappedStatement) args[0];
        if (!isUpdate && ms.getSqlCommandType() == SqlCommandType.SELECT) {
            RowBounds rowBounds = (RowBounds) args[2];
            ResultHandler resultHandler = (ResultHandler) args[3];
            BoundSql boundSql;
            if (args.length == 4) {
                boundSql = ms.getBoundSql(parameter);
            } else {
                // 几乎不可能走进这里面,除非使用Executor的代理对象调用query[args[6]]
                boundSql = (BoundSql) args[5];
            }
            for (InnerInterceptor query : interceptors) {
                if (!query.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql)) {
                    return Collections.emptyList();
                }
                query.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
            }
            CacheKey cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
            return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
        } else if (isUpdate) {
            for (InnerInterceptor update : interceptors) {
                if (!update.willDoUpdate(executor, ms, parameter)) {
                    return -1;
                }
                update.beforeUpdate(executor, ms, parameter);
            }
        }
    } else {
        // StatementHandler
        final StatementHandler sh = (StatementHandler) target;
        // 目前只有StatementHandler.getBoundSql方法args才为null
        if (null == args) {
            for (InnerInterceptor innerInterceptor : interceptors) {
                innerInterceptor.beforeGetBoundSql(sh);
            }
        } else {
            Connection connections = (Connection) args[0];
            Integer transactionTimeout = (Integer) args[1];
            for (InnerInterceptor innerInterceptor : interceptors) {
                innerInterceptor.beforePrepare(sh, connections, transactionTimeout);
            }
        }
    }
    return invocation.proceed();
}
Also used : Connection(java.sql.Connection) RowBounds(org.apache.ibatis.session.RowBounds) ResultHandler(org.apache.ibatis.session.ResultHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) StatementHandler(org.apache.ibatis.executor.statement.StatementHandler) MappedStatement(org.apache.ibatis.mapping.MappedStatement) InnerInterceptor(com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor) CacheKey(org.apache.ibatis.cache.CacheKey)

Aggregations

InnerInterceptor (com.baomidou.mybatisplus.solon.plugins.inner.InnerInterceptor)3 PaginationInnerInterceptor (com.baomidou.mybatisplus.solon.plugins.inner.PaginationInnerInterceptor)1 PropertyMapper (com.baomidou.mybatisplus.solon.toolkit.PropertyMapper)1 Connection (java.sql.Connection)1 Properties (java.util.Properties)1 CacheKey (org.apache.ibatis.cache.CacheKey)1 Executor (org.apache.ibatis.executor.Executor)1 StatementHandler (org.apache.ibatis.executor.statement.StatementHandler)1 BoundSql (org.apache.ibatis.mapping.BoundSql)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1 ResultHandler (org.apache.ibatis.session.ResultHandler)1 RowBounds (org.apache.ibatis.session.RowBounds)1 Test (org.junit.jupiter.api.Test)1