Mybatis查看完整SQL及执行性能
来源:互联网 发布:c语言闰年判断 编辑:程序博客网 时间:2024/06/10 01:51
能不能直接把?参数拼接完了以后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 别太浪
阅读全文
0 0
- Mybatis查看完整SQL及执行性能
- MyBatis源码分析-SQL语句执行的完整流程
- MyBatis源码分析-SQL语句执行的完整流程
- mybatis源码分析,sql语句执行的完整流程
- MyBatis执行动态sql及存储过程
- sql server性能分析--查看当前执行的SQL
- SQL语句执行效率及性能测试
- SQL语句执行效率及性能测试
- SQL语句执行效率及性能测试
- SQL语句执行效率及性能测试
- sql执行顺序及性能优化
- 配置log4j查看mybatis中执行的sql语句
- PostgreSQL SQL的性能调试方法3--查看执行计划
- PostgreSQL SQL的性能调试方法3--查看执行计划
- PostgreSQL SQL的性能调试方法 查看执行计划
- MySQL数据库性能优化-利用sql执行计划预先查看执行性能(五)
- MyBatis直接执行SQL查询及批量插入数据
- MyBatis直接执行SQL查询及批量插入数据
- Array对象的方法实现(4)----Array.prototype.findIndex和Array.prototype.forEach(实现常规参数的功能)
- shell输入/输出重定向
- Cygwin粘贴复制
- hadoop端口
- xml的layout_weight计算原理
- Mybatis查看完整SQL及执行性能
- LNMP部署网站 访问php文件 404
- 19.Redis Cluster
- 计算机组成原理 笔试题
- Spring Data Jpa 详解 (配置篇)
- Maven的Pom.xml详解
- 垃圾收集器及对应算法总结
- mysql基本命令
- selenium 部分关键字用法