PageHelper is a pagination plug-in, can help developers quickly and easily complete common paging feature, you simply need to use two lines of code to complete a paging effect ...
Branch recently did a project using Maven + SSM environment, there are paging function, then use the tools to complete PageHelper paging paging capabilities, experience is very good.
1. Database preparation
t_diy the user to write their own articles personality of a table, t_categories is the article's classification, t_user user table; pagination is to find diy information corresponding category according to the classification ID and displayed.
2. PageHelper introduced
1. Introducing a dependency in pom.xml
1 |
<dependency> |
(The official recommended to use the latest version, it is currently the latest 5.1.10)
Although the official explanation for the introduction of pagehelper.jar and jsqlparser.jar, but in fact will automatically introduce jsqlparser.jar introduction pagehelper.jar
2. Configure Mybatis plug-in
can choose to configure the spring configuration file, you can also configure the plug-in mybatis global configuration file. I was in mybatis global configuration file.
1 |
<plugins> |
Here we note that (I stepped pit):
Note plugins writing position
Started PageHelper
Very easy to use, call PageHelper.startPage (pageNum, pageSize); The first parameter is the first few pages, the second parameter is the page size, closely followed by a first query is to query the page .
In order to facilitate page display and processing results can be used to query results PageInfo Package: PageInfo pageInfo = new PageInfo (data , navigatePages); The first parameter is out of the query data set, it may generally be a List, a second argument is the need to continuously display how many pages the page (can be omitted).
The following is my code:
1 |
@RequestMapping (value = "/ showCategory") |
As a SQL statement, do not need to do anything related to paging, my statement is a simple SQL query:
This completes the course, the easiest and most common paging function, is not very simple.
Use Bootstrap beautification page column
Paged data has been, of course, need to optimize your pages, Bootstrap is an easy-to-use front-end library, providing a large column on how to use PageHelper pagination plug-in the actual project a lot of style, address: HTTPS: //v4.bootcss. com
Bootstrap offers a page bar style, the specific use is not said, and can refer to the official website. The desired effect should look like this:
Actually it contains a lot of small logic:
- Home page is not the first point, does not display the previous page
- Finally, when the last page is not a point, after a no show
- The current page highlighted
- The user clicks to update the displayed page
That's why you want the result to the benefits package PageInfo, the use of these PageInfo can easily control logic:
- PageInfo has a property navigatepageNums is stored in the current page number should be displayed, and before this second argument PageInfo construction related. (Loop display page)
- PageInfo has attribute list, storing pages of data (data acquisition)
- PageInfo has attribute pageNum, indicates the current page number (determined whether highlighted)
- PageInfo have attributes isFirstPage, isLastPage, indicating whether the first and last page (the first and last page control button to display the front and back pages and hidden)
My code is as follows:
1 |
<c:if test="${pageInfo.list.size()>0}"> |
最后的效果如下所示:
![](如何在实际项目中使用PageHelper分页插件/result.PNG)
(数据有限,只有两页...)