A pagination plug-in that explains mybatis extremely simple (good) single (use)

Note: this blog is completely different from the current paging plug-in, so it is recommended that you check the latest source code and documentation through the project address above.

I used to worry about mybatis paging query, and I searched many relevant articles on the Internet, but I didn't use the last one. The paging places are all handwritten paging SQL and count SQL. In short, it is very troublesome.

Later, I wanted to write a paging implementation from mybatis for some time. I wrote an implementation for languagedriver. Automatic paging is no problem, but the total query count still can't be solved at one time, and finally it's nothing.

Recently, paging has to be used again. In order to facilitate writing a general paging class, we refer to most of the mybatis paging codes on the Internet again.

In fact, a long time ago, someone had an open source implementation on GitHub that supports mysql, Oracle and sqlserver. It is similar to the above reference and considered more comprehensively. But I think too many classes are too troublesome, so I implemented a class with only one interceptor. In fact, it can be divided into two classes. One class is written as a static class and placed in the interceptor. You can also extract the page class to facilitate the use of page.

Let's start with the implementation method. The plug-in has only one class: PageHelper Java

Interceptor signature is:

The signature here is very important to the whole implementation and idea. First, I intercept the prepare method to change the paging SQL to do count query. Then I intercept the handleresultsets method to get the final processing result and put the result in the page object.

The following is the code for modifying paging, which is for Oracle data. If there are other databases, you can modify the code here yourself.

Then, a selelct count statement in the following setpageparameter method, which also needs to be modified according to the database type:

Why don't I provide support for various databases? I don't think it's necessary. Some databases don't support paging, and the simpler the plug-in is, the easier it is for developers to understand and modify it. It is certainly not a problem to modify it to the paging query you need.

Finally, complete the code (continue to see the following usage): (@ l_301_0 @)

To use this interceptor, first configure the interceptor in mybatis configuration:

Pay attention to the location of plugins when configuring interceptors. The order of plugins locations is as follows:

Finally, the example code for calling this method (service layer):

It can be seen from the above that using the plug-in is very simple. You only need to use the startpage and endpage methods of PageHelper before and after query. The call result of intermediate code already exists in the result of page. If you call PageHelper where a result is returned, the returned result is still a list, Just take the first value (I don't think anyone will use it in such a place. Of course, it doesn't make mistakes).

In addition, all mybatis codes between startpage and endpage will be paged, and PageHelper will only retain the last result. Therefore, when using, you need to ensure that only one mybatis query is executed in it at a time. If there are multiple pages, please use startpage and endpage multiple times.

Since only the implementation of Oracle is provided here, I hope readers who refer to other databases implemented by the paging plug-in can also open source the corresponding code.

Project address: http://xiazai.jb51.net/201612/yuanma/Mybatis_PageHelper_jb51.zip

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>