主頁 > 後端開發 > 深入Mybatis原始碼——執行流程

深入Mybatis原始碼——執行流程

2020-10-02 16:12:57 後端開發

前言

上一篇分析Mybatis是如何加載決議XML檔案的,本篇緊接上文,分析Mybatis的剩余兩個階段:代理封裝SQL執行

正文

代理封裝

Mybatis有兩種方式呼叫Mapper介面:

private static SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);

// 第一種
try (SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE)) {
  Blog blog = session.selectOne("org.apache.ibatis.domain.blog.mappers.BlogMapper.selectBlogWithPostsUsingSubSelect", 1);
}

// 第二種
try (SqlSession session = sqlMapper.openSession()) {
  AuthorMapper mapper = session.getMapper(AuthorMapper.class);
  Author author = mapper.selectAuthor(101);
}

從上面代碼可以看到無論是哪一種首先都要創建SqlSessionFactory物件,然后通過這個物件拿到SqlSession物件,在早期版本中只能通過該物件的增刪改呼叫Mapper介面,很明顯這種方式可讀性很差,難以維護,寫起來也復雜,所以后面谷歌開始維護Mybatis后,重新封裝提供了第二種方式直接呼叫Mapper介面,不過本質上第二種是在第一種的基礎之上實作的,所以下面就以第二種為主進行分析,進入到getMapper方法:

  public <T> T getMapper(Class<T> type) {
    return configuration.<T>getMapper(type, this);
  }

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

mapperRegistry物件在上一篇分析過,是在決議xml中的mapper節點時注冊進去的,而這個物件中快取了Mapper介面和對應的代理工廠的映射,所以getMapper的核心就是通過這個工廠去創建代理物件:

  public T newInstance(SqlSession sqlSession) {
	 //每次呼叫都會創建新的MapperProxy物件
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

然后通過Mapper介面呼叫時首先就會呼叫到MapperProxyinvoke方法:

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      if (Object.class.equals(method.getDeclaringClass())) {//如果是Object本身的方法不增強
        return method.invoke(this, args);
      } else if (isDefaultMethod(method)) {
        return invokeDefaultMethod(proxy, method, args);
      }
    } catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
    //從快取中獲取mapperMethod物件,如果快取中沒有,則創建一個,并添加到快取中
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    //呼叫execute方法執行sql
    return mapperMethod.execute(sqlSession, args);
  }

  private MapperMethod cachedMapperMethod(Method method) {
    return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration()));
  }

首先從快取中拿到MapperMethod物件,這個物件封裝了SQL陳述句的型別、命名空間、入參、回傳型別等資訊,然后通過它的execute方法呼叫SqlSession的增刪查改方法:

  public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    //根據sql陳述句型別以及介面回傳的引數選擇呼叫不同的
    switch (command.getType()) {
      case INSERT: {
    	Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.insert(command.getName(), param));
        break;
      }
      case UPDATE: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.update(command.getName(), param));
        break;
      }
      case DELETE: {
        Object param = method.convertArgsToSqlCommandParam(args);
        result = rowCountResult(sqlSession.delete(command.getName(), param));
        break;
      }
      case SELECT:
        if (method.returnsVoid() && method.hasResultHandler()) {//回傳值為void
          executeWithResultHandler(sqlSession, args);
          result = null;
        } else if (method.returnsMany()) {//回傳值為集合或者陣列
          result = executeForMany(sqlSession, args);
        } else if (method.returnsMap()) {//回傳值為map
          result = executeForMap(sqlSession, args);
        } else if (method.returnsCursor()) {//回傳值為游標
          result = executeForCursor(sqlSession, args);
        } else {//處理回傳為單一物件的情況
          //通過引數決議器決議決議引數
          Object param = method.convertArgsToSqlCommandParam(args);
          result = sqlSession.selectOne(command.getName(), param);
          if (method.returnsOptional() &&
              (result == null || !method.getReturnType().equals(result.getClass()))) {
            result = OptionalUtil.ofNullable(result);
          }
        }
        break;
      case FLUSH:
        result = sqlSession.flushStatements();
        break;
      default:
        throw new BindingException("Unknown execution method for: " + command.getName());
    }
    if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
      throw new BindingException("Mapper method '" + command.getName() 
          + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
    }
    return result;
  }

