Mybatis源碼學(xué)習(xí)記錄(SqlSession篇)

前言

本文銜接上文(Mapper接口篇)慰技,繼續(xù)分析Mapper相關(guān)的源碼

進(jìn)入源碼

上文中我們分析到了MapperMethod的execute方法

MapperMethod.java

public Object execute(SqlSession sqlSession, Object[] args) {
    Object result;
    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()) {
          executeWithResultHandler(sqlSession, args);
          result = null;
        } else if (method.returnsMany()) {
          // 根據(jù)上文示例糟红,代碼會(huì)執(zhí)行到這里
          result = executeForMany(sqlSession, args);
        } else if (method.returnsMap()) {
          result = executeForMap(sqlSession, args);
        } else if (method.returnsCursor()) {
          result = executeForCursor(sqlSession, args);
        } else {
          Object param = method.convertArgsToSqlCommandParam(args);
          result = sqlSession.selectOne(command.getName(), param);
        }
        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;
  }

根據(jù)上文示例陈肛,我們的代碼會(huì)執(zhí)行到executeForMany(sqlSession, args);這個(gè)分支句旱,下面分析這個(gè)方法做了什么工作

private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
    List<E> result;
    // 1. 首先將傳入的Object[] args轉(zhuǎn)為上文分析的合適的參數(shù)
    Object param = method.convertArgsToSqlCommandParam(args);
    if (method.hasRowBounds()) {
      // 1.1. 該調(diào)用方法有行限制參數(shù)RowBounds
      RowBounds rowBounds = method.extractRowBounds(args);
      result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
    } else {
      // 1.2. 無行限制參數(shù)RowBounds
      result = sqlSession.<E>selectList(command.getName(), param);
    }
    // 對于返回值為Collections和Array的支持,因?yàn)镾qlSession接口定義的返回值全部都是List<T>,如果我們的Mapper接口中定義了Array類型的返回值夹厌,就需要進(jìn)行轉(zhuǎn)換
    // issue #510 Collections & arrays support
    if (!method.getReturnType().isAssignableFrom(result.getClass())) {
      if (method.getReturnType().isArray()) {
        return convertToArray(result);
      } else {
        return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
      }
    }
    return result;
  }
  1. method.convertArgsToSqlCommandParam(args);

這里在真正調(diào)用SqlSession的相關(guān)方法前會(huì)執(zhí)行MethodSignature的convertArgsToSqlCommandParam(Object[] args)方法

public Object convertArgsToSqlCommandParam(Object[] args) {
      return paramNameResolver.getNamedParams(args);
}

就是調(diào)用了內(nèi)部持有的paramNameResolver對象的getNamedParams方法或南,將從代理Mapper對象方法調(diào)用拿到的Object[] args參數(shù)轉(zhuǎn)為合適的名稱參數(shù)對象以便接下來的sqlSession相關(guān)方法執(zhí)行使用

接下來,我們將目光轉(zhuǎn)向Mybatis中大名鼎鼎的Sqlsession

SqlSession.java

Mybatis暴露的頂層Api接口,用于操作Sql的執(zhí)行冀瓦,以屏蔽掉底層JDBC操作數(shù)據(jù)庫的繁瑣,讓我們可以直接調(diào)用其申明的各種方便的方法如,查詢/新增/更新/刪除/提交事務(wù)/回滾事務(wù)/獲取Mapper代理對象等

下面看下SqlSession的接口定義

public interface SqlSession extends Closeable {

    // 查詢單個(gè)結(jié)果
    <T> T selectOne(String statement);
    <T> T selectOne(String statement, Object parameter);
    
    // 查詢List<E>結(jié)果撑毛,可指定RowBounds參數(shù)
    <E> List<E> selectList(String statement);
    <E> List<E> selectList(String statement, Object parameter);
    <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
    
