MyBatis Introduction
MyBatis is an open source project for Apache Ibatis, the project was migrated from Apache Software Foundation to Google code in 2010 and renamed MyBatis. It supports the excellent persistence layer framework for common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setting of almost all JDBC code and parameters and the retrieval of result sets. MyBatis uses simple XML or annotations for configuration and raw mapping, mapping interfaces and Java POJOs (Plain old Java Objects, normal Java objects) to records in the database.
Introduction to MyBatis Interceptor
MyBatis provides a plug-in (plugin) function, although called a plug-in, but in fact this is the interceptor function. MyBatis allows you to intercept calls at a point in the execution of a mapped statement. For interceptors MyBatis provides us with a interceptor interface that allows us to define our own interceptors by implementing the interface.
MyBatis calls four types of methods by default:
1.Executor (update, query, Flushstatements, Commit, rollback, gettransaction, close, isclosed)
2.ParameterHandler (Getparameterobject, Setparameters)
3.ResultSetHandler (Handleresultsets, Handleoutputparameters)
4.StatementHandler (Prepare, parameterize, batch, update, query)
All 4 of these are configuration methods that are executed in a mybatis operation (add, delete, modify, query), and the order of execution is Executor,parameterhandler, Resultsethandler,statementhandler.
The interface is defined as:
In these three methods, plugin is used for processor construction, intercept is used to process proxy classes, SetProperties term Interceptor property settings.
The plugin class implements the Invocationhandler interface, returns a dynamic dynamic proxy class for the JDK itself, and for the plugin wrap method:
Determines whether the current target object has an interface for implementation that needs to be intercepted, returns the target object itself if not, and returns a proxy object if there is one.
The Invocationhandler of the proxy object is exactly a plugin, and when the target object executes the interface method, if it is executed through a proxy object, the Invoke method of Invocationhandler is invoked, which is the plugin invoke method:
If the currently executing method is a well-defined method that requires blocking, the target object, the method to execute, and the method parameters are encapsulated into a invocation object, and the encapsulated invocation is passed as a parameter to the Intercept method of the current interceptor. If no interception is required, the current method is called directly.
For the Getsignaturemap method
Getsignaturemap is the first to get the interceptor this intercept annotation, traverse to get the type attribute, and then according to this type to get the method attribute and the Args property of method, and finally return a type of key,value as set <Method> map. For example, the Executor.class Update method is filtered, and the key is executro,value as an instance with the Mappedstatement and method corresponding to the parameters.
The configurable nature of the Interceptor: