言うまでもなく、ソースコード学習の利点はボリュームが小さく、ロジックがシンプルであるため、学習する一連の記事を書きます。
sqlsession
MyBatis使用ポータルは、org.apache.ibatis.sessionパッケージのsqlsessionにあります。それはインターフェイスであることがわかり、org.apache.ibatis.session.defaultsパッケージにデフォルトの実装クラスdefaultsqlsessionが必要です。このクラスは決して新しいクラスではありません。Javaの規則によると、SQLSessionFactoryのファクトリーメソッドを使用しています。インターフェイスでもあることがわかったため、デフォルトの実装クラスDefaultSQLSessionFactoryが必要です。このクラスはまだ単独で作成する必要はなく、SQLSessionFactoryBuilderで工場メソッドを使用しています。
defaultsqlsession
defaultsqlsessionの主な方法:
1)cursor <t> SelectCursor(String Statement、Object Parameter、Rowbounds Rowbounds)、executor.QueryCursor(MS、wrapCollection(パラメーター)、rowbounds)に委任されました。
2)list <e> selectlist(stringステートメント、オブジェクトパラメーター、rowbounds rowbounds)およびvoid select(stringステートメント、オブジェクトパラメーター、rowbounds rowbounds、resulthandlerハンドラー)、およびexecure.query(ms、wrapcollection(パラメーター)、rowbounds、handler)に委任します。
3)int update(stringステートメント、オブジェクトパラメーター)、executor.update(ms、wrapcollection(parameter))に委任します。
最終的にそれを完了する執行者がいることがわかります。
執行者
執行者は、org.apache.ibatis.executorパッケージにあります。インターフェイスです。実装クラスは、BaseexecutorおよびCachingexecutorです。その中で、Baseexecutorは要約であり、3つのサブクラス、Simple Executor、Reuseexecutor、Batchexecutorがあります。名前を見て、意味を知っています。 Baseexecutorの主な方法:
1)リスト<e>クエリ(マッピングステートMS、オブジェクトパラメーター、Rowbounds Rowbounds、Resulthandler Resulthandler、Cachekey Key、BoundsQl BoundSQL)およびリスト<e> QueryFromDataBase(マッピングステートメントMS、オブジェクトパラメーター、RowBounds Rowbounds、Resulthandler Resulthandler Resultherdler、CACHEKEYキーキー、RESULTHLER resulthler抽象として委任<e>リスト<e> doquery(MappedStatement MS、Object Parameter、Rowbounds Rowbounds、Resulthandler Resulthandler、Boundsql BoundsQl)。
2)cursor <e> querycursor(マッピングステートMS、オブジェクトパラメーター、rowbounds rowbounds)、抽象<e> cursor <e> doquerycursor(MappedStatement MS、Object Parameter、Rowbounds Rowbounds、Boundsql BoundsQl)に委任されました。
3)int update(MappedStatement MS、Objectパラメーター)、抽象int doupdate(MappedStatement MS、オブジェクトパラメーター)に委任します。
ベースクラスはパブリックパートを処理し、サブクラスの実装に任せます。
SimpleExecutorの主な方法を見てみましょう。
1)list <e> doquery(マッピングステートメントMS、オブジェクトパラメーター、Rowbounds Rowbounds、Resulthandler Resultherler、BoundsQl BoundsQl)、ハンドラーに委任されました。
2)cursor <e> doquerycursor(MappedStatement MS、Object Parameter、Rowbounds Rowbounds、Boundsql BundsQl)、Handlerに委託された。<e> QueryCursor(STMT)。
3)int doupdate(MappedStatement MS、オブジェクトパラメーター)、Handler.Update(STMT)に委任します。
ハンドラーによって最終的に処理されることがわかります。
StatementHandler
StatementHandlerは、org.apache.ibatis.executor.statementパッケージにあります。インターフェイスです。実装クラスは、BaseStateMentementHandlerとRoutingStatementHandlerです。 BaseStatementhandlerは抽象的で、3つのサブクラスがSimplestatementementhandler、preatedStatementhandler、callablestatementhandlerがあります。これらの3つは、パラメーター、パラメーター化されたSQLステートメント、およびストアドプロシージャを使用せずにSQLステートメントを処理する必要があります。 simplestatementementhandlerの主な方法を見てみましょう。
1)リスト<e> query(ステートメントステートメント、resulthandler resulthandler)、statement.execute(sql)に委任します。
2)cursor <e> querycursor(ステートメントステートメント)、statement.execute(sql)に委任します。
3)int update(ステートメントステートメント)、statement.execute(sql)に委任します。
最後に、SQLはステートメントによって実行されます。これは、java.sqlパッケージに戻ります。
MyBatisは、主にSQLパラメーターのカプセル化処理を完了し、結果セットを取得してオブジェクトを生成し、SQLステートメントの構築プロセスをユーザーに残します。 Mybatisの著者はこのように設計しました。フレームワークは全体的に半自動性ですが、その柔軟性は大幅に向上しています。