Take you to learn the simplest paging plug-in PageHelper

Take you to learn the simplest paging plug-in PageHelper

Introduction:

PageHelper is an excellent open source mybatis paging plugin in China. It supports basic mainstream and commonly used databases, such as mysql, oracle, mariaDB, DB2, SQLite, Hsqldb, etc. Today, Mo Bai will talk to you about the PageHelper paging plug-in. Below is the open source address of PageHelper. Interested friends can check it out.

  • PageHelper's project address on github:
https://github.com/pagehelper/Mybatis-PageHelper
  1. First introduce this plug-in PageHelper
  • Official website address:
https://pagehelper.github.io/
  • Visit the page after the official website, the official website has a detailed Demo, today we will follow the official website to tap a Demo by ourselves;
  • How to use PageHelper?

1. Add dependencies to your own project, add the latest version of the maven project to the parent project, the latest version is 5.1.8

1<dependency>
2 <groupId>com.github.pagehelper</groupId>
3 <artifactId>pagehelper</artifactId>
4 <version>5.1.8</version>
5</dependency>

2. Configure the interceptor plug-in in the Spring configuration file of the data access layer, the code is as follows

 1<bean class="org.mybatis.spring.SqlSessionFactoryBean">
 2 <property name="dataSource" ref="dataSource"/>
 3 <!--Configure the PageHelper interceptor plugin-->
 4 <property name="plugins">
 5 <array>
 6 <bean class="com.github.pagehelper.PageInterceptor">
 7 <property name="properties">
 8 <value>
 9 helperDialect=mysql
10 reasonable=true
11 </value>
12 </property>
13 </bean>
14 </array>
15 </property>
16 </bean>

3. Introduction to PageHelper paging plug-in parameters

  • Here I only introduce two commonly used ones. For details, you can see the official website API documentation.

helperDialect:

The paging plug-in will automatically detect the current database link and automatically select the appropriate paging method. You can configure the helperDialect property to specify which dialect the paging plugin uses. When configuring, you can use the following abbreviated values

1oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby

Special note: When using the SqlServer2012 database, you need to manually specify it as sqlserver2012, otherwise it will use SqlServer2005 for paging. You can also implement AbstractHelperDialect, and then configure the property to the fully qualified name of the implementation class to use a custom implementation method.

reasonable:

Paging rationalization parameter, the default value is false. When this parameter is set to true, the first page will be queried when pageNum<=0, and the last page will be queried when pageNum>pages (when the total number is exceeded). When the default is false, query directly based on the parameters.

4. Two ways used in the code:

  • The first
1//The first select query after this statement will automatically be paged
2PageHelper.startPage(1, 10); 
3//The query here will be automatically paginated
4List<Country> list = countryMapper.selectIf(1);
  • The second
 1//Get the first page, 10 pieces of content, the default total number of queries count
 2PageHelper.startPage(1, 10);
 3List<Country> list = countryMapper.selectAll();
 4//Use PageInfo to wrap the results
 5PageInfo page = new PageInfo(list);
 6//Test all properties of PageInfo
 7//PageInfo source code can be seen contains a very comprehensive paging attributes
 8assertEquals(1, page.getPageNum());
 9assertEquals(10, page.getPageSize());
10assertEquals(1, page.getStartRow());
11assertEquals(10, page.getEndRow());
12assertEquals(183, page.getTotal());
13assertEquals(19, page.getPages());
14assertEquals(1, page.getFirstPage());
15assertEquals(8, page.getLastPage());
16assertEquals(true, page.isFirstPage());
17assertEquals(false, page.isLastPage());
18assertEquals(false, page.isHasPreviousPage());
19assertEquals(true, page.isHasNextPage());
  • Use PageHelper

After configuring the above coordinates and the Spring configuration file, we directly start to use PageHelper, each person's table data may be different, everyone can play according to their needs, the code is as follows

  • Mapper interface
1/**
2 * CompanyMapper interface
3*/
4public interface CompanyMapper {
5//Query all
6 List<Company> findAll();
7}
  • Mapper mapping
1 <!--Query all-->
2 <select id="findAll" resultMap="BaseResultMap">
3 select * from ss_company where companyId=#{companyId}
4</select>
  • Service interface
1public interface CompanyService {
2//Paging query
3 PageInfo<User> findByPage(int pageNum, int PageSize);
4//Query all
5 List<Company> findAll();
6}
  • impl implementation
 1//Inject Mapper
 2 @Autowired
 3 private CompanyMapper companyMapper;
 4
 5@Override
 6public PageInfo<Company> findByPage(int pageNum, int pageSize) {
 7//Start paging, the PageHelper component will automatically query the first query after it is paging
 8 PageHelper.startPage(pageNum,pageSize);
 9//call dao query
10 List<Company> list = companyMapper.findAll();
11//Create a PageInfo object to encapsulate the paging results and pass in the query collection. Will automatically calculate paging parameters
12 PageInfo<Company> pageInfo = new PageInfo<>(list);
13 return pageInfo;
14}
  • unit test
 1@RunWith(SpringJUnit4ClassRunner.class)
 2@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
 3public class UserServiceImplTest {
 4//Inject service
 5 @Autowired
 6 private CompanyService companyMapper;
 7
 8 @Test
 9 public void findByPage(){
10 PageInfo<User> pageInfo = companyMapper.findByPage(1, 2);
11 System.out.println(pageInfo);
12}
13}
  • Test Results:

Controller: Everyone’s Controller is different. I will give you a template here. If you don’t understand, you can leave a message in the background or ask questions in the group.

For reference only:

 1 @Autowired
 2 private CompanyService companyService;
 3
 4 @RequestMapping("/***")
 5 public ModelAndView findByPage(
 6 @RequestParam(defaultValue = "1") int pageNum,
 7 @RequestParam(defaultValue = "5") int pageSize) {
 8
 9 ModelAndView mv = new ModelAndView();
10 mv.addObject("pageInfo",pageInfo);
11 mv.setViewName("Detail list");
12 return mv;
13}
14}