Tracking SQL statements with log4j in MYBATIS3

Source: Internet
Author: User
Tags log4j

Printing SQL statements should be a useful feature during development.

The following describes how to turn on the ability to print SQL statements in Mybatis3.

MyBatis the built-in log factory provides logging capabilities, specific log implementations are available in the following ways:

    • Slf4j
    • Apache Commons Logging
    • Log4j 2
    • Log4j
    • JDK Logging
Select which log implementation is determined by MyBatis's built-in log factory. It will use the first found (find in the order listed above). If none is found, the log function is disabled

In addition MyBatis official online said many environments ship Commons Logging as a part of the application Server Classpath (good examples include T Omcat and WebSphere).

But in fact, Tomcat classpath under not commons-logging package, in fact, even if the project used to, it can also combine log4j good work.

So, let's get started!

1, import log4j package, here is the log4j-1.2.17

2, configuration log4j.properties (file placed under Classpath)

The following configuration will output all SQL statements

Log4j.rootlogger=error, consolelog4j.logger.com.zhuchao.test.dao=debug#consolelog4j.appender.console= org.apache.log4j.consoleappenderlog4j.appender.console.layout= org.apache.log4j.patternlayoutlog4j.appender.console.layout.conversionpattern=%d [%t]%-5p [%c]-%m%n

Where Com.zhuchao.test.dao is the package name where the Mapper class is located, if you want to specify a mapper output SQL statement

You can also implement finer-grained configurations, such as Log4j.logger.com.zhuchao.test.dao.personmapper=debug

A little bit more, you can also specify a specific SQL statement, such as Log4j.logger.com.zhuchao.test.dao.personmapper.selectbyid=debug

If you want to log records of mapper.xml files, you just need to namespace.

The Mapper.xml and Mapper classes in the general project are one by one corresponding, so both configurations are the same.

It is worth noting that the debug log level only prints SQL statements, parameters, and the number of records affected

2014-09-14 00:49:10,078 [http-8080-3] DEBUG [com.zhuchao.test.BasecodeMapper.selectByExample]-==>  Preparing : SELECT * from Basecode WHERE (cls =?) 2014-09-14 00:49:10,078 [http-8080-3] DEBUG [com.zhuchao.test.BasecodeMapper.se Lectbyexample]-==> parameters:level (String) 2014-09-14 00:49:10,079 [http-8080-3] DEBUG [ Com.zhuchao.test.BasecodeMapper.selectByExample]-<==      total:2

If you also want to see all the execution results, you can set the log level to trace:

2014-09-14 00:49:10,078 [http-8080-3] DEBUG [com.zhuchao.test.BasecodeMapper.selectByExample]-==>  Preparing : SELECT * from Basecode WHERE (cls =?) 2014-09-14 00:49:10,078 [http-8080-3] DEBUG [com.zhuchao.test.BasecodeMapper.se Lectbyexample]-==> parameters:level (String) 2014-09-14 00:49:10,079 [http-8080-3] TRACE [ Com.zhuchao.test.BasecodeMapper.selectByExample]-<==    columns:cls, ID, code, name, description, seq2014-09-14 00:49:10,079 [http-8080-3] TRACE [com.zhuchao.test.BasecodeMapper.selectByExample]-<==        row:level, 1, Level2, Level 2, Level 2, 02014-09-14 00:49:10,079 [http-8080-3] TRACE [com.zhuchao.test.BasecodeMapper.selectByExample]-<==        Row:level, 2, Level1, Level 1, Level 1, 02014-09-14 00:49:10,079 [http-8080-3] DEBUG [ Com.zhuchao.test.BasecodeMapper.selectByExample]-<==      total:2

Tracking SQL statements with log4j in MYBATIS3

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.