1. 拦截器
java">package com. xxx. platform. common. interceptor ; import com. baomidou. dynamic. datasource. toolkit. DynamicDataSourceContextHolder ;
import com. xxx. platform. common. aop. OLAPQuery ;
import com. xxx. platform. constant. CommonConstant ;
import com. xxx. platform. util. SQLFormatHelper ;
import lombok. extern. slf4j. Slf4j ;
import org. apache. ibatis. cache. CacheKey ;
import org. apache. ibatis. executor. Executor ;
import org. apache. ibatis. mapping. BoundSql ;
import org. apache. ibatis. mapping. MappedStatement ;
import org. apache. ibatis. plugin. * ;
import org. apache. ibatis. session. ResultHandler ;
import org. apache. ibatis. session. RowBounds ;
import org. springframework. beans. factory. annotation. Value ;
import org. springframework. stereotype. Component ; import java . lang. reflect. Method ;
import java . util. Objects ;
import java . util. Properties ;
@Component
@Intercepts ( { @Signature ( type = Executor . class , method = "query" , args = { MappedStatement . class , Object . class , RowBounds . class , ResultHandler . class } ) , @Signature ( type = Executor . class , method = "query" , args = { MappedStatement . class , Object . class , RowBounds . class , ResultHandler . class , CacheKey . class , BoundSql . class } ) , }
)
@Slf4j
public class SQLTimerInterceptor implements Interceptor { @Value ( "${spring.profiles.active}" ) private String profile; @Override public Object intercept ( Invocation invocation) throws Throwable { long startTime = System . currentTimeMillis ( ) ; OLAPQuery olap = null ; try { Method method = invocation. getMethod ( ) ; olap = method. getAnnotation ( OLAPQuery . class ) ; if ( ! Objects . isNull ( olap) ) { DynamicDataSourceContextHolder . push ( CommonConstant . PSS ) ; } return invocation. proceed ( ) ; } finally { if ( ! Objects . isNull ( olap) ) { DynamicDataSourceContextHolder . clear ( ) ; } long duration = System . currentTimeMillis ( ) - startTime; SQLFormatHelper. PrintSQL printSQL = SQLFormatHelper . getFormatSQL ( invocation) ; String sql Id = printSQL. sql Id; String sql = printSQL. formatSQL; int ts = "lmp-sy-dev" . equals ( profile) ? 1000 : 3000 ; if ( duration >= ts) { log. warn ( "{} execute SQL>=5s: {} ms, \nSQL: {}" , sql Id, duration, sql ) ; } } } @Override public Object plugin ( Object target) { return Plugin . wrap ( target, this ) ; } @Override public void setProperties ( Properties properties) { } }
2. 其他工具类
java">package com. xxx. platform. util ; import org. apache. commons. collections. CollectionUtils ;
import org. apache. ibatis. mapping. BoundSql ;
import org. apache. ibatis. mapping. MappedStatement ;
import org. apache. ibatis. mapping. ParameterMapping ;
import org. apache. ibatis. plugin. Invocation ;
import org. apache. ibatis. reflection. MetaObject ;
import org. apache. ibatis. session. Configuration ;
import org. apache. ibatis. type. TypeHandlerRegistry ; import java . text. DateFormat ;
import java . util. Date ;
import java . util. List ;
import java . util. regex. Matcher ;
public final class SQLFormatHelper { public static PrintSQL getFormatSQL ( Invocation invocation) { PrintSQL sql = new PrintSQL ( ) ; MappedStatement mappedStatement = ( MappedStatement ) invocation. getArgs ( ) [ 0 ] ; String sql Id = mappedStatement. getId ( ) ; sql . sql Id = sql Id; try { Object parameter = null ; if ( invocation. getArgs ( ) . length > 1 ) { parameter = invocation. getArgs ( ) [ 1 ] ; } BoundSql boundSql = mappedStatement. getBoundSql ( parameter) ; Configuration configuration = mappedStatement. getConfiguration ( ) ; String formatSQL = showSql ( configuration, boundSql) ; sql . formatSQL = formatSQL; } catch ( Exception e) { e. printStackTrace ( ) ; BoundSql bs = ( BoundSql ) invocation. getArgs ( ) [ 5 ] ; sql . formatSQL = bs. getSql ( ) ; } return sql ; } private static String getParameterValue ( Object obj) { String value = null ; if ( obj instanceof String ) { value = "'" + obj. toString ( ) + "'" ; } else if ( obj instanceof Date ) { DateFormat formatter = DateFormat . getDateTimeInstance ( DateFormat . DEFAULT , DateFormat . DEFAULT ) ; value = "'" + formatter. format ( new Date ( ) ) + "'" ; } else { if ( obj != null ) { value = obj. toString ( ) ; } else { value = "" ; } } return value; } private static String showSql ( Configuration configuration, BoundSql boundSql) { Object parameterObject = boundSql. getParameterObject ( ) ; List < ParameterMapping > parameterMappings = boundSql. getParameterMappings ( ) ; String sql = boundSql. getSql ( ) . replaceAll ( "[\\s]+" , " " ) ; if ( ! CollectionUtils . isEmpty ( parameterMappings) && parameterObject != null ) { TypeHandlerRegistry typeHandlerRegistry = configuration. getTypeHandlerRegistry ( ) ; if ( typeHandlerRegistry. hasTypeHandler ( parameterObject. getClass ( ) ) ) { sql = sql . replaceFirst ( "\\?" , Matcher . quoteReplacement ( getParameterValue ( parameterObject) ) ) ; } else { MetaObject metaObject = configuration. newMetaObject ( parameterObject) ; for ( ParameterMapping parameterMapping : parameterMappings) { String propertyName = parameterMapping. getProperty ( ) ; if ( metaObject. hasGetter ( propertyName) ) { Object obj = metaObject. getValue ( propertyName) ; sql = sql . replaceFirst ( "\\?" , Matcher . quoteReplacement ( getParameterValue ( obj) ) ) ; } else if ( boundSql. hasAdditionalParameter ( propertyName) ) { Object obj = boundSql. getAdditionalParameter ( propertyName) ; sql = sql . replaceFirst ( "\\?" , Matcher . quoteReplacement ( getParameterValue ( obj) ) ) ; } else { sql = sql . replaceFirst ( "\\?" , "缺失" ) ; } } } } return sql ; } public static class PrintSQL { public String sql Id; public String formatSQL; }
}