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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
@Intercepts({ @Signature( type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}), @Signature( type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) }) @Component public class MybatisInterceptor implements Interceptor {
private static Logger logger = LoggerFactory.getLogger(MybatisInterceptor.class);
private static Properties properties;
private static String getParameterValue(Object param) { String value; if (param == null) { value = ""; } else { value = param.toString(); if (param instanceof String) { value = "'" + value + "'"; } else if (param instanceof CodeDescEnum) { value = ((CodeDescEnum) param).getCode().toString(); } } return Matcher.quoteReplacement(value); }
private static String showSql(Configuration configuration, BoundSql boundSql) { String sql = boundSql.getSql().replaceAll("[\\s]+", " "); Object parameterObject = boundSql.getParameterObject(); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (!StringUtils.isEmpty(sql) && Objects.nonNull(parameterObject) && !CollectionUtils.isEmpty(parameterMappings)) { TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { sql = sql.replaceFirst("\\?", 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("\\?", getParameterValue(obj)); } else if (boundSql.hasAdditionalParameter(propertyName)) { Object obj = boundSql.getAdditionalParameter(propertyName); sql = sql.replaceFirst("\\?", getParameterValue(obj)); } } } } return sql; }
@Override public Object plugin(Object target) { return Plugin.wrap(target, this); }
@Override public void setProperties(Properties properties) { MybatisInterceptor.properties = properties; }
@Override public Object intercept(Invocation invocation) throws Throwable {
MybatisFilter.enable(); MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; }
BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration();
long start = System.currentTimeMillis(); Object returnValue = null; Exception sqlException = null; try { returnValue = invocation.proceed(); } catch (Exception e) { MybatisFilter.disable(); sqlException = e; } long end = System.currentTimeMillis(); long time = (end - start); Log statementLog = mappedStatement.getStatementLog(); try { int size = -1; if (returnValue != null) { if (returnValue instanceof Collection) { size = ((Collection) returnValue).size(); } else if (returnValue instanceof Number) { size = ((Number) returnValue).intValue(); } else { logger.error("处理sql结果出错, returnValue类型为 {}", returnValue.getClass().getName()); } } String sql = showSql(configuration, boundSql); statementLog.debug(String.format("sql执行耗时: %dms, 结果数: %d, %s", time, size, sql)); } catch (Exception e) { MybatisFilter.disable(); statementLog.error(String.format("[LogHub]mybatis拦截器格式化sql出错, 调用位置%s, sql模板: %s", mappedStatement.getId(), boundSql.getSql()), e); } if (sqlException != null) { throw sqlException; } return returnValue; } }
|