mybatis 源碼分析select語句

一、測試select語句

項目的啓動的時候已經開始解析mapper的xml文件

測試select語句:

<select id="listInfo" resultType="com.example.demo.domain.TestStudent">
    select * from test_student
    <where>
       <if test="id != ''">
           id > 0
       </if>
    </where>
</select>

二、斷點調試

調用select時,前面有很多步跳到這裏: 

@Override
public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
  BoundSql boundSql = ms.getBoundSql(parameterObject);
  CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
  return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
}
  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);
    context.getBindings().forEach(boundSql::setAdditionalParameter);
    return boundSql;
  }
public static Object getValue(String expression, Object root) {
  try {
    Map context = Ognl.createDefaultContext(root, MEMBER_ACCESS, CLASS_RESOLVER, null);
    return Ognl.getValue(parseExpression(expression), context, root);
  } catch (OgnlException e) {
    throw new BuilderException("Error evaluating expression '" + expression + "'. Cause: " + e, e);
  }
}

id != '' 這裏方法轉變爲 id != ""

private static Object parseExpression(String expression) throws OgnlException {
  Object node = expressionCache.get(expression);
  if (node == null) {
    node = Ognl.parseExpression(expression);
    expressionCache.put(expression, node);
  }
  return node;
}

public static Object getValue(Object tree, Map context, Object root) throws OgnlException {
    return getValue((Object)tree, (Map)context, root, (Class)null);
}
public static Object getValue(Object tree, Map context, Object root, Class resultType) throws OgnlException {
    OgnlContext ognlContext = (OgnlContext)addDefaultContext(root, context);
    Node node = (Node)tree;
    Object result;
    if (node.getAccessor() != null) {
        result = node.getAccessor().get(ognlContext, root);
    } else {
        result = node.getValue(ognlContext, root);
    }

    if (resultType != null) {
        result = getTypeConverter(context).convertValue(context, root, (Member)null, (String)null, result, resultType);
    }

    return result;
}
public final Object getValue(OgnlContext context, Object source) throws OgnlException {
    Object result = null;
    if (context.getTraceEvaluations()) {
        EvaluationPool pool = OgnlRuntime.getEvaluationPool();
        Throwable evalException = null;
        Evaluation evaluation = pool.create(this, source);
        context.pushEvaluation(evaluation);
        boolean var13 = false;

        try {
            var13 = true;
            result = this.evaluateGetValueBody(context, source);
            var13 = false;
        } catch (OgnlException var14) {
            evalException = var14;
            throw var14;
        } catch (RuntimeException var15) {
            evalException = var15;
            throw var15;
        } finally {
            if (var13) {
                Evaluation eval = context.popEvaluation();
                eval.setResult(result);
                if (evalException != null) {
                    eval.setException((Throwable)evalException);
                }

                if (evalException == null && context.getRootEvaluation() == null && !context.getKeepLastEvaluation()) {
                    pool.recycleAll(eval);
                }

            }
        }

        Evaluation eval = context.popEvaluation();
        eval.setResult(result);
        if (evalException != null) {
            eval.setException((Throwable)evalException);
        }

        if (evalException == null && context.getRootEvaluation() == null && !context.getKeepLastEvaluation()) {
            pool.recycleAll(eval);
        }
    } else {
        result = this.evaluateGetValueBody(context, source);
    }

    return result;
}

protected Object evaluateGetValueBody(OgnlContext context, Object source) throws OgnlException {
    context.setCurrentObject(source);
    context.setCurrentNode(this);
    if (!this._constantValueCalculated) {
        this._constantValueCalculated = true;
        boolean constant = this.isConstant(context);
        if (constant) {
            this._constantValue = this.getValueBody(context, source);
        }

        this._hasConstantValue = constant;
    }

    return this._hasConstantValue ? this._constantValue : this.getValueBody(context, source);
}
protected Object getValueBody(OgnlContext context, Object source) throws OgnlException {
    Object v1 = this._children[0].getValue(context, source);
    Object v2 = this._children[1].getValue(context, source);
    return OgnlOps.equal(v1, v2) ? Boolean.FALSE : Boolean.TRUE;
}

where語句拼接完後回到這個方法:

public SqlSource parse(String originalSql, Class<?> parameterType, Map<String, Object> additionalParameters) {
  ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(configuration, parameterType, additionalParameters);
  GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
  String sql;
  if (configuration.isShrinkWhitespacesInSql()) {
    sql = parser.parse(removeExtraWhitespaces(originalSql));
  } else {
    sql = parser.parse(originalSql);
  }
  return new StaticSqlSource(configuration, sql, handler.getParameterMappings());
}

解析不多,深夜了

此文結束

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章