MP插件功能
1.分页插件
首先,要在配置类中注册MyBatisPlus的核心插件,同时添加分页插件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Configuration public class MyBatisConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); paginationInnerInterceptor.setMaxLimit(1000L); interceptor.addInnerInterceptor(paginationInnerInterceptor); return interceptor; } }
|
接着,就可以使用分页的API了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Test void testPageQuery() { int pageNo = 1, pageSize = 2; Page<User> page = Page.of(pageNo, pageSize); page.addOrder(new OrderItem("balance", true)); page.addOrder(new OrderItem("id", true));
Page<User> p = userService.page(page);
long total = p.getTotal(); System.out.println("total = " + total); long pages = p.getPages(); System.out.println("pages = " + pages); List<User> users = p.getRecords(); users.forEach(System.out::println); }
|
2.通用分页实体
需求:遵循下面的接口规范,编写一个UserController接口,实现User的分页查询
| 参数 |
说明 |
| 请求方式 |
GET |
| 请求路径 |
/users/page |
| 请求参数 |
{ “pageNo”: 1, “pageSize”: 5, “sortBy”: “balance”, “isAsc”: false, “name”: “jack”, “status”: 1 } |
| 返回值 |
{ “total”: 1005, “pages”: 201, “list”: [ { “id”: 1, “username”: “Jack”, “info”: { “age”: 21, “gender”: “male”, “intro”: “佛系青年” }, “status”: “正常”, “balance”: 2000 }, { “id”: 2, “username”: “Rose”, “info”: { “age”: 20, “gender”: “female”, “intro”: “文艺青年” }, “status”: “冻结”, “balance”: 1000 } ] } |
| 特殊说明 |
- 如果排序字段为空,默认按照更新时间排序 - 排序字段不为空,则按照排序字段排序 |


海林小盆友
小盆友
本文采用 CC BY-NC-SA 4.0 许可协议