This is the fourth installment of the Mybatis series. Please visit the following website for the first three articles.
- Integration of SpringBoot quickly start to add, delete, change and check
- MybatisPlus part 2 – Application and summary of conditional builders
- MybatisPlus 3 – Custom SQL
Spring Boot-MybatisPlus supports paging
Add the following page interceptor configuration to MybatisPlus to support paging in a Spring project that has already integrated MybatisPlus
@configuration public class MybatisPlusConfiguration {@bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); }}Copy the code
Single table query pagination – table pagination
@Test public void testSelect() { LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>(); Query. Ge (User::getAge,10) // Query condition: age > 10. OrderByDesc (User::getAge); Page<User> Page = new Page<> (1,10); Usermapper.selectpage (page,query); // Query usermapper.selectPage (page,query); System.out.println(" total pages: "+ Page.getPages ()); System.out.println(" total number of entries: "+ page.getTotal()); List<User> List = page.getRecords(); list.forEach(System.out::println); }Copy the code
The following output is displayed:
Total pages: 1 Total records: 6 User(id=3, name=Tom, age=28, [email protected]) User(id=5, name=Billie, age=24, [email protected]) User(id=4, name=Sandy, age=21, [email protected]) User(id=2, name=Jack, age=20, [email protected]) User(id=1, name=Jone, age=18, [email protected]) User(id=1280261858003038209, Name = giannis, age=18, email=null)Copy the code
In the paging query, two SQL statements are executed
SELECT COUNT(1) FROM user WHERE age >=? SELECT id,name,age,email FROM user WHERE age >=? ORDER BY age DESC LIMIT ? ,?Copy the code
This kind of paging method is more suitable for traditional application, the development of table paging. You need to give the total number of entries and how many entries per page.
Pages that do not query the total number of records – drop-down pages
In some modern Internet information website, or application APP. The total number of pieces of data is usually not given, but n pieces of data are loaded each time they are pulled down by mouse or gesture.
Paging in this case usually eliminates the need to query the total number of entries, which can waste computing resources in the database and lead to long response times. So we should only query paging data, not the total number of entries. Set the third parameter of page paging to false.
The total number of pages and entries in the output is 0, but the paging data is returned as normal.
Total pages: 0 total records: 0 User(id=3, name=Tom, age=28, [email protected]) User(id=5, name=Billie, age=24, [email protected]) User(id=4, name=Sandy, age=21, [email protected]) User(id=2, name=Jack, age=20, [email protected]) User(id=1, name=Jone, age=18, [email protected]) User(id=1280261858003038209, Name = giannis, age=18, email=null)Copy the code
This is the only SQL executed
ELECT id,name,age,email FROM user WHERE age >= ? ORDER BY age DESC LIMIT ? ,?Copy the code
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series