PageHelper, the easiest paging plugin to learn from

Keywords: Programming github Spring Mybatis MySQL

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, let's talk about PageHelper, a paging plugin. Here's the open source address of PageHelper. Interested buddies can check it out PageHelper's project address in github:

1https://github.com/pagehelper/Mybatis-PageHelper

PageHelper's project address in gitosc:

1 http://git.oschina.net/free/Mybatis_PageHelper>

Let's start with this plugin, PageHelper Official address:

1https://pagehelper.github.io/

Visit the page after the official website, there is a detailed Demo in the official website, today we will tap Demo manually according to the official website; How do I use PageHelper? 1. Add dependencies to your own project, maven projects add the latest version to the parent project, which 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 Data Access Layer Spring configuration file with the following code

 1<bean class="org.mybatis.spring.SqlSessionFactoryBean">
 2        <property name="dataSource" ref="dataSource" />
 3        <!--To configure 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 of PageHelper Paging Plugin Parameters Here I will only introduce two commonly used, detailed API documentation available on the official website helperDialect: Paging plug-in automatically detects current database links and automatically selects 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 SqlServer2012 database, you need to specify sqlserver2012 manually, otherwise you will use SqlServer2005 as paging.You can also implement AbstractHelperDialect, then configure the property to implement the fully qualified name of the class to use a custom implementation. reasonable: Paging rationalization parameter, default false.When this parameter is set to true, pageNum<=0 queries the first page, and pageNum>pages (when the total is exceeded) queries the last page.When default false, queries are made directly based on the parameters.

4. There are two ways to use this in your code:

First

1// The first select query after this statement is automatically paginated
2PageHelper.startPage(1, 10); 
3// Queries here are automatically paginated
4List<country> list = countryMapper.selectIf(1);

Second

 1//Get page 1, 10 entries, default total number of queries count
 2PageHelper.startPage(1, 10);
 3List<country> list = countryMapper.selectAll();
 4//Wrap results in PageInfo
 5PageInfo page = new PageInfo(list);
 6//Test all PageInfo properties
 7//PageInfo source code shows very comprehensive paging properties
 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());

Using PageHelper After configuring the coordinates above and the Spring profile, we start using PageHelper directly. Everyone's table data may be different. Let's play it on your own, code 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](https://my.oschina.net/u/1162528)
 6public PageInfo<company> findByPage(int pageNum, int pageSize) {
 7    // Start paging, PageHelper component will automatically paginate the first query after
 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 a collection of queries.Paging parameters are automatically calculated
12    PageInfo<company> pageInfo = new PageInfo&lt;&gt;(list);
13    return pageInfo;
14}

unit testing

 1@RunWith(SpringJUnit4ClassRunner.class)
 2@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
 3public class UserServiceImplTest {
 4    // Injection 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 ontrollers are different. I'm here to give you a template. If you don't understand it, 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}

That's it for today, come on!!!See you tomorrow. >This article was started by Public Number [Framework, ID: mohu121]. Please indicate the source </user></company></company></company></company></company></user></company></country></country>

Posted by carsale on Thu, 07 Nov 2019 17:56:51 -0800