Search in sources :

Example 31 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project Mybatis-PageHelper by pagehelper.

the class PageInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    try {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        ResultHandler resultHandler = (ResultHandler) args[3];
        Executor executor = (Executor) invocation.getTarget();
        CacheKey cacheKey;
        BoundSql boundSql;
        //由于逻辑关系,只会进入一次
        if (args.length == 4) {
            //4 个参数时
            boundSql = ms.getBoundSql(parameter);
            cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
        } else {
            //6 个参数时
            cacheKey = (CacheKey) args[4];
            boundSql = (BoundSql) args[5];
        }
        List resultList;
        //调用方法判断是否需要进行分页,如果不需要,直接返回结果
        if (!dialect.skip(ms, parameter, rowBounds)) {
            //反射获取动态参数
            Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql);
            //判断是否需要进行 count 查询
            if (dialect.beforeCount(ms, parameter, rowBounds)) {
                //创建 count 查询的缓存 key
                CacheKey countKey = executor.createCacheKey(ms, parameter, RowBounds.DEFAULT, boundSql);
                countKey.update(MSUtils.COUNT);
                MappedStatement countMs = msCountMap.get(countKey);
                if (countMs == null) {
                    //根据当前的 ms 创建一个返回值为 Long 类型的 ms
                    countMs = MSUtils.newCountMappedStatement(ms);
                    msCountMap.put(countKey, countMs);
                }
                //调用方言获取 count sql
                String countSql = dialect.getCountSql(ms, boundSql, parameter, rowBounds, countKey);
                BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
                //当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中
                for (String key : additionalParameters.keySet()) {
                    countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
                }
                //执行 count 查询
                Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
                Long count = (Long) ((List) countResultList).get(0);
                //返回 true 时继续分页查询,false 时直接返回
                if (!dialect.afterCount(count, parameter, rowBounds)) {
                    //当查询总数为 0 时,直接返回空的结果
                    return dialect.afterPage(new ArrayList(), parameter, rowBounds);
                }
            }
            //判断是否需要进行分页查询
            if (dialect.beforePage(ms, parameter, rowBounds)) {
                //生成分页的缓存 key
                CacheKey pageKey = cacheKey;
                //处理参数对象
                parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
                //调用方言获取分页 sql
                String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
                BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter);
                //设置动态参数
                for (String key : additionalParameters.keySet()) {
                    pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
                }
                //执行分页查询
                resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);
            } else {
                //不执行分页的情况下,也不执行内存分页
                resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
            }
        } else {
            //rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页
            resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
        }
        return dialect.afterPage(resultList, parameter, rowBounds);
    } finally {
        dialect.afterAll();
    }
}
Also used : ArrayList(java.util.ArrayList) RowBounds(org.apache.ibatis.session.RowBounds) ResultHandler(org.apache.ibatis.session.ResultHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) ArrayList(java.util.ArrayList) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) Map(java.util.Map) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 32 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project Mybatis-PageHelper by pagehelper.

the class QueryInterceptor method intercept.

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    MappedStatement ms = (MappedStatement) args[0];
    Object parameter = args[1];
    RowBounds rowBounds = (RowBounds) args[2];
    ResultHandler resultHandler = (ResultHandler) args[3];
    Executor executor = (Executor) invocation.getTarget();
    CacheKey cacheKey;
    BoundSql boundSql;
    //由于逻辑关系,只会进入一次
    if (args.length == 4) {
        //4 个参数时
        boundSql = ms.getBoundSql(parameter);
        cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
    } else {
        //6 个参数时
        cacheKey = (CacheKey) args[4];
        boundSql = (BoundSql) args[5];
    }
    //注:下面的方法可以根据自己的逻辑调用多次,在分页插件中,count 和 page 各调用了一次
    return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
}
Also used : Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 33 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project Mybatis-PageHelper by pagehelper.

the class MSUtils method newCountMappedStatement.

/**
     * 新建count查询和分页查询的MappedStatement
     *
     * @param ms
     * @return
     */
public static MappedStatement newCountMappedStatement(MappedStatement ms) {
    MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId() + COUNT, ms.getSqlSource(), ms.getSqlCommandType());
    builder.resource(ms.getResource());
    builder.fetchSize(ms.getFetchSize());
    builder.statementType(ms.getStatementType());
    builder.keyGenerator(ms.getKeyGenerator());
    if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
        StringBuilder keyProperties = new StringBuilder();
        for (String keyProperty : ms.getKeyProperties()) {
            keyProperties.append(keyProperty).append(",");
        }
        keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
        builder.keyProperty(keyProperties.toString());
    }
    builder.timeout(ms.getTimeout());
    builder.parameterMap(ms.getParameterMap());
    //count查询返回值int
    List<ResultMap> resultMaps = new ArrayList<ResultMap>();
    ResultMap resultMap = new ResultMap.Builder(ms.getConfiguration(), ms.getId(), Long.class, EMPTY_RESULTMAPPING).build();
    resultMaps.add(resultMap);
    builder.resultMaps(resultMaps);
    builder.resultSetType(ms.getResultSetType());
    builder.cache(ms.getCache());
    builder.flushCacheRequired(ms.isFlushCacheRequired());
    builder.useCache(ms.isUseCache());
    return builder.build();
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) ArrayList(java.util.ArrayList) MappedStatement(org.apache.ibatis.mapping.MappedStatement)

