MyBatis迎撃腸内腸について多くのことを知らないJavaの初心者がたくさんいます。ここでは、JavaのMyBatis Interceptor Inteceptorを説明するために、次の記事を整理します。
この記事では、主にMyBatisのプラグインメカニズムを分析します。これは、実際にはJava Dynamic Proxyの責任チェーンモデルの実装です。
公式の文書によると。 MyBatisは、インターセプター注釈の署名パラメーターを決定する次の方法を傍受することのみを許可します。
コードは次のとおりです
executor(更新、クエリ、フラッシュステートメント、コミット、ロールバック、ゲットトランザクション、クローズ、isclose)parameterhandler(getParameterObject、setParameters)resthandler(handleresultsets、handleoutputparameters)ステートメントハンドラー(準備、パラメーター、バッチ、更新、更新、クエリ))
インターセプター処理のソースコードは次のとおりです。ここでは、Interceptorchain.Pluginall(..)はカスタムインターセプター織りです。
コードは次のとおりです
/ * org.apache.ibatis.session.configuration class */public parameterhandler newparameterhandler(MappedStatement MappedStatement、Object ParameterObject、BoundSQL BoundsQl){ParameterHandler = MappedStatement.getLang()。 /* Intercept ParameterHandler*/ parameterhandler =(parameterhandler)interceptorchain.pluginall(parameterhandler); parameterhandler;} public resultsethrler newresultsetherler(エグゼキューターエグゼキューター、マッピングステートメントマッピングステートメント、rowbounds rowbounds、parameterhandler parameterhandler、resulthandler resulthandler、boundsql boundsql) Resulthandler、Boundsql、Rowbounds); /* septrectesthandler*/ resultsetandler =(resterethandler)interceptorchain.pluginall(restherthandler); return resultsEthandler;} public StatementHandler newStatementHandler(エグゼキューターエグゼキューター、マッピングステートメントマッピングステートメント、オブジェクトパラメーターオブジェクト、ロウバウンドロウバウンド、再rthandler resultherler、boundsql boundsql) Boundsql); /* Intercept StatementHandler*/ StatementHandler =(StatementHandler)InterceptOrchain.Pluginall(StatementHandler); return StatementHandler;} public executor newexecutor(Transaction Transaction、Executortype executortype){executortype = executortype == null? DefaultExeCutortype:ExecutorType; executortype = executortype == null? executortype.simple:executortype;執行者の執行者。 if(executortype.batch == executortype){executor = new batchexecutor(this、transaction); } else if(executortype.reuse == executortype){executor = new reuseexecutor(this、transaction); } else {executor = new SimpleExecutor(this、transaction); } if(cacheenabled){executor = new cachingexecutor(executor); } /* exexector* / executor =(executor)interceptorchain.pluginall(executor);執行者を返す;}カスタムインターセプターを実装するには、インターセプターインターフェイスを実装するだけです。一般的なコードは次のとおりです。
コードは次のとおりです
/*インターセプトするインターフェイスを示す注釈の方法とパラメーター/ *注:ここでは、Invocation.ProCeed()メソッドは実際に使用され、インターセプトマーチェーンのトラバーサルコール(つまり、インターセプターのすべての登録インターセプトメソッドを実行するため)、および最終プロキシオブジェクトの元のメソッド呼び出し */ RETURN INVOCATION.PRO CELLES(); } / *ターゲットターゲットのプロキシを生成すると、@interceptsの注釈がplugin.wrap * / @overrideパブリックオブジェクトプラグイン(オブジェクトターゲット){ / *ターゲットクラスがステートメントハンドラータイプの場合、ターゲットクラスがラップされていない場合、意味のないプロキシが使用されません * / return(ターゲットインスタンスインスタンスステートメントハンドラー)?プラグイン)? } /*カスタムインターセプター構成パラメーターの設定に使用* / @Override public void setProperties(プロパティプロパティ){}}その中で、インターセプトコールのコードはPlugin.wrapにあります:
コードは次のとおりです
/* org.apache.ibatis.plugin.plugin class*/public classプラグインInvocationhandler {/*コードを省略します...*/public static Object wrap(Objectターゲット、インターセプターインターセプター){/*インターセプターの注釈署名を取得するために* class <?> type = target.getClass(); /*ターゲットクラスに一致するインターフェイスを取得*/ class <? if(interfaces.length> 0){ /* jdk dynamic proxy*を使用* / proxy.newproxyinstance(type.getClassLoader()、インターフェイス、新しいプラグイン(ターゲット、インターセプター、SignatureMap)); }ターゲットを返します。 } /*インターセプトターゲットクラスのすべてのメソッドの実行は、ここで実行されます* / @OverrideパブリックオブジェクトInvoke(オブジェクトプロキシ、メソッドメソッド、オブジェクト[] arg)スロー可能{try {set <method> method = signaturemap.get(method.getDeclaringclass(); if(method!= null && methods.contains(method)){ /*インターセプターメソッドを実行* / return interceptor.intercept(new Invocation(Target、Method、Args)); } return method.invoke(ターゲット、args); } catch(Exception e){exceptionutil.unwrapthrowable(e); }} / *コードを省略... * /}MyBatisインターセプター設計のコアコードは比較的単純であることがわかりますが、十分に柔軟です。実際に使用する場合は、無意味なプロキシ(Plugin.Wrap)ではないように注意してください。