SQLInterceptor.java
package com.guide.util;
i
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
mport java.sql.Connection;
import java.util.Date;
import java.util.Properties;
import org.apache.ibatis.executor.statement.BaseStatementHandler;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
/**
-
运行时SQL输出
*/
@Intercepts({ @Signature(type = StatementHandler.class, method = “prepare”, args = { Connection.class }) })
public class SQLInterceptor implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
RoutingStatementHandler statementHandler = (RoutingStatementHandler) invocation.getTarget();
BoundSql boundSql = statementHandler.getBoundSql();
BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler,
“delegate”);
MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate,
“mappedStatement”);
System.err.println("-------------------------------------------------" + mappedStatement.getId());
System.out.println(“sql:”+DateUtil.getDetailDate(new Date())+":"+boundSql.getSql());
return invocation.proceed();
}
public Object plugin(Object arg0) {
return Plugin.wrap(arg0, this);
}
public void setProperties(Properties properties) {
}
}
ReflectHelper.java
package com.guide.util;
import java.lang.reflect.Field;
/**
-
反射工具
*/
public class ReflectHelper {
/**
-
获取obj对象fieldName的Field
-
@param obj
-
@param fieldName
-
@return
*/
public static Field getFieldByFieldName(Object obj, String fieldName) {
for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
}
}
return null;
}
/**
-
获取obj对象fieldName的属性值
-
@param obj