Example 34 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project camel by apache.

the class MyBatisProducer method doProcessResult.

private void doProcessResult(Exchange exchange, Object result, SqlSession session) {
    final String outputHeader = getEndpoint().getOutputHeader();
    Message answer = exchange.getIn();
    if (ExchangeHelper.isOutCapable(exchange)) {
        answer = exchange.getOut();
        // preserve headers
        answer.getHeaders().putAll(exchange.getIn().getHeaders());
        if (outputHeader != null) {
            //if we put the MyBatis result into a header we should preserve the body as well
            answer.setBody(exchange.getIn().getBody());
        }
    }
    if (endpoint.getStatementType() == StatementType.SelectList || endpoint.getStatementType() == StatementType.SelectOne) {
        // we should not set the body if its a stored procedure as the result is already in its OUT parameter
        MappedStatement ms = session.getConfiguration().getMappedStatement(statement);
        if (ms != null && ms.getStatementType() == org.apache.ibatis.mapping.StatementType.CALLABLE) {
            if (result == null) {
                LOG.trace("Setting result as existing body as MyBatis statement type is Callable, and there was no result.");
                answer.setBody(exchange.getIn().getBody());
            } else {
                if (outputHeader != null) {
                    // set the result as header for insert
                    LOG.trace("Setting result as header [{}]: {}", outputHeader, result);
                    answer.setHeader(outputHeader, result);
                } else {
                    // set the result as body for insert
                    LOG.trace("Setting result as body: {}", result);
                    answer.setBody(result);
                    answer.setHeader(MyBatisConstants.MYBATIS_RESULT, result);
                }
            }
        } else {
            if (outputHeader != null) {
                LOG.trace("Setting result as header [{}]: {}", outputHeader, result);
                answer.setHeader(outputHeader, result);
            } else {
                // set the result as body for insert
                LOG.trace("Setting result as body: {}", result);
                answer.setBody(result);
                answer.setHeader(MyBatisConstants.MYBATIS_RESULT, result);
            }
        }
    } else {
        final String headerName = (outputHeader != null) ? outputHeader : MyBatisConstants.MYBATIS_RESULT;
        answer.setHeader(headerName, result);
    }
    answer.setHeader(MyBatisConstants.MYBATIS_STATEMENT_NAME, statement);
}
Also used : Message(org.apache.camel.Message) MappedStatement(org.apache.ibatis.mapping.MappedStatement)

Example 35 with MappedStatement

use of org.apache.ibatis.mapping.MappedStatement in project pinpoint by naver.

the class BindingLogPlugin32 method bindingLog.

private void bindingLog(Invocation invocation) throws SQLException {
    Object[] args = invocation.getArgs();
    MappedStatement ms = (MappedStatement) args[0];
    Object parameterObject = args[1];
    StatementType statementType = ms.getStatementType();
    if (StatementType.PREPARED == statementType || StatementType.CALLABLE == statementType) {
        Log statementLog = ms.getStatementLog();
        if (isDebugEnable(statementLog)) {
            BoundSql boundSql = ms.getBoundSql(parameterObject);
            String sql = boundSql.getSql();
            List<String> parameterList = getParameters(ms, parameterObject, boundSql);
            debug(statementLog, "==> BindingLog: " + bindLogFormatter.format(sql, parameterList));
        }
    }
}
Also used : Log(org.apache.ibatis.logging.Log) BoundSql(org.apache.ibatis.mapping.BoundSql) StatementType(org.apache.ibatis.mapping.StatementType) MappedStatement(org.apache.ibatis.mapping.MappedStatement)

Aggregations

MappedStatement (org.apache.ibatis.mapping.MappedStatement)69 Test (org.junit.Test)27 Author (org.apache.ibatis.domain.blog.Author)19 BaseDataTest (org.apache.ibatis.BaseDataTest)18 JdbcTransaction (org.apache.ibatis.transaction.jdbc.JdbcTransaction)18 StaticSqlSource (org.apache.ibatis.builder.StaticSqlSource)17 TypeHandlerRegistry (org.apache.ibatis.type.TypeHandlerRegistry)17 ArrayList (java.util.ArrayList)15 Configuration (org.apache.ibatis.session.Configuration)15 ResultMap (org.apache.ibatis.mapping.ResultMap)12 Statement (java.sql.Statement)10 StatementHandler (org.apache.ibatis.executor.statement.StatementHandler)9 ParameterMap (org.apache.ibatis.mapping.ParameterMap)9 Section (org.apache.ibatis.domain.blog.Section)8 BoundSql (org.apache.ibatis.mapping.BoundSql)8 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)8 ResultMapping (org.apache.ibatis.mapping.ResultMapping)7 SqlSource (org.apache.ibatis.mapping.SqlSource)7 RowBounds (org.apache.ibatis.session.RowBounds)7 Post (org.apache.ibatis.domain.blog.Post)6