Mybatis三種執行器

2021-10-04 14:16:19 字數 4797 閱讀 8046

配置預設的執行器

設定名描述有效值

預設值defaultexecutortype

****** 就是普通的執行器

reuse 執行器會重用預處理語句(preparedstatement)

batch 執行器不僅重用語句還會執行批量更新

******

reuse

batch

******

// org.apache.ibatis.session.executortype

public enum executortype

/**

* @param list 插入的user集合

* @param type executortype

* @param autocommit 是否自動提交

*/public void insertuser(listlist, executortype type, boolean autocommit) catch (exception e) finally

long end = system.currenttimemillis();

system.out.println("【"+type+"耗時】: " + (end - start) + "(ms)");

}

@test

public void testinsertuser()

//測試

// ******: 預設的執行器, 對每條sql進行預編譯->設定引數->執行等操作

insertuser(list, executortype.******, false);//【******耗時】: 118136(ms)

// batch: 批量執行器, 對相同sql進行一次預編譯, 然後設定引數, 最後統一執行操作

insertuser(list, executortype.batch, false);//【batch耗時】: 59105(ms)

// reuse: reuse 執行器會重用預處理語句(prepared statements)

insertuser(list, executortype.reuse, false);//【reuse耗時】: 114406(ms)

}

jdbc

public void test_1(int count) throws sqlexception 

}

public void test_2(int count) throws sqlexception 

}

public void test_3(int count) throws sqlexception 

int resarr = pstmt.executebatch();

}

@test

public void test() throws sqlexception

從執行日誌可以看出, 每次插入操作,都會執行編譯,設定引數,執行sql操作。

debug [main] ==>  preparing: insert into mybatis_bash_db.user(id, username, ***, birthday, address, password) values (?, ?, ?, ?, ?, ?) 

debug [main] ==> parameters: 0(integer), 0(string), 男(string), 2020-04-01 11:18:16.44(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

debug [main] ==> preparing: insert into mybatis_bash_db.user(id, username, ***, birthday, address, password) values (?, ?, ?, ?, ?, ?)

debug [main] ==> parameters: 1(integer), 1(string), 男(string), 2020-04-01 11:18:16.44(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

debug [main] ==> preparing: insert into mybatis_bash_db.user(id, username, ***, birthday, address, password) values (?, ?, ?, ?, ?, ?)

debug [main] ==> parameters: 2(integer), 2(string), 男(string), 2020-04-01 11:18:16.44(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

debug [main] ==> preparing: insert into mybatis_bash_db.user(id, username, ***, birthday, address, password) values (?, ?, ?, ?, ?, ?)

debug [main] ==> parameters: 3(integer), 3(string), 男(string), 2020-04-01 11:18:16.44(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

...

從執行日誌可以看出,只有第一次插入操作,執行了sql編譯步驟,對其它插入操作執行了設定引數,執行sql的操作。

debug [main] ==>  preparing: insert into mybatis_bash_db.user(id, username, ***, birthday, address, password) values (?, ?, ?, ?, ?, ?) 

debug [main] ==> parameters: 0(integer), 0(string), 男(string), 2020-04-01 11:23:58.522(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

debug [main] ==> parameters: 1(integer), 1(string), 男(string), 2020-04-01 11:23:58.522(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

debug [main] ==> parameters: 2(integer), 2(string), 男(string), 2020-04-01 11:23:58.522(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

debug [main] ==> parameters: 3(integer), 3(string), 男(string), 2020-04-01 11:23:58.522(timestamp), 重慶萬州(string), 123456(string)

debug [main] <== updates: 1

...

從執行日誌可以看出,只對第一次插入操作執行了sql編譯操作,對其它插入操作僅執行了設定引數操作,最後統一執行。

debug [main] ==>  preparing: insert into mybatis_bash_db.user(id, username, ***, birthday, address, password) values (?, ?, ?, ?, ?, ?) 

debug [main] ==> parameters: 0(integer), 0(string), 男(string), 2020-04-01 11:26:24.683(timestamp), 重慶萬州(string), 123456(string)

debug [main] ==> parameters: 1(integer), 1(string), 男(string), 2020-04-01 11:26:24.683(timestamp), 重慶萬州(string), 123456(string)

debug [main] ==> parameters: 2(integer), 2(string), 男(string), 2020-04-01 11:26:24.683(timestamp), 重慶萬州(string), 123456(string)

debug [main] ==> parameters: 3(integer), 3(string), 男(string), 2020-04-01 11:26:24.683(timestamp), 重慶萬州(string), 123456(string)

...

Mybatis的三種執行器

mybatis的持久層操作由三部分組成 連線資料來源 執行語句 操作 executor 每次執行update或select都會開啟乙個statement物件,用完立刻關閉statement物件 reuseexecutorbatchexecutor 執行update 沒有select,jdbc批處理不...

Mybatis 三種查詢方式

1.selectlist 返回值為 list1.1 適用於查詢結果都需要遍歷的需求 listlist session.selectlist a.b.selall for flower flower list 2.selectone 返回值 object,2.1 適用於返回結果只是變數或一行資料時 i...

mybatis分頁的三種方案

第1種 推薦 看到dao層方法就知道該傳什麼樣的引數,比較直觀 dao層介面函式 public listgetuserarticles param value id int id,param value offset int offset,param value pagesize int pages...