上文說過SqlSession本質上是門面模式的體現,其本質上是通過Executor執行器組件實作的,在該組件中定義了所有訪問資料庫的方法:

  public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
    try {
      //從configuration中獲取要執行的sql陳述句的配置資訊
      MappedStatement ms = configuration.getMappedStatement(statement);
      //通過executor執行陳述句,并回傳指定的結果集
      return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

Executor物件是在獲取SqlSession時創建的:

  public SqlSession openSession() {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }

  private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
    	//獲取mybatis組態檔中的environment物件
      final Environment environment = configuration.getEnvironment();
      //從environment獲取transactionFactory物件
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      //創建事務物件
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //根據配置創建executor
      final Executor executor = configuration.newExecutor(tx, execType);
      //創建DefaultSqlSession
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

TransactionFactory是我們在xml中配置的transactionManager屬性,可選的屬性有JDBC和Managed,然后根據我們的配置創建事務物件,之后才是創建Executor物件,

  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);
    }
    //如果有<cache>節點,通過裝飾器,添加二級快取的能力
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    //通過interceptorChain遍歷所有的插件為executor增強,添加插件的功能
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

Executor有三個基本的實作類:

  • BatchExecutor:批處理執行器,執行批量更新、插入等操作,
  • ReuseExecutor:可重用執行器,快取并重用Statement(Statement、PreparedStatement、CallableStatement),
  • SimpleExecutor:默認使用的執行器,每次執行都會創建 新的Statement

這三個執行器都繼承了自抽象的BaseExecutor,同時如果開啟了二級快取功能,在這里還會裝飾一個CachingExecutor為其添加二級快取的能力,另外還要注意在這段代碼的最后還有攔截器進行了包裝,也就是擴展插件的實作 ,關于這部分內容在一篇進行分析,

SQL執行

二級快取的代碼很簡單,這里直接略過,所以直接進入到BaseExecutor.query方法:

  public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {
	//獲取sql陳述句資訊,包括占位符,引數等資訊
    BoundSql boundSql = ms.getBoundSql(parameter);
    //拼裝快取的key值
    CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);
    return query(ms, parameter, rowBounds, resultHandler, key, boundSql);
 }

  public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
    if (closed) {//檢查當前executor是否關閉
      throw new ExecutorException("Executor was closed.");
    }
    if (queryStack == 0 && ms.isFlushCacheRequired()) {//非嵌套查詢,并且FlushCache配置為true,則需要清空一級快取
      clearLocalCache();
    }
    List<E> list;
    try {
      queryStack++;//查詢層次加一
      list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;//查詢以及快取
      if (list != null) {
    	 //針對呼叫存盤程序的結果處理
        handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
      } else {
    	 //快取未命中,從資料庫加載資料
        list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
      }
    } finally {
      queryStack--;
    }
    
    
    if (queryStack == 0) {
      for (DeferredLoad deferredLoad : deferredLoads) {//延遲加載處理
        deferredLoad.load();
      }
      // issue #601
      deferredLoads.clear();
      if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {//如果當前sql的一級快取配置為STATEMENT,查詢完既清空一集快取
        // issue #482
        clearLocalCache();
      }
    }
    return list;
  }

首先從一級快取localCache里面拿,如果沒有,才真正地訪問資料庫,并將回傳結果存入到一級快取中,

  private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    List<E> list;
    localCache.putObject(key, EXECUTION_PLACEHOLDER);//在快取中添加占位符
    try {
      //呼叫抽象方法doQuery,方法查詢資料庫并回傳結果,可選的實作包括:simple、reuse、batch
      list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    } finally {
      localCache.removeObject(key);//在快取中洗掉占位符
    }
    localCache.putObject(key, list);//將真正的結果物件添加到一級快取
    if (ms.getStatementType() == StatementType.CALLABLE) {//如果是呼叫存盤程序
      localOutputParameterCache.putObject(key, parameter);//快取輸出型別結果引數
    }
    return list;
  }

這里的doQuery是子類實作的,即模板模式,以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(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
      //StatementHandler物件創建stmt,并使用parameterHandler對占位符進行處理
      stmt = prepareStatement(handler, ms.getStatementLog());
      //通過statementHandler物件呼叫ResultSetHandler將結果集轉化為指定物件回傳
      return handler.<E>query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }

通讀這里的代碼我們可以發現,Executor本身是不會訪問到資料庫,而是作為指揮官,指揮三個小弟干事:

  • StatementHandler:創建PreparedStatementStatementCallableStatement物件,
  • ParameterHandler:在StatementHandler建構式中創建,對預編譯的 SQL 陳述句進行引數設定,
  • ResultSetHandler:在StatementHandler建構式中創建,對資料庫回傳的結果集(ResultSet)進行封裝,回傳用戶指定的物體型別,

上面三個物件都是在configuration.newStatementHandler方法中創建的,然后呼叫prepareStatement拿到合適的Statement,如果是預編譯的還會進行引數設定:

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    //獲取connection物件的動態代理,添加日志能力;
    Connection connection = getConnection(statementLog);
    //通過不同的StatementHandler,利用connection創建(prepare)Statement
    stmt = handler.prepare(connection, transaction.getTimeout());
    //使用parameterHandler處理占位符
    handler.parameterize(stmt);
    return stmt;
  }

