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