SSM + PageHelper, ssmpagehelper

Source: Internet
Author: User

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:
 
  1. <! -- Configure the paging plug-in -->
  2. <Plugins>
  3. <Plugin interceptor = "com. github. pagehelper. PageHelper">
  4. <! -- Set six database types: Oracle, Mysql, MariaDB, SQLite, Hsqldb, and PostgreSQL -->
  5. <Property name = "dialect" value = "mysql"/>
  6. <! -- This parameter defaults to false -->
  7. <! -- If it is set to true, the first parameter offset of RowBounds is used as the pageNum page number. -->
  8. <! -- The same effect as pageNum in startPage -->
  9. <Property name = "offsetAsPageNum" value = "true"/>
  10. <! -- This parameter defaults to false -->
  11. <! -- If it is set to true, the RowBounds page will be used for count query -->
  12. <Property name = "rowBoundsWithCount" value = "true"/>
  13. <! -- 3.3.0 available-the paging parameter is rationalized. The default value is false. -->
  14. <! -- When the rationalization is enabled, if pageNum <1, the first page is queried. If pageNum> pages, the last page is queried. -->
  15. <! -- When the rationalization is disabled, if pageNum <1 or pageNum> pages returns NULL data -->
  16. <Property name = "reasonable" value = "true"/>
  17. <! -- Pagination parameters can be passed through Mapper interface parameters -->
  18. <Property name = "supportMethodsArguments" value = "true"/>
  19. <! -- Always returns the PageInfo type. check whether the returned type is PageInfo. If not, Page is returned. -->
  20. <Property name = "returnPageInfo" value = "check"/>
  21. </Plugin>
  22. </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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.