SSM + PageHelper, ssmpagehelper
Maven is used in the project.
1 introduce the jar package
First, introduce the jar package of PageHelper.
If maven is used, you only need to introduce the plug-in pom. xml as follows:
<dependency>
<groupId>com.github.pagehelper</groupId>
<
artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</Dependency>
Be sure to note: the version number cannot be too low. When is used at the beginning, the error 500 is reported and the page is not correct.
2. Configure in sqlMapConfig. xml:
- <! -- Configure the paging plug-in -->
- <Plugins>
- <Plugin interceptor = "com. github. pagehelper. PageHelper">
- <! -- Set six database types: Oracle, Mysql, MariaDB, SQLite, Hsqldb, and PostgreSQL -->
- <Property name = "dialect" value = "mysql"/>
- <! -- This parameter defaults to false -->
- <! -- If it is set to true, the first parameter offset of RowBounds is used as the pageNum page number. -->
- <! -- The same effect as pageNum in startPage -->
- <Property name = "offsetAsPageNum" value = "true"/>
- <! -- This parameter defaults to false -->
- <! -- If it is set to true, the RowBounds page will be used for count query -->
- <Property name = "rowBoundsWithCount" value = "true"/>
- <! -- 3.3.0 available-the paging parameter is rationalized. The default value is false. -->
- <! -- When the rationalization is enabled, if pageNum <1, the first page is queried. If pageNum> pages, the last page is queried. -->
- <! -- When the rationalization is disabled, if pageNum <1 or pageNum> pages returns NULL data -->
- <Property name = "reasonable" value = "true"/>
- <! -- Pagination parameters can be passed through Mapper interface parameters -->
- <Property name = "supportMethodsArguments" value = "true"/>
- <! -- Always returns the PageInfo type. check whether the returned type is PageInfo. If not, Page is returned. -->
- <Property name = "returnPageInfo" value = "check"/>
- </Plugin>
- </Plugins>
3. The serviceImpl receives the results from mapper. xml and uses pagehelper for paging.
Example:
Public TbResult getArticleByType (String type, int pageNumber, int pageSize ){
TbArticleExample example = new TbArticleExample ();
Criteria criteria = example. createCriteria ();
Criteria. andtype0000to (type );
PageHelper. startPage (pageNumber, pageSize );
List <TbArticle> art = articleMapper. selectByExampleWithBLOBs (example );
PageInfo <TbArticle> pageInfo = new PageInfo <> (art );
TbResult result = new TbResult ();
Result. setRows (art );
Result. setTotal (pageInfo. getTotal ());
Return result;
}
We can see that PageHelper. startPage (pageNumber, pageSize); is displayed before executing the SQL statement. PageInfo is actually the result that has been divided. Here I will split the result into a TbResult class, later I found that this is not required. PageInfo. getList () is art, so you can directly return pageInfo.
4. You can receive the result at the Controller layer.