再設定されたSQLを変更した後MyBatisのインターセプタが有効になりませんか?

MyBatisの使用インターセプタは、SQLインターセプトをデータ著作権管理を行い、分析し、修正して再設定すること。しかし、いくつかは有効になり、一部は有効になりません。情報コンソールの印刷がすべてのSQLの変更が成功していることを意味し、問題が再集合の方法にあります。

この方法の始まりは、以下のとおりです。

ボイドsetCurrentSqlプライベート(INVO呼び出し、文字列のSQL){ 
MappedStatement mappedStatement = getMappedStatement(INVO);
オブジェクト[]引数の=のinvo.getArgs();
オブジェクト引数= paramObj [PARAM_OBJ_INDEX];
BoundSql boundSql = mappedStatement.getBoundSql(paramObj);
ReflectUtil。 ; SetFieldValue(boundSql、 "SQL"、SQL)
}

反射特性によってSQL boundSqlの値を変更するが、問題があります。
そして、この方法に切り替えます。
プライベートボイドsetCurrentSql(呼び出しINVO、文字列のSQL){ 
BoundSql boundSql = getBoundSql(INVO)。
一覧<ParameterMapping> parameterMappings = boundSql。
getParameterMappings();
オブジェクトparamObj = boundSql.getParameterObject()。
MappedStatement mappedStatement = getMappedStatement(INVO)。
構成設定= mappedStatement.getConfiguration()。
BoundSql newBoundSql =新しいBoundSql(構成、SQL、
parameterMappings、paramObj)。
(ParameterMapping parameterMapping:parameterMappings)用{
文字列プロップ= parameterMapping.getProperty()。
IF(boundSql.hasAdditionalParameter(プロプ)){
PARAM = boundSql.getAdditionalParameterオブジェクト(プロプ);
newBoundSql.setAdditionalParameter(プロプ、PARAM);
}
}

BoundSqlSource newSqlSource =新しい新しいBoundSqlSource(newBoundSql);
MappedStatement newMappedStatement = copyFromMappedStatement(
mappedStatement、newSqlSource);
[]引数の=のinvo.getArgsオブジェクト() ;
引数[MAPPED_STATEMENT_INDEX] = newMappedStatement;
}

問題が解決されます。より簡潔に見える方法もあります。
private void setCurrentSql(Invocation invo, String sql) {
MappedStatement mappedStatement = getMappedStatement(invo);
Object[] args = invo.getArgs();
Object paramObj = args[PARAM_OBJ_INDEX];
BoundSql boundSql = mappedStatement.getBoundSql(paramObj);
BoundSqlSource boundSqlSource = new BoundSqlSource(boundSql);
MappedStatement newMappedStatement = copyFromMappedStatement(
mappedStatement, boundSqlSource);
MetaObject metaObject = MetaObject.forObject(newMappedStatement,
new DefaultObjectFactory(), new DefaultObjectWrapperFactory(),
new DefaultReflectorFactory());
metaObject.setValue("sqlSource.boundSql.sql", sql);
args[MAPPED_STATEMENT_INDEX] = newMappedStatement;
}

上面用到的方法getMappedStatement:
private MappedStatement getMappedStatement(Invocation invo) {
Object[] args = invo.getArgs();
Object mappedStatement = args[MAPPED_STATEMENT_INDEX];
return (MappedStatement) mappedStatement;
}
私有内部类BoundSqlSource:
private class BoundSqlSource implements SqlSource {

private BoundSql boundSql;

private BoundSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}

@Override
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}

另外还有:
private static final int MAPPED_STATEMENT_INDEX = 0;
private static final int PARAM_OBJ_INDEX = 1;

おすすめ

転載: www.cnblogs.com/longmenzhitong/p/11271909.html