MyBatis-Plus Pagination Query-Page

Write in front

I feel that I haven't updated the technology blog for a while. In fact, this period of time is too sad. I can say that my mood is a low point in my life, but I am basically adjusted now. There is a saying that the young and strong do not work hard, and the boss is sad. An Internet celebrity singer I like very much, he said, if you put most of your energy into your career, your relationship may not go well for the time being, but if you put most of your energy into your relationship, your relationship may be long-term Not going well.

Environment setup

Setting up the environment, the previous articles have been built, but I have to build it again for obsessive-compulsive disorder. . .

Preparations
First create a database table

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

The corresponding data is as follows

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

Create a SpringBoot project
Create a normal SpringBoot project, and then introduce dependencies. Since you have to deal with the database, the connector must be no less

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.18</version>
</dependency>

In addition to use mybatis-plus, of course the starter must be introduced

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

For convenience, Lombok is introduced here

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

Well, the dependency is introduced, and the following simple configuration

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_plus?userSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root

The configuration is also written, then you can start to use it, you need to create an entity class before the operation.

@Data
public class User {
    
    
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

After the entity class is written, write a mapper interface, just inherit BaseMapper, and the basic single-table query will be encapsulated for you. It is really sweet.

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    

}

Core operation

After the preparatory work is done, let’s do a wave of paging processing. When using paging, I emphasize here that you need to write a configuration class first, which can be understood as an interceptor.

@Configuration
public class MybatisPlusConfig {
    
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

It's just that simple to write, it's over, let's see how to use it, it is also very simple to use, that is, a Page is introduced.

@Test
void contextLoads() {
    
    
    Page<User> page = new Page<>(1, 2);
    Page<User> userPage = userMapper.selectPage(page, null);
    System.out.println(userPage.getTotal());
    userPage.getRecords().forEach(System.out::println);
}

The query here is to query the first page, and then display 2 per page.
Insert picture description here
It can be clearly seen that there are 5 pieces of data in total, and 2 pieces of data have been queried.

That's it for today's sharing. If there is anything you want to learn, you can private message (there is no message function for the time being, it is working on it, and it will be migrated if it is done).

Guess you like

Origin blog.csdn.net/HeZhiYing_/article/details/113068459