Scenario: In the process of program development, it is often necessary to track the execution of SQL statements in the program, and to print out SQL statements and corresponding parameter passes in the console to locate errors faster!
Original source: http://www.cnblogs.com/beiyeren/p/4196134.html
The log frame used here is Logback
1 different versions of Mybitis correspond to different control strategies 1.1 myBatis3.0.6 around the version
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 ()
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);
1.2 in myBatis3.2.7 version (above also can)
Only two simple steps are required!
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
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.
(GO) logback the SQL execution process in print Mybitis