MyBatis-Plus provides convenient configurations to print SQL query statements for debugging and performance analysis. You can configure log to output SQL statements and execution parameters.
Method 1: Print SQL via or config
The SQL printing function of MyBatis-Plus can be enabled through configuration or file.
1.1 Configure in
# Enable MyBatis-Plus SQL Print Log-impl=
This configuration uses StdOutImpl to print SQL to the console.
1.2 Configure in
mybatis-plus: configuration: log-impl:
Method 2: Customize SQL log output method
MyBatis-Plus uses MyBatis' logging framework, so you can control the log output by configuring the log implementation of MyBatis. MyBatis provides a variety of log implementations, and the output method can be selected through configuration.
Common log implementations include:
SLF4J(recommend) CommonsLogging Log4j Log4j2 JDKLogging(Java Default log)
If you are using SLF4J, you can control the output of SQL through configuration.
Method 3: Configure MyBatis-Plus through code
If you want to configure SQL printing in your code instead of relying on configuration files, you can explicitly set up the log factory in your MyBatis configuration.
3.1 Configuring SqlSessionFactory and Logging Components
In Java configuration, you can configure the log implementation class for MyBatis:
@Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean(); (dataSource); // Set the log implementation class of MyBatis-Plus .useSlf4jLogging(); return (); }
Method 4: Incorporate the logging framework (recommended to use SLF4J + Logback)
Combining SLF4J and Logback to print SQL logs can provide stronger log management capabilities. Here is an example:
4.1 Configuration
First, you need to set the log level to DEBUG in the configuration file and define the log output format:
<configuration> <!-- Set log level --> <logger name="" level="DEBUG" /> <logger name="" level="DEBUG" /> <appender name="stdout" class=""> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="stdout" /> </root> </configuration> `` [Something went wrong, please try again later.]
Summarize
This is the end of this article about the four methods of printing sql configuration for mybatisPlus. For more related content for printing sql configuration for mybatisPlus, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!