关于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();
      }
    });
  }
}


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一个优秀的持久层框架,可以将数据库的操作变得非常简单和高效。在使用MyBatis执行SQL语句时,我们可以通过配置来开启或关闭MyBatis的日志记录功能,从而实现原样输出SQL执行语句。 要想在控制台输出MyBatisSQL执行语句,我们可以通过在配置文件中配置`logImpl`属性为"log4j"、"java.util.logging"或"SLF4J"等日志实现类的全限定名,来实现日志的输出。 例如,如果我们使用log4j作为日志记录的实现,我们需要在配置文件中添加如下配置: ``` <configuration> <properties> <!-- 其他配置 --> <property name="mybatis.log.impl" value="org.apache.ibatis.logging.log4j.Log4jImpl"/> </properties> <!-- 其他配置 --> </configuration> ``` 配置完成后,MyBatis将使用log4j来进行日志记录,并将SQL执行语句原样输出到控制台或日志文件中,方便我们进行调试和优化。 当我们执行具体的SQL语句时,MyBatis会在日志中输出类似下面的信息: ``` DEBUG [main] statement - ==> Preparing: SELECT * FROM user WHERE id = ? DEBUG [main] statement - ==> Parameters: 1(Integer) ``` 可以看到,MyBatis首先会输出"Preparing",表示正在准备执行SQL语句;然后会输出"Parameters",表示SQL语句中的参数值。 通过这种方式,我们可以清晰地看到MyBatis执行的SQL语句及参数值,方便我们进行调试和排查问题。同时,MyBatis还提供了其他一些配置项,可以进一步调整日志的输出方式和级别,以满足我们的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值