Add source Analysis-insert ()
---------------------------------------------------------------------
The public int insert (String statement, Object parameter) {//statement is the SQL statement that is passed in to invoke the XML definition, and parameter is the argument that the calling statement needs to pass in. Like
Return update (statement, parameter);
}
---------------------------------------------------------------------
Go to the Update method
public int update (String statement, Object parameter) {
try {
Dirty = true; //
Mappedstatement ms = Configuration.getmappedstatement (statement); The configuration gets the corresponding mapping statement by passing in the parameters.
Return Executor.update (MS, wrapcollection (parameter)); Executor is a proxy object
} catch (Exception e) {
Throw exceptionfactory.wrapexception ("Error updating database. Cause: "+ E, E);
} finally {
Errorcontext.instance (). reset ();
}
}
---------------------------------------------------------------
Enter into the Invoke method
public object invoke (object proxy, Method method, object[] args) throws Throwable {//parameter in the method is Executor.update
try {
Gets the set of methods that are declared [Executor.query], which is the set of methods that are defined by themselves under the MyBatis configuration file plugin node
Set<method> methods = Signaturemap.get (Method.getdeclaringclass ());
if (Methods! = NULL && Methods.contains (method)) {//Methods.contains (method) ==false
Return interceptor.intercept (new invocation (target, method, args));
}
Target is of type Cachingexecutor, which has two parameters simpleexecutor the delegate and Transactionalcachemanager types of TCM
Args is an object array, [0]=mappedstatement,[1]=employee (Parameters passed in the Update method above)
Return Method.invoke (target, args); Give target the method specified to continue executing the program
} catch (Exception e) {
Throw exceptionutil.unwrapthrowable (e);
}
}
----------------------------------------------------------------
public int Update (mappedstatement ms, Object Parameterobject) throws SQLException {
Flushcacheifrequired (MS); If there is a cache, clear.
Return Delegate.update (MS, Parameterobject);
}
-----------------------------------------------------------
public int Update (mappedstatement ms, Object parameter) throws SQLException {
Errorcontext.instance (). Resource (Ms.getresource ()). Activity ("Executing an Update"). Object (Ms.getid ());
if (closed) throw new Executorexception ("Executor was closed.");
Clearlocalcache (); Clear Local Cache
Return DoUpdate (MS, parameter);
}
---------------------------------------------------------------------------------------------------------
public int DoUpdate (mappedstatement ms, Object parameter) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = Ms.getconfiguration ();
Statementhandler handler = Configuration.newstatementhandler (this, MS, parameter, rowbounds.default, NULL, NULL); Create a new statement handler here
stmt = Preparestatement (Handler, Ms.getstatementlog ()); Statement preparation phase, access to information about the database, etc.
Return Handler.update (stmt);
} finally {
Closestatement (stmt);
}
}
------------------------------Newstatementhandler Specific implementation process------------------------------------------------------------------ -------------------------------------
Public Statementhandler Newstatementhandler (Executor Executor, mappedstatement mappedstatement, Object Parameterobject, Rowbounds
Rowbounds, Resulthandler Resulthandler, Boundsql boundsql) {
Statementhandler Statementhandler = new Routingstatementhandler (executor, mappedstatement, Parameterobject, RowBounds , Resulthandler, Boundsql);
Statementhandler = (Statementhandler) interceptorchain.pluginall (Statementhandler);
return statementhandler;
}
------------------------
Public Routingstatementhandler (Executor Executor, mappedstatement MS, Object parameter, Rowbounds rowbounds, Resulthandler Resulthandler, Boundsql boundsql) {
Switch (Ms.getstatementtype ()) {//prepared (default)
Case STATEMENT:
delegate = new Simplestatementhandler (executor, MS, parameter, Rowbounds, Resulthandler, Boundsql);
Break
Case PREPARED:
delegate = new Preparedstatementhandler (executor, MS, parameter, Rowbounds, Resulthandler, Boundsql);
Break
Case callable:
delegate = new Callablestatementhandler (executor, MS, parameter, Rowbounds, Resulthandler, Boundsql);
Break
Default
throw new Executorexception ("Unknown statement type:" + ms.getstatementtype ());
}
}
---------------------
Public Preparedstatementhandler (Executor Executor, mappedstatement mappedstatement, Object parameter, rowbounds Rowbounds, Resulthandler Resulthandler, Boundsql boundsql) {
Super (executor, mappedstatement, parameter, Rowbounds, Resulthandler, Boundsql);
}
---------------------------
Protected Basestatementhandler (Executor Executor, mappedstatement mappedstatement, Object parameterobject, Rowbounds Rowbounds, Resulthandler Resulthandler, Boundsql boundsql) {
This.configuration = Mappedstatement.getconfiguration ();
This.executor = executor;
This.mappedstatement = mappedstatement;
This.rowbounds = Rowbounds;
This.typehandlerregistry = Configuration.gettypehandlerregistry ();
This.objectfactory = Configuration.getobjectfactory ();
if (Boundsql = = null) {//Issue #435, get the key before calculating the statement
Generatekeys (Parameterobject);
Boundsql = Mappedstatement.getboundsql (Parameterobject); Get Boundsql
}
This.boundsql = Boundsql;
This.parameterhandler = Configuration.newparameterhandler (mappedstatement, Parameterobject, BOUNDSQL); Parameter processor
This.resultsethandler = Configuration.newresultsethandler (executor, mappedstatement, Rowbounds, ParameterHandler, Resulthandler, Boundsql);//Result processor
}
------------------the core code that actually performs the insertion is in the Preparedstatementhandler class------------------------
public int update (Statement Statement) throws SQLException {
PreparedStatement PS = (preparedstatement) statement;
Ps.execute (); Perform an Insert
int rows = Ps.getupdatecount ();
Object parameterobject = Boundsql.getparameterobject ();
Keygenerator keygenerator = Mappedstatement.getkeygenerator ();
Keygenerator.processafter (executor, mappedstatement, PS, parameterobject);
return rows; Returns the result of the execution
}
-----------------------------------------------------------------------------------------------------
Modify the source Code Analysis--update () and increase the operation
Delete Source Analysis--delete () with update () operation
public int Delete (String statement, Object parameter) {
Return update (statement, parameter);
}
Mybatis Source Analysis--crud