Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Mybatis third-party PageHelper paging plug-in

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the third-party PageHelper paging plug-in for Mybatis. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Usage

CommentAnalyses is now a Page object (defined in the PageHelper plug-in package)

The Page object inherits from ArrayList in JDK and extends and encapsulates some page-related fields, such as page number, page size, total number of records, total number of pages, and so on.

Principle

We just added a line, how does it help us achieve paging? Please look down.

What did PageHelper.startPage do?

Let's look at this line of PageHelper.startPage (pageIndex, pageSize); what has been done. Several startPage methods are overloaded in this class, resulting in a call to one of the following

You can see that the method instantiates the Page object with the paging information as a constructor parameter, calls SqlUtil.getLocalPage () to get an old Page object, and finally calls SqlUtil.setLocalPage (page); puts the newly created Page object set in.

Let's see what these two methods have done. As follows, it's simple to get the Page object from ThreadLocal and set the Page object into ThreadLocal. It is needless to say that those who know the role of ThreadLocal can be understood as saving local variables and binding to threads.

Seeing this, let's record the role of this line as creating and saving the Page object (paging information) to the ThreadLocal.

Where to use Page paging information

Well, now that it is saved, there is a place to use it.

I located the place where the call was made through code tracking, and through backtracking, I found that the call was made from this class com.github.pagehelper.dialect.AbstractDialect.

Click in to have a look, mainly to take out the Page object to make some judgments, or to save page-related information. It should be related to later.

Interceptor

The methods in the above AbstractDialect class go back and point to the

The com.github.pagehelper.util.SqlUtil#doIntercept method, and intercep calls the doIntercep method

Continue to trace up to com.github.pagehelper.PageHelper#intercept, which is the method of the Interceptor interface.

Then org.apache.ibatis.plugin.Plugin#invoke calls com.github.pagehelper.PageHelper#intercept.

You can see that PageHelper implements Interceptor, which is officially provided by Mybatis, which means interceptor in Chinese, so it is possible to do something to achieve paging by implementing this interceptor.

Plug-in

Through the code tracking, we can see that the intercept method of Interceptor is called in the invoke method of an org.apache.ibatis.plugin.Plugin class in Mybatis, and this Plugin class implements the java.lang.reflect.InvocationHandler interface of JDK, which is the JDK proxy interface.

There is a wrap method in this Plugin that returns a proxy class, so when you call the method of the proxy class, you go to the invoke method above, and you may go to the interceptor's intercept method.

So if we look at where the warp is tuned, we will know when to create the proxy class. In the PageHelper above, paste the code again.

Interceptor chain

The plugin method in this PageHelper is implemented from the Interceptor interceptor interface, so there will be a place to uniformly call this method, and you will find that it is called in the org.apache.ibatis.plugin.InterceptorChain interceptor chain, as shown below.

This class has a List that holds all interceptors, and three methods, namely, the plugin method that pluginAll uses to call all interceptors, addInterceptor to add interceptors, and getInterceptors to get the interceptor chain.

Seeing this gives you a rough idea of how it works, PageHelper implements paging by implementing Mybatis's Interceptor interface, and Mybatis calls all Interceptor through InterceptorChain.

Load & call interceptor

So let's see when Mybatis's interceptor was added to the interceptor chain and when it was called.

Through code tracing, it is found that the add method is called in the addInterceptor method of Configuration, and Configuration.addInterceptor is called in the pluginElement method of XMLConfigBuilder.

While XMLConfigBuilder parses the configuration of Mybatis in XML mode, as the name implies, the pluginElement method parses plugin-related configuration nodes in XML.

And we did configure plugin in XML.

So we now know that mybatis's interceptor is added to InterceptorChain when Mybatis parses the configuration file and when the plugins node is parsed.

When was the interceptor called. Let's see where the pluginAll method of InterceptorChain is tuned. There are four places to call the interceptor chain through code tracking.

@ Intercepts comment

And PageHelper this interceptor, we can find that this class has a @ Intercepts annotation, this annotation receives a value of @ Signature annotation, in the Signature annotation is configured, Executor.class,query also has four class:MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class.

Anyone who understands the plug-in mechanism of Mybatis will understand that this line of configuration means to intercept the query method in Executor. The method parameter list type is MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, which is the following method.

So seeing this, we can conclude that InterceptorChain's pluginAll method is in the fourth call point above, that is, org.apache.ibatis.session.Configuration#newExecutor (org.apache.ibatis.transaction.Transaction, org.apache.ibatis.session.ExecutorType). It is conceivable that newExecutor, that is, creating an Executor instance, wraps Executor through PageHelper's plugin method when creating Executor, and returns Executor's proxy class. The following will talk about creating a dynamic proxy.

Create a proxy object through PageHelper

We're going to review how we transferred to PageHelper. Enter pluginAll first.

Then it is called to the plugin method of PageHelper, and the warp method of Plugin is internally adjusted.

Let's see what the Plugin.wrap method does. The code is as follows. The code follows us and we know that target is now Executor,interceptor and PageHelper. First obtain the PageHelper intercept information, and then filter whether target is the type to be intercepted. Here, you will enter the if judgment logic and return the proxy object of Executor.

So if the Executor instance created at this time is a proxy object, the proxy's invoke method (org.apache.ibatis.plugin.Plugin#invoke) will be called at some point, and invoke will call the intercept method of the Interceptor interceptor, thus calling the intercept method of PageHelper to perform paging logic.

Org.apache.ibatis.plugin.Plugin#invoke = "com.github.pagehelper.PageHelper#intercept

The call source of interceptor-dynamic proxy

Because the dynamic proxy of Executor is returned, it must be triggered into the invoke method when a method of Executor is called, which is difficult to find. We use the breakpoint to see where the invoke method comes from. First, the breakpoint is called into the invoke method of Plugin.

Through the call stack, we can see that it is called by org.apache.ibatis.session.defaults.DefaultSqlSession#selectList (java.lang.String, java.lang.Object, org.apache.ibatis.session.RowBounds). In the invoke method, we determine whether the target method is the one we want to intercept, because this method is also annotated on PageHelper, so we will go to line 61 of the invoke method of Plugin. So it goes to the intercept method of PageHelper and executes the specific interception logic.

Paging logic

The idea is to spell SQL.

Through code tracking, the final paging logic is in the com.github.pagehelper.util.SqlUtil#doIntercept method, line 162, to get the paging SQL

Call

Com.github.pagehelper.dialect.AbstractDialect#getPageSql (org.apache.ibatis.mapping.MappedStatement, org.apache.ibatis.mapping.BoundSql, java.lang.Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.cache.CacheKey)

Calling com.github.pagehelper.dialect.AbstractDialect#getPageSql (java.lang.String, com.github.pagehelper.Page, org.apache.ibatis.session.RowBounds,org.apache.ibatis.cache.CacheKey) in com.github.pagehelper.dialect.AbstractDialect#getPageSql (org.apache.ibatis.mapping.MappedStatement, org.apache.ibatis.mapping.BoundSql, java.lang.Object, org.apache.ibatis.session.RowBounds,org.apache.ibatis.cache.CacheKey) is an abstract method, and there are many concrete implementations.

We saw mysql's splicing "limit??" in the original SQL.

This is the end of this article on "how to use the third-party PageHelper paging plug-in for Mybatis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report