Mybatis查看完整SQL及执行性能
2017-08-30 14:42
253 查看
能不能直接把?参数拼接完了以后SQL给我?
在开发的过程中,数据不对的时候,我们会去日志中找到 具体的sql 执行语句但是往往是这样的
2017-08-30-14-35-23 [DEBUG] [main] [selectRecommendGoodsForStoreApp_132] - ==> Preparing: SELECT sk.SKU_ID AS skuId, sk.SKU_IMG_PATH AS skuImgPath, sk.SKU_NAME AS skuName, sk.SKU_NORMS AS skuNorms, sk.QUALITY_PERIOD AS qualityPeriod, sk.SKU_PACKAGE_NUM_1 as skuPackageNum1, sk.SKU_PACKAGE_NUM_2 as skuPackageNum2, sk.SKU_PACKAGE_NUM_3 as skuPackageNum3, sk.SKU_PACKAGE_UNIT_1 as skuPackageUnit1, sk.SKU_PACKAGE_UNIT_2 as skuPackageUnit2, sk.SKU_PACKAGE_UNIT_3 as skuPackageUnit3, sh.SELL_PRICE AS sellPrice, sh.USER_ID as supplierId, sh.SHELF_ID as shelfId, sh.WAREHOUSE_ID as warehouseId, sh.PROMOTION_PRICE as promotionPrice FROM ess_sku_recommend AS re LEFT JOIN ess_sku_custom as sk ON sk.SKU_ID=re.SKU_ID LEFT JOIN ess_shelf_info as sh ON sh.SKU_ID = sk.SKU_ID WHERE 1=1 and sh.WAREHOUSE_ID in ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ) group by sk.sku_id
2017-08-30-14-35-23 [DEBUG] [main] [selectRecommendGoodsForStoreApp_132] - ==> Parameters: 147(String), a07bfd2e052b4513b308e969e4cc6eea(String), W10000001(String), W10000002(String), W10000003(String), W10000006(String), W10000007(String), W11708261601220001(String), W11708281000390002(String), W11708281001220003(String), W11708281001540004(String), W11708281506390001(String), Wsdfsdf(String)
看到那一堆?????? 了吗?
上面的 代码 是 带有? 的 预执行 sql 语句,现在好了,你看看第二个代码块里的 具体参数吧,你 不觉得 一个一个 的 去 替换然后 查询,然后看结果。。。。。 这个过程很反人类吗?
你只需要这段代码
下面这个是一个 mybatis plugin,放到你的 项目当中,然后在 mybatis 的配置文件中 加上 这个 plugin。至于如何加plugin ,这个根据 mybatis 版本不同,以及 具体的配置规则不一样
请大家自行适配吧。
这个功能加上去之后
1.可以打印出最后执行的SQL(?已经给你替换成具体的参数了)
2.记录这个SQL执行的时间。这一点对于性能调优 还是很有帮助了
import java.text.DateFormat; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Properties; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ParameterMapping; 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; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.type.TypeHandlerRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @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 }) }) public class SqlTimeInterceptor implements Interceptor { @SuppressWarnings("unused") private Properties properties; private static Logger log = LoggerFactory.getLogger(SqlTimeInterceptor.class); public Object intercept(Invocation invocation) throws Throwable { MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } String sqlId = mappedStatement.getId(); BoundSql boundSql = mappedStatement.getBoundSql(parameter); Configuration configuration = mappedStatement.getConfiguration(); Object returnValue = null; long start = System.currentTimeMillis(); returnValue = invocation.proceed(); long end = System.currentTimeMillis(); long time = (end - start); if (time > 1) { String sql = getSql(configuration, boundSql, sqlId, time); log.info(sql); } return returnValue; } public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time) { String sql = showSql(configuration, boundSql); StringBuilder str = new StringBuilder(100); str.append(sqlId); str.append(":"); str.append(sql); str.append(": 执行耗时"); str.append(time); str.append("ms"); return str.toString(); } 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, Locale.CHINA); value = "'" + formatter.format(new Date()) + "'"; } else { if (obj != null) { value = obj.toString(); } else { value = ""; } } return value; } public static String showSql(Configuration configuration, BoundSql boundSql) { Object parameterObject = boundSql.getParameterObject(); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); String sql = boundSql.getSql().replaceAll("[\\s]+", " "); if (parameterMappings.size() > 0 && parameterObject != null) { 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; } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties0) { this.properties = properties0; } }
装逼永无止境 P.S 别太浪
相关文章推荐
- 查看sql语句执行时间/测试sql语句性能
- Oracle 如何根据动态性能视图来查看某条SQL语句的执行计划
- 查看sql语句执行时间/测试sql语句性能
- 查看sql语句执行时间/测试sql语句性能
- 查看sql语句执行时间或测试sql语句性能
- Java日志框架——查看“完整的执行的SQL语句”
- 查看sql语句执行时间/测试sql语句性能
- 查看sql语句执行时间/测试sql语句性能
- MyBatis源码分析-SQL语句执行的完整流程
- 配置log4j查看mybatis中执行的sql语句
- 查看sql语句执行时间/测试sql语句性能
- MyBatis源码分析-SQL语句执行的完整流程
- 查看SQL语句执行时间/测试性能
- PostgreSQL SQL的性能调试方法3--查看执行计划
- 查看sql语句执行时间/测试sql语句性能
- sql server性能分析--查看当前执行的SQL
- 查看sql语句执行时间/测试sql语句性能
- SQL: 查看语句执行时间/测试sql语句性能
- 查看sql语句执行时间/测试sql语句性能
- 查看sql语句执行时间/测试sql语句性能