mybatis interceptor does not take effect after modifying sql re-set?

Mybatis use interceptors to do data rights management, sql intercept and analyze, modify and re-set. However, some take effect and some do not take effect. Information console printing means that all sql modifications are successful, then the problem lies in the method of re-set.

The beginning of this method are:

void setCurrentSql Private (Invo the Invocation, String SQL) { 
MappedStatement mappedStatement = getMappedStatement (Invo);
Object [] args = invo.getArgs ();
Object args = paramObj [PARAM_OBJ_INDEX];
BoundSql boundSql = mappedStatement.getBoundSql (paramObj);
ReflectUtil. SetFieldValue (boundSql, "sql", sql);
}

to modify the value of sql boundSql by reflection property, but there are problems.
Then switch to this method:
private void setCurrentSql(Invocation invo, String sql) {
BoundSql boundSql = getBoundSql(invo);
List<ParameterMapping> parameterMappings = boundSql.
getParameterMappings();
Object paramObj = boundSql.getParameterObject();
MappedStatement mappedStatement = getMappedStatement(invo);
Configuration configuration = mappedStatement.getConfiguration();
BoundSql newBoundSql = new BoundSql(configuration, sql,
parameterMappings, paramObj);
for (ParameterMapping parameterMapping : parameterMappings) {
String prop = parameterMapping.getProperty();
if (boundSql.hasAdditionalParameter(prop)) {
Param = boundSql.getAdditionalParameter Object (prop);
newBoundSql.setAdditionalParameter (prop, param);
}
}

BoundSqlSource newSqlSource = new new BoundSqlSource (newBoundSql);
MappedStatement newMappedStatement = copyFromMappedStatement (
mappedStatement, newSqlSource);
Object [] args = invo.getArgs () ;
args [MAPPED_STATEMENT_INDEX] = newMappedStatement;
}

problem is solved. There is also a method that looks more concise:
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;

Guess you like

Origin www.cnblogs.com/longmenzhitong/p/11271909.html