mybatis 攔截器打印完整sql實現

mybatis 打印完整sql實現


package com.test.interceptor;

import java.lang.reflect.Field;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
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.scripting.defaults.DefaultParameterHandler;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.util.ReflectionUtils;

/**
 * 
 * @ClassName: MybatisSqlPrintIntercepter
 * @Description: sql 打印攔截器
 * @author eno
 * @date :2019年12月2日 上午9:09:54
 */
@Intercepts({
		@Signature(type = StatementHandler.class, method = "query", args = { Statement.class, ResultHandler.class }),
		@Signature(type = StatementHandler.class, method = "update", args = { Statement.class }),
		@Signature(type = StatementHandler.class, method = "batch", args = { Statement.class }) })
public class MybatisSqlPrintIntercepter implements Interceptor, Ordered {
	private static final Logger logger = LoggerFactory.getLogger(MybatisSqlPrintIntercepter.class);
	private Configuration configuration = null;

	private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		}
	};

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		Object target = invocation.getTarget();
		long startTime = System.currentTimeMillis();
		try {
			return invocation.proceed();
		} finally {
			try {
				long endTime = System.currentTimeMillis();
				long sqlCost = endTime - startTime;
				StatementHandler statementHandler = (StatementHandler) target;
				BoundSql boundSql = statementHandler.getBoundSql();
				if (configuration == null) {
					final DefaultParameterHandler parameterHandler = (DefaultParameterHandler) statementHandler
							.getParameterHandler();
					Field configurationField = ReflectionUtils.findField(parameterHandler.getClass(), "configuration");
					ReflectionUtils.makeAccessible(configurationField);
					this.configuration = (Configuration) configurationField.get(parameterHandler);
				}
				// 替換參數格式化Sql語句,去除換行符
				String sql = formatSql(boundSql, configuration).concat(";");
				String 	warning = "";
				if(sqlCost > 100) {
					warning  = "[耗時過長]";
				}
				if(logger.isDebugEnabled()) {
					logger.debug("SQL==> {}    執行耗時:{} ms {}", sql, sqlCost,warning);
				}
			} catch (Exception e) {
				logger.error("==> 打印sql 日誌異常 {}",e);
			}
		}
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {

	}

	/**
	 * 獲取完整的sql實體的信息
	 *
	 * @param boundSql
	 * @return
	 */
	private String formatSql(BoundSql boundSql, Configuration configuration) {
		String sql = boundSql.getSql();
		List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
		Object parameterObject = boundSql.getParameterObject();
		// 輸入sql字符串空判斷
		if (StringUtils.isBlank(sql)) {
			return "";
		}
		if (configuration == null) {
			return "";
		}
		TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
		sql = beautifySql(sql);
		// 參考mybatis 源碼 DefaultParameterHandler
		if (parameterMappings != null) {
			for (ParameterMapping parameterMapping : parameterMappings) {
				if (parameterMapping.getMode() != ParameterMode.OUT) {
					Object value;
					String propertyName = parameterMapping.getProperty();
					if (boundSql.hasAdditionalParameter(propertyName)) {
						value = boundSql.getAdditionalParameter(propertyName);
					} else if (parameterObject == null) {
						value = null;
					} else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
						value = parameterObject;
					} else {
						MetaObject metaObject = configuration.newMetaObject(parameterObject);
						value = metaObject.getValue(propertyName);
					}
					String paramValueStr = "";
					if (value instanceof String) {
						paramValueStr = "'" + value + "'";
					} else if (value instanceof Date) {
						paramValueStr = "'" + DATE_FORMAT_THREAD_LOCAL.get().format(value) + "'";
					} else {
						paramValueStr = value + "";
					}
					sql = sql.replaceFirst("\\?", paramValueStr);
				}
			}
		}
		return sql;
	}

	private String beautifySql(String sql) {
		sql = sql.replaceAll("[\\s\n ]+", " ");
		return sql;
	}

	@Override
	public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE;
	}

}

2.添加攔截器

/**
	 * 完整sql 打印 攔截器配置
	 * @return
	 */
	@Bean
	@ConditionalOnExpression("${mybatis.sql.print:true}")
	public MybatisSqlPrintIntercepter mybatisSqlPrintIntercepter() {
		return new MybatisSqlPrintIntercepter();
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章