Search in sources :

Example 36 with BoundSql

use of org.apache.ibatis.mapping.BoundSql 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)

Example 37 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project mybatis-3 by mybatis.

the class DynamicSqlSource method getBoundSql.

@Override
public BoundSql getBoundSql(Object parameterObject) {
    DynamicContext context = new DynamicContext(configuration, parameterObject);
    rootSqlNode.apply(context);
    SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
    Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
    SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
        boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
    }
    return boundSql;
}
Also used : SqlSource(org.apache.ibatis.mapping.SqlSource) BoundSql(org.apache.ibatis.mapping.BoundSql) SqlSourceBuilder(org.apache.ibatis.builder.SqlSourceBuilder) Map(java.util.Map)

Example 38 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project mybatis.flying by limeng32.

the class EnhancedCachingInterceptor method processQuery.

/**
 * when executing a query operation 1. record this statement's id and it's
 * corresponding Cache Object into Global Caching Manager; 2. record this
 * statement's id and
 *
 * @param invocation
 * @return
 * @throws Throwable
 */
protected Object processQuery(Invocation invocation) throws Throwable {
    Object result = invocation.proceed();
    if (cachingManager.isCacheEnabled()) {
        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement) args[0];
        // 如果本条statementId表示的查询语句配置了 flushCache=true,则清空querCacheOnCommit缓存
        if (mappedStatement.isFlushCacheRequired()) {
            queryCacheOnCommit.clear();
        }
        // 和对应的二级缓存对象映射关系添加到全局缓存映射管理器中
        if (mappedStatement.isUseCache() && mappedStatement.getCache() != null) {
            cachingManager.appendStatementCacheMap(mappedStatement.getId(), mappedStatement.getCache());
        }
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        Executor executor = (Executor) invocation.getTarget();
        BoundSql boundSql = mappedStatement.getBoundSql(parameter);
        FlyingModel flyingModel = CookOriginalSql.fetchFlyingFeature(boundSql.getSql());
        // 记录本次查询所产生的CacheKey
        CacheKey cacheKey = createCacheKey(mappedStatement, parameter, rowBounds, boundSql, executor);
        if (flyingModel.isHasFlyingFeature()) {
            switch(flyingModel.getActionType()) {
                case count:
                    cacheKey.update(DigestUtils.md5Hex(JSON.toJSONString(parameter)));
                    break;
                case selectAll:
                    cacheKey.update(DigestUtils.md5Hex(JSON.toJSONString(parameter)));
                    break;
                case selectOne:
                    cacheKey.update(DigestUtils.md5Hex(JSON.toJSONString(parameter)));
                    break;
                default:
                    break;
            }
        }
        queryCacheOnCommit.putElement(mappedStatement.getId(), cacheKey);
        /* 处理分页 */
        ResultHandler resultHandler = (ResultHandler) args[3];
        Executor executorProxy = (Executor) invocation.getTarget();
        MetaObject metaParameter = MetaObject.forObject(parameter, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
        MetaObject metaExecutor = MetaObject.forObject(executorProxy, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
        /* 当需要分页查询时,缓存里加入page信息 */
        // if (metaParameter.getOriginalObject() instanceof Conditionable) {
        Cache cache = mappedStatement.getCache();
        Object value = cache.getObject(cacheKey);
        if (metaExecutor.hasGetter("delegate")) {
            TransactionalCacheManager tcm = (TransactionalCacheManager) metaExecutor.getValue("tcm");
            Executor delegate = (Executor) metaExecutor.getValue("delegate");
            Object list = delegate.query(mappedStatement, parameter, rowBounds, resultHandler, cacheKey, boundSql);
            if (value != null) {
                return value;
            } else {
                tcm.putObject(cache, cacheKey, list);
                return list;
            }
        }
    // }
    }
    return result;
}
Also used : FlyingModel(indi.mybatis.flying.models.FlyingModel) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) MetaObject(org.apache.ibatis.reflection.MetaObject) RowBounds(org.apache.ibatis.session.RowBounds) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) TransactionalCacheManager(org.apache.ibatis.cache.TransactionalCacheManager) CacheKey(org.apache.ibatis.cache.CacheKey) Cache(org.apache.ibatis.cache.Cache)

Example 39 with BoundSql

use of org.apache.ibatis.mapping.BoundSql in project paascloud-master by paascloud.

the class SqlLogInterceptor method intercept.

/**
 * Intercept object.
 *
 * @param invocation the invocation
 *
 * @return the object
 *
 * @throws Throwable the throwable
 */
@Override
public Object intercept(Invocation invocation) throws Throwable {
    long start = System.currentTimeMillis();
    MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
    Object parameter = null;
    if (invocation.getArgs().length > 1) {
        parameter = invocation.getArgs()[1];
    }
    BoundSql boundSql = mappedStatement.getBoundSql(parameter);
    Configuration configuration = mappedStatement.getConfiguration();
    String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
    List<String> paramList = getParamList(configuration, boundSql);
    Object proceed = invocation.proceed();
    int result = 0;
    if (proceed instanceof ArrayList) {
        ArrayList resultList = (ArrayList) proceed;
        result = resultList.size();
    }
    if (proceed instanceof Integer) {
        result = (Integer) proceed;
    }
    if (enableSqlLogInterceptor) {
        long end = System.currentTimeMillis();
        long time = end - start;
        Boolean flag = (Boolean) ThreadLocalMap.get(NotDisplaySqlAspect.DISPLAY_SQL);
        if (time >= noticeTime * GlobalConstant.Number.THOUSAND_INT) {
            log.error("执行超过{}秒,sql={}", noticeTime, sql);
            log.error("result={}, time={}ms, params={}", result, time, paramList);
            return proceed;
        }
        if (flag == null || Objects.equals(flag, true)) {
            log.info("sql={}", sql);
            log.info("result={},time={}ms, params={}", result, time, paramList);
        }
    }
    return proceed;
}
Also used : Configuration(org.apache.ibatis.session.Configuration) BoundSql(org.apache.ibatis.mapping.BoundSql) MetaObject(org.apache.ibatis.reflection.MetaObject) MappedStatement(org.apache.ibatis.mapping.MappedStatement)

Aggregations

BoundSql (org.apache.ibatis.mapping.BoundSql)39 Test (org.junit.Test)24 BaseDataTest (org.apache.ibatis.BaseDataTest)23 DynamicSqlSource (org.apache.ibatis.scripting.xmltags.DynamicSqlSource)23 TextSqlNode (org.apache.ibatis.scripting.xmltags.TextSqlNode)23 Configuration (org.apache.ibatis.session.Configuration)18 IfSqlNode (org.apache.ibatis.scripting.xmltags.IfSqlNode)17 MappedStatement (org.apache.ibatis.mapping.MappedStatement)12 WhereSqlNode (org.apache.ibatis.scripting.xmltags.WhereSqlNode)10 CacheKey (org.apache.ibatis.cache.CacheKey)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Executor (org.apache.ibatis.executor.Executor)5 MetaObject (org.apache.ibatis.reflection.MetaObject)5 RowBounds (org.apache.ibatis.session.RowBounds)5 ResultHandler (org.apache.ibatis.session.ResultHandler)4 Connection (java.sql.Connection)3 Map (java.util.Map)3 SqlSource (org.apache.ibatis.mapping.SqlSource)3 ChooseSqlNode (org.apache.ibatis.scripting.xmltags.ChooseSqlNode)3