    // 返回Map的查詢方式
    <K, V> Map<K, V> selectMap(String statement, String mapKey);
    <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);
    <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
    
    // 返回懶處理迭代器Cursor游標(biāo)的查詢雌续,注意這種方式的查詢非常適合與數(shù)百萬數(shù)據(jù)項(xiàng)的查詢(不會(huì)直接加載進(jìn)內(nèi)存中)
    <T> Cursor<T> selectCursor(String statement);
    <T> Cursor<T> selectCursor(String statement, Object parameter);
    <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);
    
    // 指定resultHandler參數(shù)的查詢
    void select(String statement, Object parameter, ResultHandler handler);
    void select(String statement, ResultHandler handler);
    void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);
    
    // insert相關(guān)
    int insert(String statement);
    int insert(String statement, Object parameter);
    
    // update相關(guān)
    int update(String statement);
    int update(String statement, Object parameter);
    
    // delete相關(guān)
    int delete(String statement);
    int delete(String statement, Object parameter);
    
    // 事務(wù)相關(guān),force參數(shù)代表是否強(qiáng)制提交/回滾事務(wù)
    void commit();
    void commit(boolean force);
    void rollback();
    void rollback(boolean force);
    
    // 檢索當(dāng)前Mybatis配置對象
    Configuration getConfiguration();

    // 檢索一個(gè)綁定到當(dāng)前SqlSession對象的type類型的Mapper代理對象
    <T> T getMapper(Class<T> type);

    // 檢錯(cuò)內(nèi)部持有的數(shù)據(jù)庫連接對象
    Connection getConnection();
}

接口終歸是接口胯杭,活還是得有人來干驯杜,下面看看SqlSession的實(shí)現(xiàn)類有哪些

DefaultSqlSession.java

Mybatis 默認(rèn)的SqlSession實(shí)現(xiàn)類,實(shí)現(xiàn)了接口中定義的所有功能

接口定義

public class DefaultSqlSession implements SqlSession {...}

可以看到這里DefaultSqlSession僅實(shí)現(xiàn)了SqlSession接口

內(nèi)部屬性定義

  // 重要屬性做个,持有Mybatis Configuration實(shí)例
  private final Configuration configuration;
  // 持有執(zhí)行器Executor鸽心,后面會(huì)詳細(xì)分析
  private final Executor executor;

  // 是否自動(dòng)提交事務(wù)
  private final boolean autoCommit;
  // 是否已過期,執(zhí)行update操作就會(huì)置為true
  private boolean dirty;
  // 當(dāng)查詢數(shù)以百萬計(jì)的數(shù)據(jù)時(shí)用到
  private List<Cursor<?>> cursorList;

每個(gè)SqlSession實(shí)例內(nèi)部都持有一個(gè)Mybatis的配置實(shí)例以及一個(gè)執(zhí)行器Executor實(shí)例,且在其構(gòu)造函數(shù)中就需要初始化這兩個(gè)屬性了

構(gòu)造函數(shù)

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
    this.configuration = configuration;
    this.executor = executor;
    this.dirty = false;
    this.autoCommit = autoCommit;
  }

  public DefaultSqlSession(Configuration configuration, Executor executor) {
    this(configuration, executor, false);
  }

接下來我們繼續(xù)上面的查詢例子sqlSession.<E>selectList(command.getName(), param);

這里的command.getName的值是"com.wilson.mapper.UserOrderMapper.completedOrdersByUserPhone"這個(gè)字符串居暖,而param則是一個(gè)Map對象顽频,其值為{{"phone", "15800000000"}, {"param1", "15800000000"}}

ok,接下來我們直接看DefaultSqlSession的selectList方法做了什么

  @Override
  public <E> List<E> selectList(String statement, Object parameter) {
    // 內(nèi)部調(diào)用重載的帶有RowBounds參數(shù)的方法膝但,傳入默認(rèn)的RowBounds冲九,即offset為0,limit為Integer的最大值
    return this.selectList(statement, parameter, RowBounds.DEFAULT);
  }

  @Override
  public <E> List<E> selectList(String statement, Object 
  parameter, RowBounds rowBounds) {
    try {
      // 1.  
      MappedStatement ms = configuration.getMappedStatement(statement);
      // 2. 
      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();
    }
  }

