The mybatis interceptor implements paging

Final Dao layer result:

The next step is to implement paging step by step.

I To create a page object:

It can be found that a pagelist class is inherited here; This class is also a self created class that implements the list interface. The reason why pagelist is used is that page needs to implement the list interface, and the abstract methods in the interface need to be implemented one by one. Therefore, pagelist is provided to write methods to implement the list interface in a unified place.

Why page needs to implement the list interface will be explained later in the code.

Pagelist class:

II Provide Dao and mapper xml

How to write Dao:

Page
pageByConditions(RowBounds rowBounds,Model record);

mapper. xml:

OK, the above are the basic operations of mybatis, so there is no unnecessary explanation.

III Create Interceptor:

What we need to do is create a page interceptor and a page executor.

1. Pageinterceptor: implement the interceptor interface, execute the pageexecutor, intercept SQL, and add paging SQL (limit XX, XX)

2. Pageexecutor: implement the executor interface. When querying, add the total number of queries and modify the return value type. Because what we need to do is paging and query operation, the non query methods inside use the basic implementation, and only two query methods are modified.

Pageinterceptor complete code:

Pageexecutor complete code:

The reason why page needs to implement the list interface: you can see that the return value of the query method is list < E >, and now we want to use the page < E > object in Dao to receive the results returned by mybatis, so we need to let page implement the list interface.

Execution sequence of paging query: enter the plugin method of pageinterceptor, intercept the executor, enter the query method of pageexecutor, and execute the executor Query() returns to the plugin method of pageinterceptor again. This time, it will be executed

Enter the intercept method, splice the executed SQL into paging restriction statements, and then query the data result set. executor. After query() is executed, continue to execute pageresolver. If the method name matches the configured string that needs to perform paging operation, query the total amount of data and return the page object; If there is no match, the list object is returned directly.

IV XML configuration:

V Test code:

This paper mainly introduces the steps and methods of paging with mybatis interceptor. It has a good reference value. Let's follow Xiaobian to have a look

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
分享
二维码
< <上一篇
下一篇>>