Java mybatis interceptor
Many java beginners don't know much about the mybatis interceptor. Here I'll sort out the next detailed explanation of the mybatis interceptor in Java,
This paper mainly analyzes the plug-in mechanism of mybatis, which is actually the implementation of responsibility chain mode of Java Dynamic agent implementation.
According to official documents. Mybatis only allows the following methods to be intercepted, which decides to write the interceptor annotation signature parameter.
The code is as follows
The source code of interception processing is as follows, in which interceptorchain pluginAll(..) This is the weaving custom Interceptor:
The code is as follows
To implement a custom interceptor, you only need to implement the interceptor interface. The approximate code is as follows:
The code is as follows
Among them, the code to intercept calls is in plugin In Wrap:
The code is as follows
You can see that the core code of mybatis interceptor design is still relatively simple, but flexible enough. In actual use, pay attention not to be a meaningless agent (plugin. Wrap).