MyBatis Mapper interface joins the log4j log

Source: Internet
Author: User

Configuring log4j is relatively straightforward, such as logging of this mapper interface needs to be logged:

Package Org.mybatis.example;public interface Blogmapper {@Select ("select * FROM blog WHERE id = #{id}") Blog Selectblog (int id);}

As long as a file named log4j.properties is created in the app's classpath, the specific contents of the file are 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 adding the above configuration, log4j will record the detailed execution log of the org.mybatis.example.BlogMapper and only log the error message for other classes in the application.

You can also adjust the log from the entire Mapper interface level to the statement level for finer-grained control. The following configuration records only the logs for the selectblog statement:

Log4j.logger.org.mybatis.example.blogmapper.selectblog=trace

In contrast, logging can be logged on a set of mapper interfaces, as long as the log function is turned on for the package where the Mapper interface is located:

Log4j.logger.org.mybatis.example=trace

Some queries may return a large amount of data, and just want to record the SQL statements they execute. To do this, the log level of the SQL statement in MyBatis is set to debug (fine in JDK logging), and the result log level is trace (finer in JDK logging). So, just adjust the log level to debug to achieve the goal:

Log4j.logger.org.mybatis.example=debug

What happens if the log is to be logged like 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 B Log where id = #{id} </select></mapper>

To log this file, simply add the logging function to the namespace:

Log4j.logger.org.mybatis.example.blogmapper=trace

Further, the log to record the specific statement can do this:

Log4j.logger.org.mybatis.example.blogmapper.selectblog=trace

The remainder of the configuration file log4j.properties is for the log format, which is beyond the scope of this document. For more information about log4j, refer to Log4j's website.

This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1875793

MyBatis Mapper interface joins the log4j log

Related Article

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.