Mybatis源码阅读

模块进度

  1. 解析器模块-complete
package org.apache.ibatis.parsing;

/**
 * @author Clinton Begin
 */
public class GenericTokenParser {

  private final String openToken;
  private final String closeToken;
  private final TokenHandler handler;

  public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
    this.openToken = openToken;
    this.closeToken = closeToken;
    this.handler = handler;
  }

  public String parse(String text) {
    if (text == null || text.isEmpty()) {
      return "";
    }
    // search open token
    int start = text.indexOf(openToken);
    if (start == -1) {
      return text;
    }
    char[] src = text.toCharArray();
    int offset = 0;
    final StringBuilder builder = new StringBuilder();
    StringBuilder expression = null;
    //循环是为了解决多个占位符的情况 eg:123${a}${b}
    while (start > -1) {
      if (start > 0 && src[start - 1] == '\\') {
        // this open token is escaped. remove the backslash and continue.
        //如果包含转义字符,那么不做处理,直接添加 [offset, start - offset - 1] 内容,然后加上openToken 比如123\\${1232, 那么这里build的内容就是123${
        builder.append(src, offset, start - offset - 1).append(openToken);
        offset = start + openToken.length();
      } else {
        // found open token. let's search close token.
        if (expression == null) {
          expression = new StringBuilder();
        } else {
          expression.setLength(0);
        }
        builder.append(src, offset, start - offset);
        offset = start + openToken.length();
        int end = text.indexOf(closeToken, offset);
        while (end > -1) {
          if (end > offset && src[end - 1] == '\\') {
            // this close token is escaped. remove the backslash and continue.
            expression.append(src, offset, end - offset - 1).append(closeToken);
            offset = end + closeToken.length();
            end = text.indexOf(closeToken, offset);
          } else {
            expression.append(src, offset, end - offset);
            break;
          }
        }
        if (end == -1) {
          // close token was not found.
          builder.append(src, start, src.length - start);
          offset = src.length;
        } else {
          builder.append(handler.handleToken(expression.toString()));
          offset = end + closeToken.length();
        }
      }
      start = text.indexOf(openToken, offset);
    }
    if (offset < src.length) {
      builder.append(src, offset, src.length - offset);
    }
    return builder.toString();
  }
}

个人觉得值得阅读的源码部分,这里陷入的一个误区,那就是如果${}前面加入的转义字符,那么最后获取到的结果里面也会有${},阅读的时候忘记这一点,读半天,一直不知道为何要把${}加入到内容中去。

  1. 反射模块-complete
  2. 异常模块-complete
  3. 数据源模块-complete
  4. 事务模块-complete
  5. 缓存模块-complete
    用到了装饰器模式
  6. 类型模块-complete
  7. io模块-complete
  8. 日志模块-complete
  9. 注解模块-complete
  10. Binding模块-complete

....

结语

看到binding模块后面的就没有怎么阅读了,主要是感觉收益甚微,后面就只看了一些核心部分了,这种挨着看的方式太过于浪费时间,也许是我的问题,这种方式不太适合我...

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

人生中没有四季 唯有那寒冬的荒野