筹备

在浏览源码前,须要先clone源码 地址:https://github.com/mybatis/my...

Mybatis框架应用大量常见的设计模式,学习Mybatis源码咱们次要学习以下几点:
  • 学习大佬们的编码思维及标准
  • 学习一些传承下来的设计模式
  • 实际java基础理论
带着问题浏览源码
  • 问题1:源码中用了哪些设计模式?为什么要用这些设计模式?
  • 问题2.Mybatis关上调试模式之后,能打印sql语句等信息,这是怎么实现的?实现过程中应用了什么设计模式?如果有多个日志实现,加载程序是什么?

带着这些个问题 咱们去源码中找寻答案
首先咱们先来看看源码包功能模块图

Mybatis框架提供了一个顶层接口SqlSession ,外围解决层,根底撑持层 这里应用了一个常见的设计模式:外观模式/门面模式
明天咱们次要解说根底撑持层日志模块外部是如何解决的.

源码-logging

关上源码我的项目,能够看到有20个目录,这里咱们先不深刻去看,把眼光聚焦到logging目录

关上目录,咱们能够看到映入眼帘的就是 Log LogException LogFactory (红色局部)和具体的日志实现(黄色局部),

关上Log接口,咱们能够看到Log接口提供了七个接口办法

实现这个接口的实现办法咱们看看都是谁呢,能够看到就是咱们刚刚黄色框框局部,那么他是怎么来解决那么多Log实现的呢,别急,持续往下看

咱们轻易关上一个Log的实现类,可一看到实际上这并不是真正的实现了所须要的Log只是在对每个用到的Log实现做了参数适配而已
(其余的类也是相似,这里应用了适配器模式)

那么有那么多日志实现,他的加载程序是什么呢.到这里咱们会想到刚刚看到的LogFactory类,大叫都晓得工厂办法就是用来获取实例的既然要获取实例那么他的解决也必定在工厂外部处理完毕了(这里应用了简略工厂模式) 那么咱们关上LogFactory类来看看他的加载程序到底是怎么实现的:

