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:
<name= "Java.sql.Connection" level= "DEBUG"/> < name= "Java.sql.Statement" level= "DEBUG"/> <name= "Java.sql.PreparedStatement" level= "DEBUG" />
Source Code Analysis:
Preparedstatementlogger inside See this log.isdebugenabled ()
PublicObject Invoke (Object proxy, Method method, object[] params)throwsThrowable {Try { if(Execute_methods.contains (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) { returnresultsetlogger.newinstance (RS); } Else { return NULL; } } Else { returnMethod.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 > < name= "Logprefix" value= "DAO." /> </ Settings >
Then add the configuration
<name= "DAO" level= "DEBUG"/>
Source Code Analysis:
Connectionlogger
Publicobject Invoke (Object proxy, Method method, object[] params)throwsThrowable {Try { if(Object.class. Equals (Method.getdeclaringclass ())) { returnMethod.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); returnstmt; }
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)throwsSQLException {Statement stmt=NULL; Try{Configuration Configuration=ms.getconfiguration (); Statementhandler Handler=Configuration.newstatementhandler (Wrapper, MS, parameter, Rowbounds, Resulthandler, Boundsql); stmt=preparestatement (Handler, Ms.getstatementlog ()); returnHandler.<e>query (stmt, Resulthandler); } finally{closestatement (stmt); } }
This statementlog is from Ms.getstatementlog (). and Mappedstatement's Statementlog
String logid =ifnull) Logid = Configuration.getlogprefix () += Logfactory.getlog ( Logid);
As you can see here, Logprefix determines all the log prefixes, so just configure Logprefix.
MyBatis 3.2.7 how to print SQL