开发过程中,数据量大的时候,查询效率会有所下降,这时,我们往往会使用分页。
具体操作入下:
1、添加分页插件:
package com.zhang.config;import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;@EnableTransactionManagement
@Configuration
@MapperScan("com.zhang.mapper")
public class MybatisPlusConfig {@Beanpublic PaginationInterceptor paginationInterceptor() {// 1、创建分页插件PaginationInterceptor paginationInterceptor = new PaginationInterceptor();// 2、添加分页插件paginationInterceptor.setLimit(500);return paginationInterceptor;}
}
2、编写接口:
@GetMapping(value = "/testPages")public R<?> test(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
// 1、分页条件Page<Animal> page = new Page<>(pageNo, pageSize);
// 2、分页排序page.addOrder(OrderItem.asc("age"));IPage<Animal> pages = animalService.page(page);return R.ok(pages);}
3、测试分页效果:
如:
{"code": 0,"data": {"records": [{"id": "1775e2db7fb5e0d9b8e98d4137f58b91","name": "狗毛","age": 2,"delFlag": "正常","hobby": "骨","createTime": "2023-09-30T04:10:22.000+00:00","updateTime": "2022-01-30T08:55:56.000+00:00","relation": null},{"id": "320f4e82f56d66cb8d8ab2c991f9526c","name": "狗哥","age": 2,"delFlag": "正常","hobby": "骨头","createTime": "2022-02-23T06:24:22.000+00:00","updateTime": null,"relation": null}],"total": 9,"size": 2,"current": 1,"orders": [{"column": "age","asc": true}],"searchCount": true,"pages": 5},"msg": "执行成功"
}