日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

Executor 執(zhí)行器

今天分享一下 Executor。它在框架中是具體sql的執(zhí)行器,sqlSession(門面模式)封裝通用的api,把具體操作委派給 Executor 執(zhí)行,Executor協(xié)同BoundSql,StatementHandler,ParameterHandler 和 ResultSetHandler 完成工作。

它使用裝飾器的方式組織 Executor 對象。如 CachingExecutor 裝飾了SimpleExecutor 提供二級緩存功能。

可以通過插件機(jī)制擴(kuò)展功能。MyBatisplus 就是通過插件機(jī)制擴(kuò)展的功能。

下面是更新流程,Executor 處于流程中間藍(lán)色部分,緩存執(zhí)行器,基礎(chǔ)執(zhí)行器,簡單執(zhí)行器三個(gè) Executor 通過責(zé)任鏈的方式組織起來,各司其職,一同完成執(zhí)行工作。,可以感受到它的作用是承上啟下。

來,全搞懂,原來Mybatis執(zhí)行一個(gè)sql有這么多類型,絕

 

執(zhí)行器介紹

來,全搞懂,原來Mybatis執(zhí)行一個(gè)sql有這么多類型,絕

 

Mybatis 一共提供了四種執(zhí)行器的實(shí)現(xiàn)和一個(gè)模板類:

  • 基礎(chǔ)執(zhí)行器 BaseExecutor:實(shí)現(xiàn)Executor接口的抽象類,實(shí)現(xiàn)了框架邏輯,具體的邏輯委派給子類實(shí)現(xiàn)。一級緩存也是在這里實(shí)現(xiàn)的。
  • 緩存執(zhí)行器 CachingExecutor:實(shí)現(xiàn)了二級緩存,是jvm級別的全局緩存。
  • 簡單執(zhí)行器 SimpleExecutor:繼承自 BaseExecutor,具體執(zhí)行邏輯的實(shí)現(xiàn)。
  • 重用執(zhí)行器 ReuseExecutor:相同的 sql 只會預(yù)編譯一次。
  • 批處理執(zhí)行器 BatchExecutor:批處理執(zhí)行器 使用 JDBC 的batch API 執(zhí)行 SQL 的批量操作,如insert 或者 update。select的邏輯和 SimpleExecutor 的實(shí)現(xiàn)一樣。

今天介紹 SimpleExecutor,ReuseExecutor 和 BatchExecutor 三個(gè)執(zhí)行器的特定和邏輯, CachingExecutor 的功能是提供二級緩存,暫時(shí)不在這里介紹。

SimpleExecutor

簡單執(zhí)行器顧名思義,處理的邏輯比較簡單直接,來一個(gè) sql 預(yù)編譯一個(gè),處理一個(gè)。 示例代碼如下:

// 創(chuàng)建 SimpleExecutor 
SimpleExecutor simpleExecutor = new SimpleExecutor(sessionFactory.getConfiguration(),
jdbcTransaction);
// 獲取 MAppedStatement 
final MappedStatement ms = sessionFactory.getConfiguration().getMappedStatement("example.mapper.UserMapper.getUserByID");
final BoundSql boundSql = ms.getBoundSql(1);
// 執(zhí)行 2 次查詢
simpleExecutor.doQuery(ms, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, boundSql);
simpleExecutor.doQuery(ms, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER, boundSql);

執(zhí)行結(jié)果:


[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: select * from `user` where id = ? 
[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: select * from `user` where id = ? 
[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

通過日志看到,雖然執(zhí)行相同的 sql 但是每次都要執(zhí)行預(yù)編譯。這是一個(gè)需要優(yōu)化的點(diǎn)。

ReuseExecutor

ReuseExecutor 對相同 SQL 重復(fù)編譯做了優(yōu)化,相同的 sql 的 Statement 只創(chuàng)建一個(gè)。

示例代碼上面一樣,只是把 SimpleExecutor 換成 ReuseExecutor 。 從執(zhí)行我們看到,Preparing 只有一次,執(zhí)行結(jié)果也是正確的:

[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: select * from `user` where id = ? 
[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

[DEBUG][main] m.p.ThresholdInterceptor.intercept ThresholdInterceptor plugin... 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: 1(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug <==      Total: 1 

他是怎么做到的呢?翻開代碼看看實(shí)現(xiàn),其實(shí)邏輯也很簡單,用 SQL 當(dāng)作 key 保存對應(yīng)的 Statement 來實(shí)現(xiàn)重用。

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    BoundSql boundSql = handler.getBoundSql();
    String sql = boundSql.getSql();
    // 關(guān)鍵邏輯,通過 sql 判斷是否已經(jīng)創(chuàng)建了 Statement,如果有則重用。
    if (hasStatementFor(sql)) {
      stmt = getStatement(sql);
      applyTransactionTimeout(stmt);
    } else {
      Connection connection = getConnection(statementLog);
      stmt = handler.prepare(connection, transaction.getTimeout());
      putStatement(sql, stmt);
    }
    handler.parameterize(stmt);
    return stmt;
  }
  private final Map<String, Statement> statementMap = new HashMap<>();
  private boolean hasStatementFor(String sql) {
    try {
      Statement statement = statementMap.get(sql);
      return statement != null && !statement.getConnection().isClosed();
    } catch (SQLException e) {
      return false;
    }
  }

BatchExecutor

有些場景下,我們要批量保存或者刪除,更新數(shù)據(jù),這時(shí)候我們一條一條的執(zhí)行效率就會很低,需要一個(gè)批量執(zhí)行的機(jī)制。

JDBC 批量操作

批量操作可以把相關(guān)的sql打包成一個(gè) batch,一次發(fā)送到服務(wù)器,減少和服務(wù)器的交互,也就是 RTT 時(shí)間。

使用批量操作前要確認(rèn)服務(wù)器是否支持批量操作,可通過 DatabaseMetaData.supportsBatchUpdates() 方法的返回值來判斷。

實(shí)例代碼,通過 JDBC 提供的 API 執(zhí)行批量操作。

Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

DatabaseMetaData metaData = conn.getMetaData();
System.out.println("metaData.supportsBatchUpdates() = " + metaData.supportsBatchUpdates());

//執(zhí)行 sql
System.out.println("Creating statement...");
String sql = "update user set name=? where id = ?";
pstmt = conn.prepareStatement(sql);

// 設(shè)置變量
pstmt.setString(1, "Pappu");
pstmt.setInt(2, 1);
// 添加到 batch
pstmt.addBatch();

// 設(shè)置變量
pstmt.setString(1, "Pawan");
pstmt.setInt(2, 2);
// 添加到 batch
pstmt.addBatch();

//執(zhí)行,并獲取結(jié)果
int[] count = pstmt.executeBatch();

Mybatis 如何實(shí)現(xiàn)

Mybatis 只有對 update 有支持批量操作,并且需要手動 flushStatements。

insert、delete、update,都是update操作

    BatchExecutor batchExecutor = new BatchExecutor(configuration, jdbcTransaction);

    final MappedStatement update = configuration
        .getMappedStatement("dm.UserMapper.updateName");
    final MappedStatement delete = configuration
        .getMappedStatement("dm.UserMapper.deleteById");
    final MappedStatement get = sessionFactory.getConfiguration()
        .getMappedStatement("dm.UserMapper.getUserByID");
    final MappedStatement insertUser = sessionFactory.getConfiguration()
        .getMappedStatement("dm.UserMapper.insertUser");

    // query
    batchExecutor.doUpdate(insertUser, new User().setName("" + new Date()));
    batchExecutor.doUpdate(insertUser, new User().setName("" + new Date()));

    // batch update
    User user = new User();
    user.setId(2);
    user.setName("" + new Date());
    batchExecutor.doUpdate(update, user);

    user.setId(3);
    batchExecutor.doUpdate(update, user);

    batchExecutor.doUpdate(insertUser, new User().setName("" + new Date()));

    //
    final List<BatchResult> batchResults = batchExecutor.flushStatements(false);
    jdbcTransaction.commit();
    printBatchResult(batchResults);

執(zhí)行日志:

[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: insert into `user` (name) values(?); 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: update `user` set name=? where id = ? 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String), 2(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String), 3(Integer) 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==>  Preparing: insert into `user` (name) values(?); 
[DEBUG][main] o.a.i.l.j.BaseJdbcLogger.debug ==> Parameters: Sat Jul 04 15:07:30 CST 2020(String) 
[DEBUG][main] o.a.i.t.j.JdbcTransaction.commit Committing JDBC Connection [com.MySQL.cj.jdbc.ConnectionImpl@4b3ed2f0] 
第 1 個(gè)結(jié)果
[1, 1]
第 2 個(gè)結(jié)果
[1, 1]
第 3 個(gè)結(jié)果
[1]

從日志可以看到看到清晰的執(zhí)行過程。

  • 第一個(gè)insert語句后面跟著兩個(gè)參數(shù),是一個(gè)statement。對應(yīng)第 1 個(gè)結(jié)果
  • 第二個(gè)update語句后面跟著兩個(gè)參數(shù),是一個(gè)statement。對應(yīng)第 2 個(gè)結(jié)果
  • 第三個(gè)insert語句后面跟著兩個(gè)參數(shù),是一個(gè)statement。對應(yīng)第 3 個(gè)結(jié)果

整體邏輯和程序是一致的,但是有個(gè)問題,為什么三個(gè)相同的 insert,會分開兩個(gè)結(jié)果返回呢?

這是因?yàn)?Mybatis 為了保證批次和邏輯順序一致做了優(yōu)化,并不是相同的sql就放到相同的statement。而是要按照執(zhí)行順序把相同的sql當(dāng)作一個(gè)批次。

從代碼中可以看到這部分邏輯:

public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
  if (sql.equals(currentSql) && ms.equals(currentStatement)) {
    使用當(dāng)前的 statement
  } else {
    創(chuàng)建新的statement
  }
}

總結(jié)

網(wǎng)絡(luò)上有些文章介紹使用 foreach 的方式執(zhí)行批量操作,我個(gè)人不建議這樣操作。

  1. 因?yàn)?JDBC 已經(jīng)提供了批量操作的接口,符合規(guī)范,兼容性和性能更好。
  2. foreach拼接的 sql 比較長,會增加網(wǎng)絡(luò)流量,而且驅(qū)動對sql長度是有限制的,并且要增加allowMultiQueries參數(shù)。
  3. foreach 拼接的 sql 每次都不一定相同,服務(wù)器會重新編譯。

Mysql 的 sql 執(zhí)行流程是連接器,查詢緩存,分析器,優(yōu)化器,執(zhí)行器。分析器先會做“詞法分析”。優(yōu)化器是在表里面有多個(gè)索引的時(shí)候,決定使用哪個(gè)索引;或者在一個(gè)語句有多表關(guān)聯(lián)(join)的時(shí)候,決定各個(gè)表的連接順序。在適合的場景使用 ReuseExecutor 或 BatchExecutor 不僅可以提高性能,還可以減少對 Mysql 服務(wù)器的壓力。


作者:但莫
鏈接:https://juejin.im/post/6854573210420772877
來源:掘金

分享到:
標(biāo)簽:Mybatis
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定