Use of paging assistant PageHelper

Time:2021-9-26

Use of paging assistant PageHelper

brief introduction

PageHelper is an easy-to-use mybatis paging plug-in. It is very convenient to realize paging function through this plug-in.

Official website address

use

This plug-in is very simple to use.

Introduce dependency

Create a new springboot project and add the following dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

<!--mapper-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>
<!-- PageHelper paging -- >
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

Add configuration

#Database
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/study_springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root

#mybatis
mybatis.type-aliases-package=com.njit.model
mybatis.mapper-locations=classpath:mapper/*.xml

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

Then specify the package name of TK mapper on the startup class.

@SpringBootApplication
@MapperScan("com.njit.mapper")
public class PagehelperDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(PagehelperDemoApplication.class, args);
    }

}

Add entity class of database

@Data
public class User {

    private Integer id;

    private String name;

    private String password;
}

Write a general mapper

import com.njit.model.User;
import tk.mybatis.mapper.common.Mapper;

/**
 * @Author: njitzyd
 * @Date: 2021/2/21 22:28
 * @Description: UserMapper
 * @Version 1.0.0
 */
public interface UserMapper  extends Mapper<User> {
}

test

In actual use, you only need to use static methods to specify paging parameters before database query.

@SpringBootTest
class PagehelperDemoApplicationTests {

    @Autowired
    private UserMapper userMapper;
    @Test
    void contextLoads() {
        PageHelper.startPage(2,3);
        userMapper.selectAll().forEach(e->
                System.out.println(e)
        );
    }

}

The following results will be output:

Use of paging assistant PageHelper

It is consistent with our expected data. The data in the database is shown in the figure below:

Use of paging assistant PageHelper

summary

This completes the introduction to the basic use of paging assistant.

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]