I. Preparation of the project
Use the source code of the previous section directly, Spring Boot tutorial (vii): Spring Boot integrated druid connection pool
For convenience, the following sections no longer modify the package name and the startup class name according to the chapter content, so the source code for the previous section should be modified as follows:
1. Package Name Modification
Modify the package name Com.songguoliang.mybatis to Com.songguoliang.springboot.
2. Modify the Startup class
Modify 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. The mybatis.type-aliases-package= in the application.properties configuration file Com.songguoliang.mybatis.entity changed to Mybatis.type-aliases-package=com.songguoliang.springboot.entity II, Add Pagehelper Dependency
<!--pagehelper--
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
Third, configuration file modification
Add the following configuration in the Application.properties configuration file:
#pagehelper分页插件配置
pagehelper.helperdialect=mysql
pagehelper.reasonable=true
Pagehelper.supportmethodsarguments=true
Pagehelper.params=count=countsql
Iv. Modification of controller
To modify the lists method in Usercontroller:
@GetMapping ("/users") public
list<user> lists (@RequestParam (defaultvalue = "1") int PageNo, @RequestParam ( DefaultValue = "ten") int pageSize) {
pagehelper.startpage (pageno,pagesize);
return Userservice.getusers ();
}
The two parameters of PageNo and pagesize are to receive values passed by the foreground, and default values are provided for both parameters by DefaultValue. Paging Main code: Pagehelper.startpage (pageno,pagesize);
It is important to note that the paging code pagehelper.startpage (pageno,pagesize) is only valid for the first query thereafter. If you change the code to the following and add a query, the second query is not paged
@GetMapping ("/users") public
list<user> lists (@RequestParam (defaultvalue = "1") int PageNo, @RequestParam ( DefaultValue = "ten") int pageSize) {
pagehelper.startpage (pageno,pagesize);
Userservice.getusers ();//This query will be paged back
userservice.getusers ();//This query will not be paged
}
v. Testing
Start the service, browser input http://localhost:8080/users?pageNo=1&pageSize=5, you can see only the first page 5 data:
Browser input http://localhost:8080/users, this is because there is no parameters, the background to take the default values, query the first page of 10 data.
Vi. returning paging information
All we have returned is data, and the total number of pages, the current page, and the pages per page are not returned.
Below we modify the controller, service, mapper the return value of the method, change the list<user> to Page<user> The page is a class in the Com.github.pagehelper package, which is a subclass of Java.util.ArrayList.
1. Change the return value to page<user> in Usermapper
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. Change the return value to page<user> in UserService
Public page<user> getusers () {
return usermapper.getusers ();
}
3. Package page<user> data with Com.github.pagehelper.PageInfo class
@GetMapping ("/users") public
pageinfo<user> lists (@RequestParam (defaultvalue = "1") int pageno,@ Requestparam (defaultvalue = "ten") int pageSize) {
pagehelper.startpage (pageno,pagesize);
pageinfo<user> PageInfo = new pageinfo<> (Userservice.getusers ());
return pageInfo;
}
4, restart the service, browser input http://localhost:8080/users?pageNo=1&pageSize=5, you can see only the first page of 5 data, and contains paging related information:
Source:
GitHub
Code Cloud