如果在DEBUG模式下拿到的Connection物件是ConnectionLogger,這就和第一篇的內容串聯起來了,之后再通過query方法呼叫execute執行SQL陳述句,并使用ResultSetHandler處理結果集:

  public List<Object> handleResultSets(Statement stmt) throws SQLException {
    ErrorContext.instance().activity("handling results").object(mappedStatement.getId());
    //用于保存結果集物件
    final List<Object> multipleResults = new ArrayList<>();

    int resultSetCount = 0;
    //statment可能回傳多個結果集物件,這里先取出第一個結果集
    ResultSetWrapper rsw = getFirstResultSet(stmt);
    //獲取結果集對應resultMap,本質就是獲取欄位與java屬性的映射規則
    List<ResultMap> resultMaps = mappedStatement.getResultMaps();
    int resultMapCount = resultMaps.size();
    validateResultMapsCount(rsw, resultMapCount);//結果集和resultMap不能為空,為空拋出例外
    while (rsw != null && resultMapCount > resultSetCount) {
     //獲取當前結果集對應的resultMap
      ResultMap resultMap = resultMaps.get(resultSetCount);
      //根據映射規則(resultMap)對結果集進行轉化,轉換成目標物件以后放入multipleResults中
      handleResultSet(rsw, resultMap, multipleResults, null);
      rsw = getNextResultSet(stmt);//獲取下一個結果集
      cleanUpAfterHandlingResultSet();//清空nestedResultObjects物件
      resultSetCount++;
    }
    //獲取多結果集,多結果集一般出現在存盤程序的執行,存盤程序回傳多個resultset,
    //mappedStatement.resultSets屬性列出多個結果集的名稱,用逗號分割;
    //多結果集的處理不是重點,暫時不分析
    String[] resultSets = mappedStatement.getResultSets();
    if (resultSets != null) {
      while (rsw != null && resultSetCount < resultSets.length) {
        ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);
        if (parentMapping != null) {
          String nestedResultMapId = parentMapping.getNestedResultMapId();
          ResultMap resultMap = configuration.getResultMap(nestedResultMapId);
          handleResultSet(rsw, resultMap, null, parentMapping);
        }
        rsw = getNextResultSet(stmt);
        cleanUpAfterHandlingResultSet();
        resultSetCount++;
      }
    }

    return collapseSingleResultList(multipleResults);
  }

