English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Introduction to the implementation of Mybatis interceptors

 Introduction to MyBatis

MyBatis was originally an open source project iBatis of apache, 2010This project was migrated from apache software foundation to google code in the year, and renamed to MyBatis. It is an excellent persistence framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates almost all JDBC code and manual setting of parameters as well as retrieval of result sets. MyBatis uses simple XML or annotations for configuration and original mapping, mapping interfaces and Java's POJOs (Plain Old Java Objects, ordinary Java objects) to records in the database.

Introduction to MyBatis interceptors

MyBatis provides a plugin feature, although it is called a plugin, it is actually an interceptor feature. MyBatis allows you to intercept calls at a certain point during the execution of mapped statements. For interceptors, MyBatis provides an Interceptor interface, through which you can define your own interceptors.

MyBatis defaults to calling four types of methods:

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)

Above4All are methods of Configuration, which are executed in a MyBatis operation (addition, deletion, modification, query), and the execution order is Executor, ParameterHandler, ResultSetHandler, StatementHandler.

The definition of the interface is:

Among these three methods, plugin is used for building processors, intercept is used for proxy classes, and setProperties is used for interceptor attribute settings.
The Plugin class implements the InvocationHandler interface and returns a JDK's own dynamic proxy class. For the plugin's wrap method:

Determine whether the current target object implements the corresponding interface that needs to be intercepted. If not, return the target object itself; if so, return a proxy object.

The InvocationHandler of the proxy object is a Plugin. When the target object executes an interface method, if it is executed through the proxy object, it will call the invocationHandler's invoke method, which is the Plugin's invoke method:

If the method currently being executed is a predefined method that needs to be intercepted, then wrap the target object, the method to be executed, and the method parameters into an Invocation object, and then pass the wrapped Invocation as a parameter to the intercept method of the current interceptor. If there is no need to intercept, call the current method directly.

For the getSignatureMap method

getSignatureMap first gets the interceptor's Intercept annotation, iterates to get the type attribute, and then gets the method with the method attribute and args attribute according to this type, and finally returns a Map with type as key and Set<Method> as value. For example, filtering the update method of Executor.class will produce a key of Executro and a value of Method instance, which contains MappedStatement and method method corresponding parameters.

Configurability of interceptors:

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like