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.mybatis
to com.songguoliang.springboot
.
2. Modify the startup class
Change the startup class DruidApplication
to 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.entity
tomybatis.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();
}
pageNo
AndpageSize
two parameters are to receive the value passed from the foreground, anddefaultValue
provide 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=5
and you can see that only 5 pieces of data on the first page are queried:
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.
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.Page
com.github.pagehelper
java.util.ArrayList
1. UserMapper
Modify 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. UserService
Change the return value toPage<User>
public Page<User> getUsers() {
return userMapper.getUsers();
}
com.github.pagehelper.PageInfo
3. 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:
Source code:
github
code cloud