How to enable logging of query in MyBatis?

To enable the logging of MyBatis queries, you can achieve this by configuring MyBatis’s logging implementation class. One common way to do this is as follows:

  1. configuration file for MyBatis
<configuration>
    <!-- 其他配置 -->
    
    <!-- 开启日志 -->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
</configuration>
  1. Add the corresponding logging framework dependencies to the project, such as using Logback as the logging framework.
<dependencies>
    <!-- 其他依赖 -->
    
    <!-- Logback日志框架依赖 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>
  1. The logback configuration file is named logback.xml.
<configuration>
    <!-- 其他配置 -->
    
    <!-- 配置MyBatis日志输出 -->
    <logger name="org.mybatis" level="DEBUG" />

    <!-- 其他日志配置 -->
</configuration>

After this configuration, MyBatis will print the query SQL statements and related log information to a log file or the console.

bannerAds