一、Mybatis執行器有哪些
Mybatis執行器是Mybatis框架中的重要組成部分,用於執行SQL語句並返回結果。Mybatis執行器包括:
- SimpleExecutor:簡單執行器
- ReuseExecutor:可重用執行器
- BatchExecutor:批量執行器
三個執行器相互獨立,也可以組合使用。
二、Mybatis三個執行器
Mybatis的三個執行器分別是SimpleExecutor、ReuseExecutor和BatchExecutor。下面對它們分別進行介紹。
1. SimpleExecutor
SimpleExecutor是Mybatis中最簡單的執行器,它每次執行都會創建一個Statement對象,執行完SQL語句就立即關閉Statement對象。SimpleExecutor存在的唯一意義就是為了方便開發者從Mybatis中獲取底層的JDBC執行器。SimpleExecutor主要有三個核心方法:
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
Connection connection = getConnection(configuration);
stmt = handlerPrepareStatement(configuration, connection, ms.getBoundSql(parameter).getSql());
handlerParameter(stmt, ms, parameter);
int effectCount = stmt.executeUpdate();
return effectCount;
} finally {
if (stmt != null) {
closeStmt(stmt);
}
}
}
public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds,
ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
Connection connection = getConnection(configuration);
stmt = handlerPrepareStatement(configuration, connection, boundSql.getSql());
handlerParameter(stmt, ms, parameter);
ResultSet rs = stmt.executeQuery();
return handlerResultSet(rs, ms, resultHandler, rowBounds, cacheKey, boundSql);
} finally {
if (stmt != null) {
closeStmt(stmt);
}
}
}
public List doFlushStatements(boolean isRollback) throws SQLException {
return null;
}
2. ReuseExecutor
ReuseExecutor是Mybatis中的可重用執行器,它會先從緩存中拿到Statement對象,如果緩存中沒有,則創建新的Statement對象。在執行完SQL語句之後,不會立即關閉Statement對象,而是將它放回緩存中以便下次重複利用。ReuseExecutor主要有兩個核心方法:
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
Connection connection = getConnection(configuration);
stmt = getStatement(configuration, connection, ms.getBoundSql(parameter).getSql());
handlerParameter(stmt, ms, parameter);
int effectCount = stmt.executeUpdate();
return effectCount;
} finally {
if (stmt != null) {
closeStmt(stmt);
}
}
}
public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds,
ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
Connection connection = getConnection(configuration);
stmt = getStatement(configuration, connection, boundSql.getSql());
handlerParameter(stmt, ms, parameter);
ResultSet rs = stmt.executeQuery();
return handlerResultSet(rs, ms, resultHandler, rowBounds, cacheKey, boundSql);
} finally {
if (stmt != null) {
closeStmt(stmt);
}
}
}
3. BatchExecutor
BatchExecutor是Mybatis中的批量執行器,它採用JDBC的批量執行特性,將多個SQL語句一次性發送給數據庫執行。BatchExecutor主要有三個核心方法:
public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
addBatch(ms, parameter);
return flushStatements();
}
public List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds,
ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException {
if (closed) {
throw new ExecutorException("Executor was closed.");
}
Object cacheObject = localCache.getObject(cacheKey);
if (cacheObject != null) {
if (cacheObject instanceof List) {
return resultHandler.handleResultSets(this, (List) cacheObject);
} else if (cacheObject instanceof Cursor) {
return resultHandler.handleCursorResultSets((Cursor) cacheObject);
} else {
return resultHandler.handleResultSets(this, (ResultSet) cacheObject);
}
}
flushStatements();
return null;
}
public List doFlushStatements(boolean isRollback) throws SQLException {
List batchResults = new ArrayList();
for (Statement stmt : statementMap.values()) {
BatchResult batchResult;
if (isRollback) {
batchResult = new BatchResult(stmt.getClass().getSimpleName(), new int[0], stmt.getUpdateCount());
} else {
batchResult = new BatchResult(stmt.getClass().getSimpleName(), stmt.executeBatch(), new int[0]);
}
batchResults.add(batchResult);
closeStmt(stmt);
}
statementMap.clear();
return batchResults;
}
三、Mybatis執行器原理
Mybatis執行器的原理是:根據用戶配置的SQL,使用JDBC向數據庫發送SQL請求,並接收數據庫返回的結果。
Mybatis會根據用戶配置的SQL類型(SELECT、UPDATE、INSERT、DELETE),來選擇執行查詢(Query)方法還是執行更新(Update)方法。Query方法會返回一個ResultHandler,Update方法會返回影響行數。
Mybatis中的執行器會結合JDBC提供的三種執行方式(Statement、PreparedStatement和callableStatement)和三種結果集(ResultSet、UpdateCount和ResultSetOrUpdateCount),實現對SQL語句的執行和結果的處理。
四、Mybatis執行器作用
Mybatis的執行器主要作用是抽象出常見的SQL語句,統一封裝一些底層的數據庫操作,從而讓開發者更加便捷地操作數據庫。Mybatis執行器核心就是使用JDBC向數據庫發送SQL語句,並處理返回的結果。
五、Mybatis執行器類型
Mybatis執行器有三種類型:SimpleExecutor、ReuseExecutor和BatchExecutor。三個執行器相互獨立,也可以組合使用。
六、Mybatis執行器有幾種
Mybatis總共有三個執行器,分別是SimpleExecutor、ReuseExecutor和BatchExecutor。如上所述。
七、Mybatis執行器幹什麼的
Mybatis執行器主要作用是執行SQL語句並返回結果。Mybatis框架提供了不同類型的執行器,可以根據實際的需要進行選擇。
八、Mybatis執行器默認
Mybatis的默認執行器是SimpleExecutor。
九、Mybatis執行流程圖
Mybatis執行器的流程圖如下:
+-------------+
| Executor |
| |
+------+------+
|
+-----------+ +--------------------+
| Statement |-----------| StatementHandler |
| 或 PreparedStatement | |
+-----------+ +------+-------------+
|
+------------------------+------------------------+
| |
+----------+------+ +-----------+-------+
| Parameter | | ResultSetHandler |
| Handler | | |
+----------+------+ +-----------+--------+
|
|
+-------------------+
| JDBC |
| |
+------------------+
在執行SQL語句之前,Mybatis會將SQL語句中的參數替換成?,並將參數類型和值保存在BoundSql對象中。緊接着,Mybatis會調用StatementHandler的prepare()方法,使用JDBC的Statement對象創建SQL語句的結果集。當SQL語句中存在動態SQL時,StatementHandler會動態構建SQL語句,並將參數設置到SQL語句中。
當StatementHandler對象構建好SQL語句之後,Mybatis會將其交給JDBC驅動程序處理。當JDBC處理完SQL語句後,Mybatis會將ResultSet結果集交給ResultSetHandler進行處理。ResultSetHandler主要用來將ResultSet轉化為java對象,並處理ResultSet中的異常。
最後,Mybatis會將處理結果返回給應用程序。
原創文章,作者:YMRW,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/133462.html