Spring Boot Tutorial (8): Spring Boot integrated pagehelper paging plugin

1. Project preparation

Directly use the source code of the previous chapter, Spring Boot Tutorial (7): Spring Boot integrates druid connection pool

For convenience, the following chapters will not modify the package name and startup class name according to the content of the chapter, so first make the following modifications to the source code of the previous section:

1. Package name modification

Change the package name com.songguoliang.mybatisto com.songguoliang.springboot.

2. Modify the startup class

Change the startup class DruidApplicationto Application, and change the annotation @MapperScan("com.songguoliang.mybatis.mapper")scan package to@MapperScan("com.songguoliang.springboot.mapper")

package com.songguoliang.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-02 14:51
 */
@SpringBootApplication
@MapperScan("com.songguoliang.springboot.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3. Change in the application.properties configuration file mybatis.type-aliases-package=com.songguoliang.mybatis.entitytomybatis.type-aliases-package=com.songguoliang.springboot.entity

2. Add pagehelper dependencies

<!-- pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

3. Configuration file modification

Add the following configuration to the application.properties configuration file:

#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

Fourth, modify the Controller

Modified method UserController:lists

@GetMapping("/users")
public List<User> lists(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
    PageHelper.startPage(pageNo,pageSize);
    return userService.getUsers();
}
  • pageNoAnd pageSizetwo parameters are to receive the value passed from the foreground, and defaultValueprovide default values ​​for these two parameters.
  • Pagination main code:PageHelper.startPage(pageNo,pageSize);

It should be noted that the pagination code PageHelper.startPage(pageNo,pageSize);is only valid for the first query after it. If you change the code to the following and add a query, the second query does not have paging

@GetMapping("/users")
public List<User> lists(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize) {
    PageHelper.startPage(pageNo,pageSize);
    userService.getUsers();//这个查询会分页
    return userService.getUsers();//这个查询不会分页
}

5. Test

Start the service, enter the browser, http://localhost:8080/users?pageNo=1&pageSize=5and you can see that only 5 pieces of data on the first page are queried:
write picture description here

Browser input http://localhost:8080/users, this is because no parameters are passed, the default value is taken in the background, and 10 pieces of data on the first page are queried.
write picture description here

6. Return paging information

What we return above is only data, but the paging-related information such as the total number of pages, the current number of pages, and the number of entries per page is not returned.

Next, we will modify the return value of the method in the controller, service, and mapper, and change it List<User>to be the class in the Page<User>package , which is a subclass.Pagecom.github.pagehelperjava.util.ArrayList

1. UserMapperModify the return value toPage<User>

package com.songguoliang.springboot.mapper;

import com.github.pagehelper.Page;
import com.songguoliang.springboot.entity.User;

/**
 * @Description
 * @Author sgl
 * @Date 2018-05-02 15:02
 */
public interface UserMapper {

    Page<User> getUsers();
}

2. UserServiceChange the return value toPage<User>

public Page<User> getUsers() {
    return userMapper.getUsers();
}

com.github.pagehelper.PageInfo3. Encapsulate Page<User>data with classes

@GetMapping("/users")
public PageInfo<User> lists(@RequestParam(defaultValue = "1") int pageNo,@RequestParam(defaultValue = "10") int pageSize) {
    PageHelper.startPage(pageNo,pageSize);
    PageInfo<User> pageInfo = new PageInfo<>(userService.getUsers());
    return pageInfo;
}

4. Restart the service, enter the browser http://localhost:8080/users?pageNo=1&pageSize=5, you can see that only 5 pieces of data on the first page are queried, and the information related to paging is included:
write picture description here





Source code:
github
code cloud

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681891&siteId=291194637