MyBatis Print SQL

Source: Internet
Author: User

The log frame used in this article is Logback

myBatis3.0.6 around the version when

When you print SQL, you only need to configure the following properties:

<logger name= "java.sql.Connection" level= "Debug"/><logger name= "java.sql.Statement" level= "Debug"/> <logger name= "java.sql.PreparedStatement" level= "DEBUG"/>

Source Code Analysis:

Preparedstatementlogger inside See this log.isdebugenabled ()
public object invoke (object proxy, Method method, object[] params) throws Throwable {    try {      if (execute_methods.co Ntains (Method.getname ())) {        if (log.isdebugenabled ()) {          log.debug ("==>  executing:" + Removebreakingwhitespace (SQL));          Log.debug ("==> Parameters:" + getparametervaluestring ());        }        Clearcolumninfo ();        if ("ExecuteQuery". Equals (Method.getname ())) {          ResultSet rs = (ResultSet) method.invoke (statement, params);          if (rs! = null) {            return resultsetlogger.newinstance (RS);          } else {            return null;          }        } else {          return Method.invoke (statement, params);        }      }

This log defines PreparedStatement.

Private static final Log log = Logfactory.getlog (Preparedstatement.class);
Around myBatis3.2.7 version

Changed the mode of print SQL, which refines the SQL print to each of the mapperstatement methods.

If you plan to have a global configuration that prints all of the SQL, you need the following configuration

Add setting Configuration to MyBatis configurations

<settings>        <setting name= "logprefix" value= "DAO." /></settings>

Then add the configuration

<logger name= "DAO" level= "DEBUG"/>

Source Code Analysis:

Connectionlogger
public object invoke (object proxy, Method method, object[] params)      throws Throwable {    try {      if (object.class. Equals (Method.getdeclaringclass ())) {        return Method.invoke (this, params);      }          if ("Preparestatement". Equals (Method.getname ())) {        if (isdebugenabled ()) {          debug ("Preparing:" + Removebreakingwhitespace ((String) params[0]), true);        }                PreparedStatement stmt = (preparedstatement) method.invoke (connection, params);        stmt = Preparedstatementlogger.newinstance (stmt, Statementlog, querystack);        return stmt;      }

One of the isdebugenabled () refers to

Protected Boolean isdebugenabled () {    return statementlog.isdebugenabled ();}

Note The statementlog here, see Simpleexecutor's Preparestatement (handler, Ms.getstatementlog ());

Public <E> list<e> Doquery (mappedstatement MS, Object parameter, rowbounds rowbounds, Resulthandler Resulthandler, Boundsql boundsql) throws SQLException {    Statement stmt = null;    Try {      Configuration configuration = Ms.getconfiguration ();      Statementhandler handler = Configuration.newstatementhandler (wrapper, MS, parameter, Rowbounds, Resulthandler, BOUNDSQL);      stmt = Preparestatement (Handler, Ms.getstatementlog ());      Return Handler.<e>query (stmt, Resulthandler);    } finally {      closestatement (stmt);    }  }

This statementlog is from Ms.getstatementlog (). and Mappedstatement's Statementlog

String logid = ID; if (configuration.getlogprefix () = null) Logid = Configuration.getlogprefix () + ID; Mappedstatement.statementlog = Logfactory.getlog (Logid);

As you can see here, Logprefix determines all the log prefixes, so just configure Logprefix.

    • This article from: Hobby Linux Technology Network
    • This article link: http://www.ahlinux.com/java/18264.html

MyBatis Print SQL

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.