In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the execution order of multiple plugins in mybatis? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. Preface
On the official website of mybatis, there are plug-ins that say that if mybatis plugins has multiple plug-ins at the same time, what is the order in which they are executed?
Second, preparatory work, code preparation
1. Project structure
2 、 TestDAO
Public interface TestDAO {Test selectById (Integer id); default void testDefaultMethod () {System.out.println ("= calls the default method in the interface to verify the isDefaultMethod method in MapperProxy =");}}
3 、 Test
@ Data@NoArgsConstructor@AllArgsConstructorpublic class Test {private Integer id; private String name;}
4 、 ExamplePlugin
@ Intercepts ({@ Signature (type= Executor.class, method = "query", args= {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @ Signature (type= ParameterHandler.class, method = "setParameters", args= {PreparedStatement.class}), @ Signature (type= ResultSetHandler.class, method = "handleResultSets", args= {Statement.class}) @ Signature (type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})}) public class ExamplePlugin implements Interceptor {@ Override public Object intercept (Invocation invocation) throws Throwable {System.out.println ("+ invocation.getMethod (). GetName () +") Return invocation.proceed (); @ Override public Object plugin (Object target) {return Plugin.wrap (target, this);} @ Override public void setProperties (Properties properties) {}}
5 、 SecondExamplePlugin
@ Intercepts ({@ Signature (type= Executor.class, method = "query", args= {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), @ Signature (type= ParameterHandler.class, method = "setParameters", args= {PreparedStatement.class}), @ Signature (type= ResultSetHandler.class, method = "handleResultSets", args= {Statement.class}) @ Signature (type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})}) public class SecondExamplePlugin implements Interceptor {@ Override public Object intercept (Invocation invocation) throws Throwable {System.out.println ("+ invocation.getMethod (). GetName () +") Return invocation.proceed (); @ Override public Object plugin (Object target) {return Plugin.wrap (target, this);} @ Override public void setProperties (Properties properties) {}}
6 、 Main
Public class Main {public static SqlSession getSqlSession () throws IOException {InputStream inputStream = Resources.getResourceAsStream ("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (inputStream); return sqlSessionFactory.openSession ();} public static void main (String [] args) throws IOException {TestDAO testDAO = getSqlSession (). GetMapper (TestDAO.class); Test test = testDAO.selectById (1); / / testDAO.testDefaultMethod () / / Class files are cached in the java virtual machine. We print the class files to the file for viewing / / generateProxyFile ("F:/TestDAOProxy.class");} private static void generateProxyFile (String path) {byte [] classFile = ProxyGenerator.generateProxyClass ("$Proxy0", new Class [] {TestDAO.class}); try (FileOutputStream fos = new FileOutputStream (path)) {fos.write (classFile) Fos.flush (); System.out.println ("proxy class class file written successfully");} catch (Exception e) {System.out.println ("write file error");}
7 、 TestMapper.xml
Id, name SELECT FROM test WHERE id = # {id}
8 、 mybatis-confi.xml
9 、 POM
4.0.0 com.me mybatis-test 1.0-SNAPSHOT 1.8 1.8 org.mybatis mybatis 3.4.6 org.projectlombok lombok 1.16.14 junit Junit 4.12 mysql mysql-connector-java 8.0.12 III. Begin to explore
1. Running result
SecondExamplePlugin starts to stir up trouble: query
ExamplePlugin starts to stir up trouble: query
SecondExamplePlugin starts to stir up trouble: prepare
ExamplePlugin starts to stir up trouble: prepare
SecondExamplePlugin starts to stir up trouble: setParameters
ExamplePlugin starts to stir up trouble: setParameters
SecondExamplePlugin starts to stir up trouble: handleResultSets
ExamplePlugin starts to stir up trouble: handleResultSets
Question: why is it in this order?
Contrary to our order in the mybatis-config.xml file, why?
3. Comment out one. Let's start with a plugin and debug to see what has been done.
4. As shown in the figure, enter breakpoints in the four methods of Configuration: newParameterHandler, newResultSetHandler, newStatementHandler, and newExecutor.
5. Main method of debug Main class
6. We found that in newExecutor, we were stopped
What is the interceptorChain here? We looked up and found that it was new in the Configuration class. It is equivalent to the
7. We already know what interceptorChain is, so enter its pluginAll method
We can see that it is the plugin method that traverses the interceptors. And interceptors is ArrayList, which is ordered. So in the configuration file, which plugin comes first, here it comes first.
8. Enter the plugin method of interceptor and find that we have come to the plugin method of the ExamplePlugin class written by ourselves.
9. It continues to call Plugin's static method wrap
1) the first step is to get the type and method in the @ Signature annotation, which is the annotation we used in ExamplePlugin.
2) the second step is to generate a proxy class using a dynamic proxy. Where Plugin is used as InvocationHandler
10. UML diagram
Eventually, Executor is no longer the original class, but its proxy class. The flow of the newStatementHandler method and the newResultSetHandler method is similar, and eventually the proxy class is generated.
When Executor, StatementHandler, ParameterHandler, and ResultSetHandler execute their own methods, they actually call the invoke method in their proxy class Plugin.
That is, in interceptor.intercept (new Invocation (target, method, args)); this sentence goes back to our ExamplePlugin's intercept method.
The agent for Executor throughout the process. (here only Executor is used as an example.)
IV. Conclusion
It's just an agent. Remember pluginAll?
What about multiple interceptor? Of course, the proxy class is proxied again.
So, the latter will represent the front, which is why the SecondExamplePlugin executes first-- the outer layer, the first execution.
The execution order of multiple plug-ins is clear, so what about the execution order of the methods in the plug-in?
Of course, it depends on when these methods are called.
After reading the above, have you mastered the execution order of multiple plugins in mybatis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.