关于mybatis无法输出sql语句的问题

最近有人在论坛发帖说mybatis使用log4j无法打印sql语句,研究了,给出以下解决方案。


在mybatis的配置文件内加入如下设置:

<configuration>
<settings><setting name="logImpl" value="LOG4J"/></settings>

……
</configuration> 

告诉mybatis用log4j日志输出。


建立一个log4j.xml文件,内容如下:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">



	<appender name="appender" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="D:/logs/debug.log" />
		<param name="Append" value="true" />
		<param name="threshold" value="DEBUG" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.txt'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[xxoo] %p [%t] %c{1}.%M(%L) | %m%n" />
		</layout>
	</appender>
	<logger name="com.ibatis" additivity="true">

		<level value="debug" />

	</logger>

	<logger name="java.sql.Connection" additivity="true">

		<level value="debug" />

	</logger>

	<logger name="java.sql.Statement" additivity="true">

		<level value="debug" />

	</logger>

	<logger name="java.sql.PreparedStatement" additivity="true">

		<level value="debug" />

	</logger>

	<logger name="java.sql.ResultSet" additivity="true">

		<level value="debug" />

	</logger>

	<root>
		<priority value="debug" />
		<appender-ref ref="appender" />
	</root>
</log4j:configuration>



注意root级别改为debug。


如果你去看mybatis源码,你会发现,mybatis的logfactory如下:


public final class LogFactory
{
  public static final String MARKER = "MYBATIS";
  private static Constructor<? extends Log> logConstructor;

  public static Log getLog(Class<?> aClass)
  {
    return getLog(aClass.getName());
  }

  public static Log getLog(String logger) {
    try {
      return (Log)logConstructor.newInstance(new Object[] { logger }); } catch (Throwable t) {
    }
    throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
  }

  public static synchronized void useCustomLogging(Class<? extends Log> clazz)
  {
    setImplementation(clazz);
  }

  public static synchronized void useSlf4jLogging() {
    setImplementation(Slf4jImpl.class);
  }

  public static synchronized void useCommonsLogging() {
    setImplementation(JakartaCommonsLoggingImpl.class);
  }

  public static synchronized void useLog4JLogging() {
    setImplementation(Log4jImpl.class);
  }

  public static synchronized void useJdkLogging() {
    setImplementation(Jdk14LoggingImpl.class);
  }

  public static synchronized void useStdOutLogging() {
    setImplementation(StdOutImpl.class);
  }

  public static synchronized void useNoLogging() {
    setImplementation(NoLoggingImpl.class);
  }

  private static void tryImplementation(Runnable runnable) {
    if (logConstructor == null)
      try {
        runnable.run();
      }
      catch (Throwable t)
      {
      }
  }

  private static void setImplementation(Class<? extends Log> implClass) {
    try {
      Constructor candidate = implClass.getConstructor(new Class[] { String.class });
      Log log = (Log)candidate.newInstance(new Object[] { LogFactory.class.getName() });
      log.debug("Logging initialized using '" + implClass + "' adapter.");
      logConstructor = candidate;
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    }
  }

  static
  {
    tryImplementation(new Runnable() {
      public void run() {
        LogFactory.useSlf4jLogging();
      }
    });
    tryImplementation(new Runnable() {
      public void run() {
        LogFactory.useCommonsLogging();
      }
    });
    tryImplementation(new Runnable() {
      public void run() {
        LogFactory.useLog4JLogging();
      }
    });
    tryImplementation(new Runnable() {
      public void run() {
        LogFactory.useJdkLogging();
      }
    });
    tryImplementation(new Runnable() {
      public void run() {
        LogFactory.useNoLogging();
      }
    });
  }
}


它支持各种日志,但是你要开启。

你可能感兴趣的:(mybatis,打印sql语句)