|
@@ -15,39 +15,46 @@
|
|
|
*/
|
|
|
package com.baomidou.mybatisplus.plugins;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.entity.CountOptimize;
|
|
|
-import com.baomidou.mybatisplus.entity.GlobalConfiguration;
|
|
|
-import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
|
|
|
-import com.baomidou.mybatisplus.plugins.pagination.DialectFactory;
|
|
|
-import com.baomidou.mybatisplus.plugins.pagination.Pagination;
|
|
|
-import com.baomidou.mybatisplus.toolkit.SqlUtils;
|
|
|
-import com.baomidou.mybatisplus.toolkit.SystemClock;
|
|
|
+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.*;
|
|
|
+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 java.text.DateFormat;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Locale;
|
|
|
-import java.util.Properties;
|
|
|
+import com.baomidou.mybatisplus.entity.CountOptimize;
|
|
|
+import com.baomidou.mybatisplus.entity.GlobalConfiguration;
|
|
|
+import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
|
|
|
+import com.baomidou.mybatisplus.plugins.pagination.DialectFactory;
|
|
|
+import com.baomidou.mybatisplus.plugins.pagination.Pagination;
|
|
|
+import com.baomidou.mybatisplus.toolkit.SqlUtils;
|
|
|
+import com.baomidou.mybatisplus.toolkit.SystemClock;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
|
* 性能分析拦截器,用于输出每条 SQL 语句及其执行时间
|
|
|
* </p>
|
|
|
*
|
|
|
- * @author hubin
|
|
|
+ * @author hubin nieqiurong
|
|
|
* @Date 2016-07-07
|
|
|
*/
|
|
|
-@Intercepts({@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }),
|
|
|
+@Intercepts({
|
|
|
+ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
|
|
|
+ RowBounds.class, ResultHandler.class }),
|
|
|
@Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
|
|
|
public class PerformanceInterceptor implements Interceptor {
|
|
|
|
|
@@ -62,26 +69,24 @@ public class PerformanceInterceptor implements Interceptor {
|
|
|
*/
|
|
|
private String optimizeType = "default";
|
|
|
|
|
|
- private Properties properties;
|
|
|
-
|
|
|
public Object intercept(Invocation invocation) throws Throwable {
|
|
|
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
|
|
|
Object parameterObject = invocation.getArgs()[1];
|
|
|
RowBounds rowBounds = null;
|
|
|
Pagination pagination = null;
|
|
|
boolean isPageSql = false;
|
|
|
- if(invocation.getMethod().getName().equals("query")){
|
|
|
+ if (invocation.getMethod().getName().equals("query")) {
|
|
|
rowBounds = (RowBounds) invocation.getArgs()[2];
|
|
|
- if(rowBounds instanceof Pagination){
|
|
|
+ if (rowBounds instanceof Pagination) {
|
|
|
isPageSql = true;
|
|
|
Pagination page = (Pagination) rowBounds;
|
|
|
- pagination = new Pagination(page.getCurrent(),page.getLimit());
|
|
|
+ pagination = new Pagination(page.getCurrent(), page.getLimit());
|
|
|
}
|
|
|
}
|
|
|
BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
|
|
|
Configuration configuration = mappedStatement.getConfiguration();
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
- if(isPageSql){
|
|
|
+ if (isPageSql) {
|
|
|
Pagination page = (Pagination) rowBounds;
|
|
|
boolean orderBy = true;
|
|
|
String dbType = GlobalConfiguration.getDbType(configuration).getDb();
|
|
@@ -92,16 +97,16 @@ public class PerformanceInterceptor implements Interceptor {
|
|
|
}
|
|
|
String sql = DialectFactory.buildPaginationSql(pagination,
|
|
|
SqlUtils.concatOrderBy(boundSql.getSql(), page, orderBy), dbType, null).replaceAll("[\\s]+", " ");
|
|
|
- sqlBuilder.append(getSql(configuration,boundSql,sql));
|
|
|
- }else {
|
|
|
- sqlBuilder.append(getSql(configuration, boundSql,boundSql.getSql()));
|
|
|
+ sqlBuilder.append(getSql(configuration, boundSql, sql));
|
|
|
+ } else {
|
|
|
+ sqlBuilder.append(getSql(configuration, boundSql, boundSql.getSql()));
|
|
|
}
|
|
|
String statementId = mappedStatement.getId();
|
|
|
long start = SystemClock.now();
|
|
|
Object result = invocation.proceed();
|
|
|
long end = SystemClock.now();
|
|
|
long timing = end - start;
|
|
|
- String sql = SqlUtils.sqlFormat(sqlBuilder.toString(),format);
|
|
|
+ String sql = SqlUtils.sqlFormat(sqlBuilder.toString(), format);
|
|
|
System.err.println(" Time:" + timing + " ms" + " - ID:" + statementId + "\n Execute SQL:" + sql + "\n");
|
|
|
if (maxTime >= 1 && timing > maxTime) {
|
|
|
throw new MybatisPlusException(" The SQL execution time is too large, please optimize ! ");
|
|
@@ -109,11 +114,11 @@ public class PerformanceInterceptor implements Interceptor {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public static String getSql(Configuration configuration, BoundSql boundSql,String sql) {
|
|
|
+ public static String getSql(Configuration configuration, BoundSql boundSql, String sql) {
|
|
|
Object parameterObject = boundSql.getParameterObject();
|
|
|
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
|
|
|
sql = sql.replaceAll("[\\s]+", " ");
|
|
|
- if (parameterMappings!=null&¶meterMappings.size() > 0 && parameterObject != null) {
|
|
|
+ if (parameterMappings != null && parameterMappings.size() > 0 && parameterObject != null) {
|
|
|
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
|
|
|
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
|
|
|
sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));
|
|
@@ -137,16 +142,17 @@ public class PerformanceInterceptor implements Interceptor {
|
|
|
private static String getParameterValue(Object obj) {
|
|
|
String value;
|
|
|
if (obj instanceof String) {
|
|
|
- value = obj!=null ? "'" + obj.toString() + "'":"''";
|
|
|
+ value = obj != null ? "'" + obj.toString() + "'" : "''";
|
|
|
} else if (obj instanceof Date) {
|
|
|
- if(obj instanceof java.sql.Date){
|
|
|
- value = obj!=null ? "'" + obj.toString() + "'":"''";
|
|
|
- }else {
|
|
|
- DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
|
|
|
- value = obj!=null ? "'" + formatter.format(obj) + "'":"''";
|
|
|
+ if (obj instanceof java.sql.Date) {
|
|
|
+ value = obj != null ? "'" + obj.toString() + "'" : "''";
|
|
|
+ } else {
|
|
|
+ DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT,
|
|
|
+ Locale.CHINA);
|
|
|
+ value = obj != null ? "'" + formatter.format(obj) + "'" : "''";
|
|
|
}
|
|
|
} else {
|
|
|
- value = obj!=null ? obj.toString():"";
|
|
|
+ value = obj != null ? obj.toString() : "";
|
|
|
}
|
|
|
return value;
|
|
|
}
|
|
@@ -159,7 +165,7 @@ public class PerformanceInterceptor implements Interceptor {
|
|
|
}
|
|
|
|
|
|
public void setProperties(Properties prop) {
|
|
|
- this.properties = prop;
|
|
|
+ // TODO
|
|
|
}
|
|
|
|
|
|
public long getMaxTime() {
|