這里最終就是通過反射模塊以及Configuration類中的result相關配置進行結果映射:

  private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
    try {
      if (parentMapping != null) {//處理多結果集的嵌套映射
        handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
      } else {
        if (resultHandler == null) {//如果resultHandler為空,實體化一個人默認的resultHandler
          DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
          //對ResultSet進行映射,映射結果暫存在resultHandler中
          handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
          //將暫存在resultHandler中的映射結果,填充到multipleResults
          multipleResults.add(defaultResultHandler.getResultList());
        } else {
          //使用指定的rusultHandler進行轉換
          handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
        }
      }
    } finally {
      // issue #228 (close resultsets)
      //呼叫resultset.close()關閉結果集
      closeResultSet(rsw.getResultSet());
    }
  }

  public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
    if (resultMap.hasNestedResultMaps()) {//處理有嵌套resultmap的情況
      ensureNoRowBounds();
      checkResultHandler();
      handleRowValuesForNestedResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    } else {//處理沒有嵌套resultmap的情況
      handleRowValuesForSimpleResultMap(rsw, resultMap, resultHandler, rowBounds, parentMapping);
    }
  }

  private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
      throws SQLException {
	//創建結果背景關系,所謂的背景關系就是專門在回圈中快取結果物件的
    DefaultResultContext<Object> resultContext = new DefaultResultContext<>();
    //1.根據分頁資訊,定位到指定的記錄
    skipRows(rsw.getResultSet(), rowBounds);
    //2.shouldProcessMoreRows判斷是否需要映射后續的結果,實際還是翻頁處理,避免超過limit
    while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
      //3.進一步完善resultMap資訊,主要是處理鑒別器的資訊
      ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
      //4.讀取resultSet中的一行記錄并進行映射,轉化并回傳目標物件
      Object rowValue = https://www.cnblogs.com/yewy/p/getRowValue(rsw, discriminatedResultMap);
      //5.保存映射結果物件
      storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());
    }
  }

  private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap) throws SQLException {
    final ResultLoaderMap lazyLoader = new ResultLoaderMap();
    //4.1 根據resultMap的type屬性,實體化目標物件
    Object rowValue = createResultObject(rsw, resultMap, lazyLoader, null);
    if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
      //4.2 對目標物件進行封裝得到metaObjcect,為后續的賦值操作做好準備
      final MetaObject metaObject = configuration.newMetaObject(rowValue);
      boolean foundValues = this.useConstructorMappings;//取得是否使用建構式初始化屬性值
      if (shouldApplyAutomaticMappings(resultMap, false)) {//是否使用自動映射
    	 //4.3一般情況下 autoMappingBehavior默認值為PARTIAL,對未明確指定映射規則的欄位進行自動映射
        foundValues = applyAutomaticMappings(rsw, resultMap, metaObject, null) || foundValues;
      }
       //4.4 映射resultMap中明確指定需要映射的列
      foundValues = applyPropertyMappings(rsw, resultMap, metaObject, lazyLoader, null) || foundValues;
      foundValues = lazyLoader.size() > 0 || foundValues;
      //4.5 如果沒有一個映射成功的屬性,則根據的配置回傳null或者結果物件
      rowValue = foundValues || configuration.isReturnInstanceForEmptyRow() ? rowValue : null;
    }
    return rowValue;
  }
  • 自動映射
  private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
	//獲取resultSet中存在的,但是ResultMap中沒有明確映射的列,填充至autoMapping中
    List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);
    boolean foundValues = false;
    if (!autoMapping.isEmpty()) {
      //遍歷autoMapping,通過自動匹配的方式為屬性復制
      for (UnMappedColumnAutoMapping mapping : autoMapping) {
    	//通過typeHandler從resultset中拿值
        final Object value = https://www.cnblogs.com/yewy/p/mapping.typeHandler.getResult(rsw.getResultSet(), mapping.column);
        if (value != null) {
          foundValues = true;
        }
        if (value != null || (configuration.isCallSettersOnNulls() && !mapping.primitive)) {
          // gcode issue #377, call setter on nulls (value is not'found')
          //通過metaObject給屬性賦值
          metaObject.setValue(mapping.property, value);
        }
      }
    }
    return foundValues;
  }
  • 指定映射
  private boolean applyPropertyMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, ResultLoaderMap lazyLoader, String columnPrefix)
      throws SQLException {
	//從resultMap中獲取明確需要轉換的列名集合
    final List<String> mappedColumnNames = rsw.getMappedColumnNames(resultMap, columnPrefix);
    boolean foundValues = false;
    //獲取ResultMapping集合
    final List<ResultMapping> propertyMappings = resultMap.getPropertyResultMappings();
    for (ResultMapping propertyMapping : propertyMappings) {
      String column = prependPrefix(propertyMapping.getColumn(), columnPrefix);//獲得列名,注意前綴的處理
      if (propertyMapping.getNestedResultMapId() != null) {
        // the user added a column attribute to a nested result map, ignore it
    	//如果屬性通過另外一個resultMap映射,則忽略
        column = null;
      }
      if (propertyMapping.isCompositeResult()//如果是嵌套查詢,column={prop1=col1,prop2=col2}
          || (column != null && mappedColumnNames.contains(column.toUpperCase(Locale.ENGLISH)))//基本型別映射
          || propertyMapping.getResultSet() != null) {//嵌套查詢的結果
    	//獲得屬性值
        Object value = https://www.cnblogs.com/yewy/p/getPropertyMappingValue(rsw.getResultSet(), metaObject, propertyMapping, lazyLoader, columnPrefix);
        // issue #541 make property optional
        //獲得屬性名稱
        final String property = propertyMapping.getProperty();
        if (property == null) {//屬性名為空跳出回圈
          continue;
        } else if (value == DEFERED) {//屬性名為DEFERED,延遲加載的處理
          foundValues = true;
          continue;
        }
        if (value != null) {
          foundValues = true;
        }
        if (value != null || (configuration.isCallSettersOnNulls() && !metaObject.getSetterType(property).isPrimitive())) {
          // gcode issue #377, call setter on nulls (value is not'found')
          //通過metaObject為目標物件設定屬性值
          metaObject.setValue(property, value);
        }
      }
    }
    return foundValues;
  }

反射實體化物件的代碼比較長,但邏輯都比較清晰,上面的關鍵流程代碼也都加上了注釋,讀者可自行參照原始碼閱讀,

總結

Mybatis核心原理就分析完了,相比較Spring原始碼簡單了很多,但代碼的優雅度和優秀的設計思想一點也不亞于Spring,也是非常值得我們好好學習掌握的,不過這3篇只是分析了Mybaits的核心執行原理,另外還有插件怎么擴展、攔截器會攔截哪些方法以及Mybatis和Spring的整合又是怎么實作的呢?讀者們可以好好思考下,答案將在下一篇揭曉,

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/148748.html

標籤:Java

上一篇:SpringBoot實作國際化i18n功能

下一篇:Java集合框架-概述

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more