MyBatisのインターセプタプラグ(インターセプタ)

参考https://blog.csdn.net/weixin_39494923/article/details/91534658

//実際のプロジェクト

パッケージcom.yueworldframework.core.mybatis。

輸入com.yueworldframework.core.support.SessionHelper。
輸入org.apache.ibatis.executor.Executor。
輸入org.apache.ibatis.mapping.MappedStatement。
輸入org.apache.ibatis.mapping.SqlCommandType。
org.apache.ibatis.pluginインポート*。
輸入org.slf4j.Logger。
輸入org.slf4j.LoggerFactory;
輸入org.springframework.beans.BeanUtils。

輸入java.lang.reflect.Methodオブジェクト。
輸入java.util.Date;
輸入java.util.Properties;

/ **
 * wangbsによって作成されました
 * /
@Intercepts({@署名(タイプ= Executor.class、方法= "更新"、引数の= {MappedStatement.class、Object.classを})})
パブリッククラスAuditingInterceptorはインターセプタを{実装します
    プライベート静的最終ロガーロガー= LoggerFactory.getLogger(AuditingInterceptor.class)。

    プライベートプロパティの小道具= NULL;

    @オーバーライド
    パブリックオブジェクトインターセプト(呼び出し呼び出しは)のThrowableを{スロー
        MappedStatement mappedStatement =(MappedStatement)invocation.getArgs()[0];
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType()。
        IF(invocation.getArgs()[1] POJOのinstanceof){
            SINGパラメータ=(歌唱)invocation.getArgs()[1]。
            IF(SqlCommandType.INSERT == sqlCommandType){
                //マスターキーIDを初期化します
                方法initializeUUID = BeanUtils.findDeclaredMethod(parameter.getClass()、 "initializeUUID")。
                {(ヌル!= initializeUUID)の場合
                    initializeUUID.invoke(パラメータ)。
                }
                IF(ヌル== parameter.getCreator()){
                    parameter.setCreator(SessionHelper.getId())。
                }
                parameter.setUpdater(SessionHelper.getId())。
                parameter.setCreatedDate(新しいDate());
                parameter.setUpdatedDate(parameter.getCreatedDate())。
                parameter.setVersion(1)。
            }そうであれば(SqlCommandType.UPDATE == sqlCommandType){
                parameter.setUpdater(SessionHelper.getId())。
                parameter.setUpdatedDate(新しいDate());
                parameter.setVersion(parameter.getVersion()+ 1)。
            }
        }
        invocation.proceed返します();
    }

    @オーバーライド
    パブリックオブジェクトプラグイン(オブジェクト・ターゲット){
        IF(エグゼキュータのinstanceofターゲット){
            Plugin.wrap(ターゲット、this)を返します。
        }他{
            ターゲットを返します。
        }
    }

    公共のボイドのsetProperties(プロパティのプロパティ){
        もし(ヌル=プロパティ&& properties.isEmpty()!!)小道具=プロパティ。
    }
}

  

おすすめ

転載: www.cnblogs.com/xiaowangbangzhu/p/11670657.html
おすすめ