1. 程式人生 > >mybatis原始碼分析三之SqlSession

mybatis原始碼分析三之SqlSession

SqlSession

  1. SqlSession是一個會話,相當於jdbc的Connection物件,生命週期應該是請求資料庫處理事務的過程中。它是非執行緒安全的,在每次建立SqlSession都必須及時關閉它,它長期存在就會使資料庫連線池的活動資源減少。

SqlSession下的四大物件

  1. Mapper執行的過程是通過Executor、StatementHandler、ParameterHandler和ResultHandler來完成資料庫操作和結果返回的
    • Executor表示執行器,排程相關的handler來執行對應的sql(StatementHandler、ParameterHandler、ResultHandler)
    • StatementHandler的作用是使用資料庫的Statement(PreparedStatement)執行操作
    • ParameterHandler用於SQL對引數的處理
    • ResultHandler是進行最後資料集(ResultSet)的封裝返回處理的

Executor

  1. Executor是一個真正執行java和資料庫互動的東西
    • SIMPLE
    • REUSE
    • BATCH
  2. 建立

    在DefaultSqlSessionFactory中建立DefaultSqlSession之前建立Executor(通過Configuration建立),以下是Configuration中建立Executor的程式碼。根據配置型別去建立三種執行器中的一種,在建立之後將Executor放到interceptorChain之中
     public
    Executor newExecutor(Transaction transaction, ExecutorType executorType) { executorType = executorType == null ? defaultExecutorType : executorType; executorType = executorType == null ? ExecutorType.SIMPLE : executorType; Executor executor; if (ExecutorType.BATCH == executorType) { executor = new
    BatchExecutor(this, transaction); } else if (ExecutorType.REUSE == executorType) { executor = new ReuseExecutor(this, transaction); } else { executor = new SimpleExecutor(this, transaction); } if (cacheEnabled) { executor = new CachingExecutor(executor); } executor = (Executor) interceptorChain.pluginAll(executor); return executor; }
  3. SimpleExecutor

     public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        Statement stmt = null;
        try {
          Configuration configuration = ms.getConfiguration();
          //通過Configuration建立StatementHandler
          StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, rowBounds, resultHandler, boundSql);
          //預編譯sql,並對引數進行初始化操作
          stmt = prepareStatement(handler, ms.getStatementLog());
          //resultHandler 組裝返回結果
          return handler.<E>query(stmt, resultHandler);
        } finally {
          closeStatement(stmt);
        }
      }
    
      private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
        Statement stmt;
        Connection connection = getConnection(statementLog);
        stmt = handler.prepare(connection);
        //設定引數並執行(預編譯sql)
        handler.parameterize(stmt);
        return stmt;
      }
    

StatementHandler

  1. 建立

        /***
        在Executor建立StatementHandler,StatementHandler是在Configuration中建立,程式碼如下
        **/
          public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
            /**
            RoutingStatementHandler不是真實的服務物件,而是通過介面卡找到對應的StatementHandler(物件的介面卡模式)
                SimpleStatementHandler
                PreparedStatementHandler
                CallableStatementHandler
            */
            StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
            statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
            return statementHandler;
          }
    

  2. SimpleStatementHandler

    
          public <E> List<E> query(Statement statement, ResultHandler resultHandler)
              throws SQLException {
            String sql = boundSql.getSql();
            statement.execute(sql);
            //使用ResultSetHandler封裝結果返回給呼叫者
            return resultSetHandler.<E>handleResultSets(statement);
          }   
  3. PreparedStatementHandler

      public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
        //sql編譯已經在之前都做好了,所以這裡直接執行就可以了
        PreparedStatement ps = (PreparedStatement) statement;
        ps.execute();
        return resultSetHandler.<E> handleResultSets(ps);
      }
    
        public void parameterize(Statement statement) throws SQLException {
        KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
        ErrorContext.instance().store();
        keyGenerator.processBefore(executor, mappedStatement, statement, boundSql.getParameterObject());
        ErrorContext.instance().recall();
        rebindGeneratedKey();
        parameterHandler.setParameters((PreparedStatement) statement);
      }

paramsHandler

  1. 程式碼

    public interface ParameterHandler {
     //返回引數物件   
      Object getParameterObject();
     //設定預編譯sql語句的引數
      void setParameters(PreparedStatement ps)
          throws SQLException;
    
    }
    mybatis提供一個預設的DefaultParameterHandler
    public void setParameters(PreparedStatement ps)
          throws SQLException {
        ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId());
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        if (parameterMappings != null) {
          //從parameterObject物件中獲取引數,然後使用typeHandler進行引數處理
          MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
          for (int i = 0; i < parameterMappings.size(); i++) {
            ParameterMapping parameterMapping = parameterMappings.get(i);
            if (parameterMapping.getMode() != ParameterMode.OUT) {
              Object value;
              String propertyName = parameterMapping.getProperty();
              PropertyTokenizer prop = new PropertyTokenizer(propertyName);
              if (parameterObject == null) {
                value = null;
              } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                value = parameterObject;
              } else if (boundSql.hasAdditionalParameter(propertyName)) {
                value = boundSql.getAdditionalParameter(propertyName);
              } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)
                  && boundSql.hasAdditionalParameter(prop.getName())) {
                value = boundSql.getAdditionalParameter(prop.getName());
                if (value != null) {
                  value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length()));
                }
              } else {
                value = metaObject == null ? null : metaObject.getValue(propertyName);
              }
              TypeHandler typeHandler = parameterMapping.getTypeHandler();
              if (typeHandler == null) {
                throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId());
              }
              JdbcType jdbcType = parameterMapping.getJdbcType();
              if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();
              typeHandler.setParameter(ps, i + 1, value, jdbcType);
            }
          }
        }
      }
    

resultSetHandler

  1. 結果集處理器

     public interface ResultSetHandler {
     //包裝結果集
      <E> List<E> handleResultSets(Statement stmt) throws SQLException;
       //處理儲存過程輸出引數的
      void handleOutputParameters(CallableStatement cs) throws SQLException;
    
    }