import java.lang.reflect.Constructor;public final class LogFactory {  public static final String MARKER = "MYBATIS";  private static Constructor<? extends Log> logConstructor; //定义logConstructor 他继承于Constructor  static {    //加载程序在这里.初始化时必然依照这个程序执行,能够发现上面这一排排就是咱们刚刚看到的各个适配器    tryImplementation(LogFactory::useSlf4jLogging);    tryImplementation(LogFactory::useCommonsLogging);    tryImplementation(LogFactory::useLog4J2Logging);    tryImplementation(LogFactory::useLog4JLogging);    tryImplementation(LogFactory::useJdkLogging);    tryImplementation(LogFactory::useNoLogging);  }  private LogFactory() {    // 在这里私有化结构器 就不能通过new来创立实例了  }  public static Log getLog(Class<?> clazz) {//第一步.如果咱们须要一个Log 那么咱们就调用这个办法,传入须要的类即可    return getLog(clazz.getName());  }  public static Log getLog(String logger) {//第二步.依据类的名字获取Log实现.如果获取不到会报错,这个logConstructor就是咱们下面的,他外部在初始化时曾经通过static的动态代码块进行了解决,所以会解决为对应的日志实现,    try {      return logConstructor.newInstance(logger);    } catch (Throwable t) {      throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);    }  }  public static synchronized void useCustomLogging(Class<? extends Log> clazz) {    setImplementation(clazz);  }  public static synchronized void useSlf4jLogging() {//每个动态同步办法都是去调用setImplementation    setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);  }  public static synchronized void useCommonsLogging() {    setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);  }  public static synchronized void useLog4JLogging() {    setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);  }  public static synchronized void useLog4J2Logging() {    setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);  }  public static synchronized void useJdkLogging() {    setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);  }  public static synchronized void useStdOutLogging() {    setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);  }  public static synchronized void useNoLogging() {    setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);  }  private static void tryImplementation(Runnable runnable) {//将传入的代码块执行,留神这里是.run 不是.start     if (logConstructor == null) {//如果还没有实现才执行      try {        runnable.run();      } catch (Throwable t) {//如果加载这个实现呈现了异样,如:ClassNotFoundException 这个时候间接疏忽,期待下一个实现调用        // ignore      }    }  }        //在获取到一个实现胜利后 会调用这个办法来初始化Log  private static void setImplementation(Class<? extends Log> implClass) {    try {      Constructor<? extends Log> candidate = implClass.getConstructor(String.class);      Log log = candidate.newInstance(LogFactory.class.getName());      if (log.isDebugEnabled()) {        log.debug("Logging initialized using '" + implClass + "' adapter.");      }      logConstructor = candidate;    } catch (Throwable t) {      throw new LogException("Error setting Log implementation.  Cause: " + t, t);    }  }}

回顾一下问题:源码中用了哪些设计模式?
到目前咱们看到Mybatis应用了 1.外观模式/门面模式 2.适配器模式 3简略工厂模式
到这里咱们曾经拿到了Log的实例不论他是通过log4j还是slf4j的实现,那他是怎么实现对日志进行加强的呢 怎么打印进去的SQL语句和参数以及后果计数的?
如果想要输入这些信息,那么首先必定要获取到这些信息.
上面咱们就去看这块,看看他是怎么获取和存贮这些信息的.

public abstract class BaseJdbcLogger {  protected static final Set<String> SET_METHODS;//set办法容器  protected static final Set<String> EXECUTE_METHODS = new HashSet<>();//执行办法容器  private final Map<Object, Object> columnMap = new HashMap<>();//列的键值对  private final List<Object> columnNames = new ArrayList<>();//所有key的列表  private final List<Object> columnValues = new ArrayList<>();//所有value的列表  protected final Log statementLog;  // Log实现  protected final int queryStack;  /*   * Default constructor   */  public BaseJdbcLogger(Log log, int queryStack) {//默认的结构    this.statementLog = log;    if (queryStack == 0) {      this.queryStack = 1;    } else {      this.queryStack = queryStack;    }  }  static {//初始化 就加载两个容器 这两个容器用来装所有的预编译set参数的办法和所有执行办法    SET_METHODS = Arrays.stream(PreparedStatement.class.getDeclaredMethods())//这里应用JAVA8新个性将PreparedStatement类的所有set结尾的办法映射成一个Set            .filter(method -> method.getName().startsWith("set"))            .filter(method -> method.getParameterCount() > 1)            .map(Method::getName)            .collect(Collectors.toSet());    EXECUTE_METHODS.add("execute");//将4个执行办法增加到EXECUTE_METHODS这个Set    EXECUTE_METHODS.add("executeUpdate");    EXECUTE_METHODS.add("executeQuery");    EXECUTE_METHODS.add("addBatch");  }  protected void setColumn(Object key, Object value) {    columnMap.put(key, value);    columnNames.add(key);    columnValues.add(value);  }  protected Object getColumn(Object key) {    return columnMap.get(key);  }  protected String getParameterValueString() {    List<Object> typeList = new ArrayList<>(columnValues.size());    for (Object value : columnValues) {      if (value == null) {        typeList.add("null");      } else {        typeList.add(objectValueString(value) + "(" + value.getClass().getSimpleName() + ")");      }    }    final String parameters = typeList.toString();    return parameters.substring(1, parameters.length() - 1);  }  protected String objectValueString(Object value) {    if (value instanceof Array) {      try {        return ArrayUtil.toString(((Array) value).getArray());      } catch (SQLException e) {        return value.toString();      }    }    return value.toString();  }  protected String getColumnString() {    return columnNames.toString();  }  protected void clearColumnInfo() {    columnMap.clear();    columnNames.clear();    columnValues.clear();  }  protected String removeExtraWhitespace(String original) {    return SqlSourceBuilder.removeExtraWhitespaces(original);  }  protected boolean isDebugEnabled() {    return statementLog.isDebugEnabled();  }  protected boolean isTraceEnabled() {    return statementLog.isTraceEnabled();  }  protected void debug(String text, boolean input) {    if (statementLog.isDebugEnabled()) {      statementLog.debug(prefix(input) + text);    }  }  protected void trace(String text, boolean input) {    if (statementLog.isTraceEnabled()) {      statementLog.trace(prefix(input) + text);    }  }  private String prefix(boolean isInput) {    char[] buffer = new char[queryStack * 2 + 2];    Arrays.fill(buffer, '=');    buffer[queryStack * 2 + 1] = ' ';    if (isInput) {      buffer[queryStack * 2] = '>';    } else {      buffer[0] = '<';    }    return new String(buffer);  }}

到这咱们晓得了他是怎么存贮信息的获取的形式就是在调用时通过存贮的办法和set办法列表来查问,那么他是什么时候被调用呢?认真看看这个图咱们发现了端倪 在BaseJdbcLogger上面还有

  • ConnectionLogger
  • PreparedStatementLogger
  • ResultSetLogger
  • StatementLogger
    这些不就是咱们很眼生的流程吗 获取数据库连贯--> 预编译SQL--> 执行SQL--> 获取后果

    咱们轻易关上一个 看看他们是怎么在本人的一亩三分地干活的
public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler {//继承于BaseJdbcLogger 实现InvocationHandler 看到InvocationHandler 就会想到->代理模式  private final Connection connection;//他实要代理的就是这connection  private ConnectionLogger(Connection conn, Log statementLog, int queryStack) {    super(statementLog, queryStack);    this.connection = conn;  }  @Override  public Object invoke(Object proxy, Method method, Object[] params)      throws Throwable {    try {      if (Object.class.equals(method.getDeclaringClass())) {        return method.invoke(this, params);      }      if ("prepareStatement".equals(method.getName()) || "prepareCall".equals(method.getName())) {//判断办法名是不是这两中的一个 短语或 有一个就为true        if (isDebugEnabled()) {//原来开启debug 就会打出日志是在这里呀          debug(" Preparing: " + removeExtraWhitespace((String) params[0]), true);        }        PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);// 通过connection创立了PreparedStatement        stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack); //把stmt 传过来给PreparedStatementLogger的newInstance 创立了一个stmt的代理返回        return stmt;//留神这里返回的不是实在stmt 而是代理过后的stmt      } else if ("createStatement".equals(method.getName())) {//如果是创立Statement        Statement stmt = (Statement) method.invoke(connection, params); //拿到连贯去创立一个Statement        stmt = StatementLogger.newInstance(stmt, statementLog, queryStack);//把Statement传进StatementLogger 获取Statement的代理对象来返回        return stmt;//留神这里返回的不是实在stmt 而是代理过后的stmt      } else {        return method.invoke(connection, params);      }    } catch (Throwable t) {      throw ExceptionUtil.unwrapThrowable(t);    }  }  /**   * Creates a logging version of a connection.   *   * @param conn   *          the original connection   * @param statementLog   *          the statement log   * @param queryStack   *          the query stack   * @return the connection with logging   */  public static Connection newInstance(Connection conn, Log statementLog, int queryStack) {    InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack);    ClassLoader cl = Connection.class.getClassLoader();    return (Connection) Proxy.newProxyInstance(cl, new Class[]{Connection.class}, handler);  }  /**   * return the wrapped connection.   *   * @return the connection   */  public Connection getConnection() {    return connection;  }
public final class PreparedStatementLogger extends BaseJdbcLogger implements InvocationHandler {  private final PreparedStatement statement; //还是一样 这里是被代理的理论对象  private PreparedStatementLogger(PreparedStatement stmt, Log statementLog, int queryStack) {    super(statementLog, queryStack);    this.statement = stmt;  }  @Override  public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {//加强的逻辑在这里    try {      if (Object.class.equals(method.getDeclaringClass())) {        return method.invoke(this, params);      }      if (EXECUTE_METHODS.contains(method.getName())) {//还记得那几个容器吗 用来存储各种办法的  当初他来了        //开启DEBUG 我就把参数打印进去        if (isDebugEnabled()) {          debug("Parameters: " + getParameterValueString(), true);        }        clearColumnInfo();        if ("executeQuery".equals(method.getName())) {          ResultSet rs = (ResultSet) method.invoke(statement, params);//获取到一个resultset          return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);//把resultset 也代理了来返回        } else {          return method.invoke(statement, params);        }      } else if (SET_METHODS.contains(method.getName())) {        if ("setNull".equals(method.getName())) {          setColumn(params[0], null);        } else {          setColumn(params[0], params[1]);        }        return method.invoke(statement, params);      } else if ("getResultSet".equals(method.getName())) {        ResultSet rs = (ResultSet) method.invoke(statement, params);        return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);      } else if ("getUpdateCount".equals(method.getName())) {        int updateCount = (Integer) method.invoke(statement, params);        if (updateCount != -1) {          debug("   Updates: " + updateCount, false);        }        return updateCount;      } else {        return method.invoke(statement, params);      }    } catch (Throwable t) {      throw ExceptionUtil.unwrapThrowable(t);    }  }  /**   * Creates a logging version of a PreparedStatement.   *   * @param stmt - the statement   * @param statementLog - the statement log   * @param queryStack - the query stack   * @return - the proxy   */  public static PreparedStatement newInstance(PreparedStatement stmt, Log statementLog, int queryStack) {    InvocationHandler handler = new PreparedStatementLogger(stmt, statementLog, queryStack);    ClassLoader cl = PreparedStatement.class.getClassLoader();    return (PreparedStatement) Proxy.newProxyInstance(cl, new Class[]{PreparedStatement.class, CallableStatement.class}, handler);  }  /**   * Return the wrapped prepared statement.   *   * @return the PreparedStatement   */  public PreparedStatement getPreparedStatement() {    return statement;  }}

最初是ResultSetLogger

  @Override  public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {//ResultSetLogger的加强办法    try {      if (Object.class.equals(method.getDeclaringClass())) {        return method.invoke(this, params);      }      Object o = method.invoke(rs, params);      if ("next".equals(method.getName())) {         if ((Boolean) o) {          rows++;//只有还有下一条next,就记数加1  rows++;          if (isTraceEnabled()) {            ResultSetMetaData rsmd = rs.getMetaData();            final int columnCount = rsmd.getColumnCount();            if (first) {              first = false;              printColumnHeaders(rsmd, columnCount);            }            printColumnValues(columnCount);          }        } else {          debug("     Total: " + rows, false);//开启debug状况下 打印出总数计数        }      }      clearColumnInfo();      return o;    } catch (Throwable t) {      throw ExceptionUtil.unwrapThrowable(t);    }  }

到这里咱们根本曾经摸完了这块逻辑 当初复盘一下问题

  • 问题1.源码中用了哪些设计模式?为什么要用这些设计模式?
    门面/外观模式 适配器模式 代理模式 简略工厂模式. 为什么要应用这些模式 太长了我就不写了,大家自行总结
  • 问题2.Mybatis关上调试模式之后,能打印sql语句等信息,这是怎么实现的?实现过程中应用了什么设计模式?如果有多个日志实现,加载程序是什么?
    在调试模式能打印日志,是mybatis框架在理论执行时对对象进行了代理,进行了加强.他应用了代理设计模式.他实现日志的程序是:
  static {    tryImplementation(LogFactory::useSlf4jLogging);    tryImplementation(LogFactory::useCommonsLogging);    tryImplementation(LogFactory::useLog4J2Logging);    tryImplementation(LogFactory::useLog4JLogging);    tryImplementation(LogFactory::useJdkLogging);    tryImplementation(LogFactory::useNoLogging);  }

加群让咱们一起学习吧

关注公众号:java宝典