Mybatis 打印完整sql

package org.apache.ibatis.logging.jdbc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.reflection.ExceptionUtil;

import com.alibaba.druid.pool.DruidPooledPreparedStatement;

public final class PreparedStatementLogger extends BaseJdbcLogger implements
		InvocationHandler {
	private PreparedStatement statement;

	private PreparedStatementLogger(PreparedStatement stmt, Log statementLog,
			int queryStack) {
		super(statementLog, queryStack);
		this.statement = stmt;
	}

	public Object invoke(Object proxy, Method method, Object[] params)
			throws Throwable {
		try {
			if (Object.class.equals(method.getDeclaringClass())) {
				return method.invoke(this, params);
			}
			if (EXECUTE_METHODS.contains(method.getName())) {
				if (isDebugEnabled()) {
					debug("Parameters: " + getParameterValueString(), true);
					/***********************反编译源文件修改内容 begin *********************************/
										/***输出带参数的完整sql语句 zhaochongli 2015/10/21 **/
					try {
						if(this.statement instanceof DruidPooledPreparedStatement){
							DruidPooledPreparedStatement b = (DruidPooledPreparedStatement) this.statement;
							String sql = b.getSql();
							String p = getParameterValueString();
							if(StringUtils.isNotBlank(p)){
								String [] pms = p.replace(" ", "").split(",");
								if(pms.length > 0){
									for(int i=0;i1){
											if(val[1].indexOf("String") != -1){
												sql = sql.replaceFirst("\\?", "'"+val[0]+"'");
											}else{
												sql = sql.replaceFirst("\\?", val[0]);
											}
										}else{
											sql = sql.replaceFirst("\\?", val[0]);
										}
									}
								}
							}
							debug("SQL: "+sql.replace("\n", "").replace("\t", ""),true);
						}
					} catch (Exception e) {
						debug("组装sql失败" + e.getMessage(),true);
					}
					/***********************反编译源文件修改内容 end *********************************/
					
				}
				clearColumnInfo();
				if ("executeQuery".equals(method.getName())) {
					ResultSet rs = (ResultSet) method.invoke(this.statement, params);
					return rs == null ? null : ResultSetLogger.newInstance(rs, this.statementLog, this.queryStack);
				}
				return method.invoke(this.statement, params);
			}
			if (SET_METHODS.contains(method.getName())) {
				if ("setNull".equals(method.getName()))
					setColumn(params[0], null);
				else {
					setColumn(params[0], params[1]);
				}
				return method.invoke(this.statement, params);
			}
			if ("getResultSet".equals(method.getName())) {
				ResultSet rs = (ResultSet) method.invoke(this.statement, params);
				return rs == null ? null : ResultSetLogger.newInstance(rs, this.statementLog, this.queryStack);
			}
			if ("getUpdateCount".equals(method.getName())) {
				int updateCount = ((Integer) method.invoke(this.statement, params)).intValue();
				if (updateCount != -1) {
					debug("   Updates: " + updateCount, false);
				}
				return Integer.valueOf(updateCount);
			}
			return method.invoke(this.statement, params);
		} catch (Throwable t) {
			throw ExceptionUtil.unwrapThrowable(t);
		}
	}

	public static PreparedStatement newInstance(PreparedStatement stmt,
			Log statementLog, int queryStack) {
		InvocationHandler handler = new PreparedStatementLogger(stmt,
				statementLog, queryStack);
		ClassLoader cl = PreparedStatement.class.getClassLoader();
		return (PreparedStatement) Proxy.newProxyInstance(cl, new Class[] {
				PreparedStatement.class, CallableStatement.class }, handler);
	}

	public PreparedStatement getPreparedStatement() {
		return this.statement;
	}
}

 

你可能感兴趣的:(LearningNotes)