---
title: "Spring mvc Mybatis使用 Logback开启日志"
author: "daoshud1"
date: "2016-10-24T16:24:45Z"
tags: []
categories: ["编程语言"]
slug: "spring-mvc-mybatis-logback"
draft: false
lastmod: "2016-10-24T16:24:45Z"
id: 2332838
url: "https://daoshud1.iteye.com/blog/2332838"
---

mybatis用logback日志不显示sql的解决方法
1.mybatis-config.xml的设定
关于logimpl的设定值还不支持logback，如果用SLF4J是不好用的。
这是官方文档的描述，见下图

![](http://dl2.iteye.com/upload/attachment/0120/8014/0d2dbeb0-b48f-389d-ad9b-7fe0417c4436.png)
设定改为STDOUT_LOGGING是可以显示sql的
修改Mybatis配置文件

```java

	<settings>
		<setting name="logImpl" value="STDOUT_LOGGING" />
	</settings>

```

2.原因是：

```java

protected Connection getConnection(Log statementLog) throws SQLException {
    Connection connection = transaction.getConnection();
    if (statementLog.isDebugEnabled()) {
      return ConnectionLogger.newInstance(connection, statementLog);
    } else {
      return connection;
    }
  }

```


 如果设定了
STDOUT_LOGGING
  实现类是StdOutImpl.java
  public boolean isDebugEnabled() {
    return true;
  }
 debug就开启了，log就可以打印sql了
3.logback.xml的设定
 
```java

   <logger name="org.apache.ibatis" level="DEBUG">
		<appender-ref ref="STDOUT"/>
	</logger>
	
	<logger name="java.sql" level="debug">
	    <appender-ref ref="STDOUT"/>
	</logger> 

```