数据库中的数据,传到前端,往往会分页处理。这里有两个关键参数:当前页,每页记录数。
这里举例子这样一个场景来举例说明:查找数据库一张表里的所有数据,并且自定义当前页和每页条数来显示。
这里在地址栏传当前页数和每页记录数
//分页查询的方法
//current代表当前页 limit代表每记录数
@GetMapping("getByPage/{current}/{limit}")
@ApiOperation("列表分页")
public R getByPage(@PathVariable long current,
@PathVariable long limit){
//创建page对象,EduTeacher是要分页的对象
Page<EduTeacher> pageList=new Page<>(current,limit); //current代表当前页,limit代表每页的记录数
//调用Mybatis-Plus分页方法实现分页
//调用方法,底层封装,把分页所有数据封装到pageList对象里面
teacherService.page(pageList,null);
long total = pageList.getTotal(); //总记录数
List<EduTeacher> records = pageList.getRecords(); //数据集lists集合
//把数据装进Map集合
Map map=new HashMap();
map.put("total",total);
map.put("rows",records);
return R.ok().data(map);
}
对应前端对接后端接口的方式是
getListPage(current,limit){
return request({
// url: '/pageList'+current+"/"+limit,
//动态获取要用键盘左上角的按键
url:`/pageList/${current}/${limit}`, //建议这样写
method: 'get'
})
},
url对应后端的接口地址,后面的${current}/${limit}表示动态获取参数
method:'get' 表示发送的get请求。