重點(diǎn):

  1. 首先從configuration實(shí)例中獲取到MappedStatement實(shí)例
  2. 調(diào)用執(zhí)行器executorquery方法跟束,并傳入1. 中得到的MappedStatement實(shí)例作為參數(shù)

以上兩步我們分開分析莺奸,首先看第一步如何從配置對象configuration中獲取到MappedStatement實(shí)例

根據(jù)statementId獲取MappedStatement實(shí)例

// Configuration.java
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");

public MappedStatement getMappedStatement(String id) {
  // 根據(jù)傳入的statementId獲取
  return this.getMappedStatement(id, true);
}

public MappedStatement getMappedStatement(String id, boolean validateIncompleteStatements) {
  if (validateIncompleteStatements) {
    buildAllStatements();
  }
  // 就是從mappedStatements取出已經(jīng)存入的MappedStatement實(shí)例
  return mappedStatements.get(id);
}

總結(jié)

至此本文內(nèi)容分析結(jié)束,SqlSession真正的SQL查詢交給了Executor執(zhí)行器冀宴,前文不是說過SqlSession是Mybatis提供的頂層API嘛灭贷,通過SqlSession就可以操作數(shù)據(jù)庫的各種SQL操作了,這里怎么又出來一個(gè)執(zhí)行器呢略贮?實(shí)際上甚疟,Mybatis的SqlSession非常聰明,將各種SQL操作的臟活逃延、累活委托給了Executor執(zhí)行器干览妖,自己則坐享其成...

下文我們將分析Mybatis的Executor篇

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市揽祥,隨后出現(xiàn)的幾起案子讽膏,更是在濱河造成了極大的恐慌,老刑警劉巖拄丰,帶你破解...
    沈念sama閱讀 216,692評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件府树,死亡現(xiàn)場離奇詭異,居然都是意外死亡料按,警方通過查閱死者的電腦和手機(jī)奄侠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來载矿,“玉大人垄潮,你說我怎么就攤上這事。” “怎么了弯洗?”我有些...
    開封第一講書人閱讀 162,995評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵甫题,是天一觀的道長。 經(jīng)常有香客問我涂召,道長,這世上最難降的妖魔是什么敏沉? 我笑而不...
    開封第一講書人閱讀 58,223評(píng)論 1 292
  • 正文 為了忘掉前任果正,我火速辦了婚禮,結(jié)果婚禮上盟迟,老公的妹妹穿的比我還像新娘秋泳。我一直安慰自己,他們只是感情好攒菠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評(píng)論 6 388
  • 文/花漫 我一把揭開白布迫皱。 她就那樣靜靜地躺著,像睡著了一般辖众。 火紅的嫁衣襯著肌膚如雪卓起。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,208評(píng)論 1 299
  • 那天凹炸,我揣著相機(jī)與錄音戏阅,去河邊找鬼。 笑死啤它,一個(gè)胖子當(dāng)著我的面吹牛奕筐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播变骡,決...
    沈念sama閱讀 40,091評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼离赫,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了塌碌?” 一聲冷哼從身側(cè)響起渊胸,我...
    開封第一講書人閱讀 38,929評(píng)論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎誊爹,沒想到半個(gè)月后蹬刷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,346評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡频丘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評(píng)論 2 333
  • 正文 我和宋清朗相戀三年办成,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片搂漠。...
    茶點(diǎn)故事閱讀 39,739評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡迂卢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情而克,我是刑警寧澤靶壮,帶...
    沈念sama閱讀 35,437評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站员萍,受9級(jí)特大地震影響腾降,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜碎绎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評(píng)論 3 326
  • 文/蒙蒙 一螃壤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧筋帖,春花似錦奸晴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至代箭,卻和暖如春墩划,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嗡综。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評(píng)論 1 269
  • 我被黑心中介騙來泰國打工走诞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蛤高。 一個(gè)月前我還...
    沈念sama閱讀 47,760評(píng)論 2 369
  • 正文 我出身青樓蚣旱,卻偏偏與公主長得像袱蜡,于是被迫代替她去往敵國和親实牡。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容