Introduction to MyBatis
MyBatis is an open source project of apache. In 2010, this project was moved from apache software foundation to Google code and was renamed MyBatis. It supports excellent persistence layer frameworks for ordinary SQL queries, stored procedures and advanced mapping. MyBatis eliminates manual settings of almost all JDBC code and parameters and search for result sets. MyBatis uses simple XML or annotations for configuration and original mapping to map interfaces and Java's POJOs (Plain Old Java Objects) into records in the database.
Introduction to MyBatis Interceptor
MyBatis provides a plugin function. Although it is called a plugin, it is actually an interceptor function. MyBatis allows you to intercept calls at a certain point during the execution of a mapped statement. Mybatis provides us with an Interceptor interface, by implementing this interface, we can define our own interceptor.
MyBatis calls four types of methods by default:
1.Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClose)
2. ParameterHandler (getParameterObject, setParameters)
3.ResultSetHandler (handleResultSets, handleOutputParameters)
4.StatementHandler (prepare, parameterize, batch, update, query)
The above 4 are all Configuration methods. These methods will be executed in an operation of MyBatis (add, delete, modify, and query). The order of execution is Executor, ParameterHandler, ResultSetHandler, and StatementHandler.
The definition of an interface is:
Among these three methods, plugin is used for the construction of the processor, intercept is used for handling proxy classes, and setProperties term interceptor property settings.
The Plugin class implements the InvocationHandler interface and returns a JDK's own dynamic proxy class. For the wrap method of the plugin:
Determine whether the current target object has an interface that needs to be intercepted. If not, it returns the target object itself, and if there is, it returns a proxy object.
The InvocationHandler of the proxy object is a Plugin. When the target object executes the interface method, if it is executed through the proxy object, the invoke method of the invocationHandler will be called, which is the invoke method of the Plugin:
If the currently executed method is a defined method that needs to be intercepted, the target object, the method to be executed, and the method parameters are encapsulated into an Invocation object, and the encapsulated Invocation is passed as parameters to the intercept method of the current interceptor. If there is no need to intercept, the current method is called directly.
For getSignatureMap method
getSignatureMap will first get the Intercept annotation of the intercept, traversing it to get the type attribute, and then obtain the method with the method attribute and args attribute based on this type, and finally return a map with type key and value Set<Method>. Examples: Filtering the update method of Executor.class will produce a key as Executro and a value as a Method instance, with parameters corresponding to MappedStatement and method methods.
Interceptor configurability: