MyBatis: prints SQL logs and mybatissql

Source: Internet
Author: User

MyBatis: prints SQL logs and mybatissql

Log4J configuration is relatively simple. For example, you need to record the logs of this mapper interface:

package org.mybatis.example;public interface BlogMapper {  @Select("SELECT * FROM blog WHERE id = #{id}")  Blog selectBlog(int id);}

 

You only need to create a name namedLog4j. propertiesFile, the specific content of the file is as follows:

# Global logging configurationlog4j.rootLogger=ERROR, stdout# MyBatis logging configuration...log4j.logger.org.mybatis.example.BlogMapper=TRACE# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

After the preceding configuration is added, Log4JOrg. mybatis. example. BlogMapperThe detailed execution log is recorded. For other classes in the application, only the error information is recorded.

You can also adjust the log level from the entire mapper interface level to the statement level to achieve more fine-grained control. The following configuration is recorded onlySelectBlogStatement log:

log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE

 

In contrast, you can record logs for a set of ER er interfaces, as long as you enable the log function for the package where the mapper interface is located:

log4j.logger.org.mybatis.example=TRACE

 

Some queries may return a large amount of data,What should I do if I only want to record the SQL statement it executes?? Therefore, the log level of SQL statements in Mybatis is setDEBUG(FINE in JDK Logging), and the result log level is TRACE (FINER in JDK Logging ). Therefore, you only need to adjust the log level to DEBUG:

log4j.logger.org.mybatis.example=DEBUG

 

What should I do if I want to record logs similar to the following mapper file instead of the mapper interface?

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.mybatis.example.BlogMapper">  <select id="selectBlog" resultType="Blog">    select * from Blog where id = #{id}  </select></mapper>

 

To record logs in this file, you only need to add the logging function to the namespace:

log4j.logger.org.mybatis.example.BlogMapper=TRACE

 

Further, you can record the logs of specific statements as follows:

log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE

 

See it. There is no difference between the two configurations!

Configuration FileLog4j. propertiesThe remaining content is for the log format, which is beyond the scope of this document. For more information about Log4J, refer to the Log4J website. However, you can give it a try to see what different effects will be produced by different configurations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.