Source: fanjian0423,
fangjian0423.github.io/2014/12/15/mybatis_interceptor/
If you have a good article to submit, please click here to learn more
MyBatis Interceptor Introduction
MyBatis provides a plug-in (plugin) function, though called a plugin, but in fact it is an interceptor function. So what does the interceptor intercept in MyBatis?
Let's take a look at the official website:
MyBatis allows you to intercept calls at a point during the execution of a mapped statement. By default, MyBatis allows method calls that use plug-ins to intercept include:
Executor (update, query, Flushstatements, Commit, rollback, gettransaction, close, isClosed)
Parameterhandler (Getparameterobject, Setparameters)
Resultsethandler (Handleresultsets, Handleoutputparameters)
Statementhandler (Prepare, parameterize, batch, update, query)
We have seen some of the methods that can intercept executor interfaces, such as Update,query,commit,rollback, and some other ways of interfacing.
The overall summary is:
Ways to intercept Actuators
Handling of interception parameters
Handling of blocking result sets
Handling of blocking SQL syntax constructs
Use of interceptors
Interceptor Introduction and Configuration
First we look at the interface definition of the MyBatis interceptor:
Public interface Interceptor {
Object Intercept (invocation invocation) throws Throwable;
Object Plugin (object target);
void SetProperties (properties properties);
}
Relatively simple, there are only 3 methods. MyBatis does not have an implementation class for the Interceptor interface by default, and developers can implement interceptors that meet their needs.
An example of an interceptor on the MyBatis website below:
@Intercepts ({@Signature (
Type= Executor.class,
method = "Update",
args = {mappedstatement.class,object.class})})
public class Exampleplugin implements interceptor {
Public Object intercept (invocation invocation) throws Throwable {
return Invocation.proceed ();
}
public object Plugin (object target) {
Return Plugin.wrap (target, this);
}
public void SetProperties (properties properties) {
}
}
Global XML configuration:
<plugins>
<plugin interceptor= "Org.format.mybatis.cache.interceptor.ExamplePlugin" ></plugin>
</plugins>
This interceptor intercepts the update method of the executor interface (in fact, the new, delete, modify operation of Sqlsession), and all the update methods that execute executor are intercepted by the interceptor.
SOURCE Analysis
Let's look at the source code behind this snippet.
Start with profiling from the source--and configuration file:
Xmlconfigbuilder parsing mybatis Global configuration file Pluginelement Private method:
private void Pluginelement (xnode parent) throws Exception {
if (parent! = NULL) {
For (XNode Child:parent.getChildren ()) {
String Interceptor = Child.getstringattribute ("Interceptor");
Properties Properties = Child.getchildrenasproperties ();
Interceptor Interceptorinstance = (Interceptor) resolveclass (Interceptor). Newinstance ();
Interceptorinstance.setproperties (properties);
Configuration.addinterceptor (interceptorinstance);
}
}
}
The specific parsing code is actually relatively simple, it is not affixed, mainly through reflection instantiation of the plugin node in the Interceptor attribute representation of the class. The Addinterceptor method of the global configuration class is then called.
public void Addinterceptor (Interceptor Interceptor) {
Interceptorchain.addinterceptor (Interceptor);
}
This interceptorchain is the internal property of the configuration, the type is Interceptorchain, that is, an interceptor chain, and we look at its definition:
public class Interceptorchain {
Private final list<interceptor> interceptors = new arraylist<interceptor> ();
public object Pluginall (object target) {
for (Interceptor Interceptor:interceptors) {
target = Interceptor.plugin (target);
}
return target;
}
public void Addinterceptor (Interceptor Interceptor) {
Interceptors.add (Interceptor);
}
Public list<interceptor> getinterceptors () {
Return collections.unmodifiablelist (interceptors);
}
}