1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; import org.apache.ibatis.plugin.*; import org.apache.ibatis.reflection.DefaultReflectorFactory; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.factory.DefaultObjectFactory; import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.springframework.util.StringUtils;
import java.util.Properties;
@Intercepts(@Signature( type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) ) @Slf4j public class TestInterceptor implements Interceptor {
@Override public Object intercept(Invocation invocation) throws Throwable { this.queryInvocation(invocation); return invocation.proceed(); }
@Override public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override public void setProperties(Properties properties) {
}
private void queryInvocation(Invocation invocation) { log.info("进入自定义query拦截器");
MappedStatement arg = (MappedStatement) invocation.getArgs()[0];
SqlSource sqlSource = arg.getSqlSource(); Object param = invocation.getArgs()[1]; BoundSql boundSql = sqlSource.getBoundSql(param); String sql = boundSql.getSql(); if (StringUtils.isEmpty(sql)) { return; }
MappedStatement newMappedStatement = this.copyFromMappedStatement(arg, new BoundSqlSqlSource(boundSql));
MetaObject metaObject = MetaObject.forObject(newMappedStatement, new DefaultObjectFactory(), new DefaultObjectWrapperFactory(), new DefaultReflectorFactory()); metaObject.setValue("sqlSource.boundSql.sql", this.newSql(sql)); invocation.getArgs()[0] = newMappedStatement; }
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) { builder.keyProperty(ms.getKeyProperties()[0]); } builder.timeout(ms.getTimeout()); builder.parameterMap(ms.getParameterMap()); builder.resultMaps(ms.getResultMaps()); builder.resultSetType(ms.getResultSetType()); builder.cache(ms.getCache()); builder.flushCacheRequired(ms.isFlushCacheRequired()); builder.useCache(ms.isUseCache()); return builder.build(); }
public String newSql(String sql) { log.info("封装前的sql: {}", sql); sql += " limit 1"; log.info("封装后的sql: {}", sql); return sql; } }
|