Print SQL statements and execution results using log4j under MyBatis

Source: Internet
Author: User
Tags log4j

Reprinted from: https://www.cnblogs.com/jeevan/p/3493972.html

Originally thought very simple question, the result oneself engaged for a half day or not; Then Google, Baidu, search out a variety of methods, there are add slf4j*.jar, there is said to remove Slf4j*.jar, there is said to configure Mybatis.cfg.xml, and some even a glance seems impossible to succeed. Tried so much, the result is not a good one. May be a version inconsistency issue? But it also indicates what version is applicable. The specific reason is not known.

Later reference to the official documents, only to know, in fact, as long as the configuration Mapper interface class or configuration file mapper path is debug. Refer to here for details. It also shows that if you want to print the results of the SQL statement execution, you need to set it to the trace level.

Log4j.rootlogger=info,stdout Log4j.appender.stdout=org.apache.log4j.consoleappender  Log4j.appender.stdout.layout=org.apache.log4j.patternlayout  Log4j.appender.stdout.layout.ConversionPattern =%d%p [%c]-%m%n    # Print SQL statement: Debug; Execute result: trace## Specify Mapper in namespace configuration file

Log4j.logger.mapperNS =trace
<Mappernamespace= "Mapperns.user">    <SelectID= "Selectuser"ParameterType= "int"Resulttype= "User">          <![cdata[SELECT * from user where id = #{id}]]>    </Select></Mapper>  

Print results

2013-12-27 13:16:56,335 DEBUG [mapperNS.user.selectUser]-ooo Using Connection [email protected]]2013-12-27 13:16:56,344 DEBUG [MapperNS.user.selectUser]-==> preparing:select * FROM user where id =? 2013-12-27 13:16:56,408 DEBUG [MapperNS.user.selectUser]-==> parameters:1 (String) 2013-12-27 13:16:56,430 TRACE [ MapperNS.user.selectUser]-<= = Columns:ID, name, sex, degree, birthdaty2013-12-27 13:16:56,436 TRACE [MapperNS.user.selectUser]-<==        Row:1, Wang, 1, 1.00, 1983-08-012013-12-27 13:16:56,437 DEBUG [MapperNS.user.selectUser]-<==      total:12013-12-27 13:16:56,439 INFO [action. Useraction]-user [ID=1,name=wang,Sex=1,Birthday=null]

There's one more question to keep in check.

Log4j:warn No Appenders could is found for logger (org.apache.ibatis.logging.LogFactory). Log4j:warn Please initialize the log4j system Properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

In this case, the log is not printed, stating that the app does not initialize the log4j. The workaround is to do so when the app starts

The path root directory here refers to the application's root directory, not the classes directory Propertyconfigurator.configure ("Bin/conf/log4j.properties");

Print SQL statements and execution results using log4j under MyBatis

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.