07mybatis使用日志框架logback显示sql语句

java 开发领域日志框架
log4j
log for java

logback
来显示mybatis sql语句

1、在mybatis框架配置文件中 src/main/resource/mybatis-config.xml 加入
<settings>
<setting name="logPrefix" value="dao."/>
</settings>

加入效果如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<settings>
<setting name="logPrefix" value="dao."/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 配置接口 -->
<!--<mapper class="com.fz.mapper.BookMapper"/>-->

<!-- 映射配置文件 -->
<!--<mapper resource="com/fz/mapper/BookMapper.xml"/>-->

<!-- 指定mapper接口所在的包 -->
<package name="com.fz.mapper"/>
</mappers>
</configuration>

2、maven项目pom.xml文件加入logback依赖包
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>


3、logback默认配置文件在 src/main/resources/logback.xml
<?xml version="1.0"?>
<configuration>
<!-- ch.qos.logback.core.ConsoleAppender 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- 日志级别 -->
<root>
<level value="error" />
<appender-ref ref="console" />
</root>
<logger name="dao" level="DEBUG"/>
</configuration>

4、maven 读取资源文件配置文件 pom.xml
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>

 

posted @ 2017-06-20 19:32  归然  阅读(3987)  评论(0编辑  收藏  举报