1、分页查询的执行过程
在实际的项目开发过程中,分页显示是很常见的页面布局;下面我们来梳理一下常规操作中分页查询的执行过程:
-
- 页面发送ajax请求,将分页查询参数(page、pagesize、name等额外信息)提交到服务端
-
- 服务端(controller)接收页面提交的数据并调用Service查询数据
-
- Service调用Mapper操作数据库,查询分页数据
-
- Controller将查询到的分页数据响应给页面
-
- 页面接收到分页数据并通过前端组件(例如:ElementUI的Table组件)展示到页面上
2、页面发送ajax请求:
async init () {
const params = {
page: this.page,
pageSize: this.pageSize,
name: this.input ? this.input : undefined
}
await getMemberList(params).then(res => {
if (String(res.code) === '1') {
this.tableData = res.data.records || []
this.counts = res.data.total
}
}).catch(err => {
this.$message.error('请求出错了:' + err)
})
},
3、创建mybaitisPlus的配置类
首先创建一个mybaitisPlus的配置类,使用mplus提供的分页插件进行过滤查询
package com.tigerhhzz.wuaimai.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置MP的分页插件
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
3、写controller接口
然后在controller中写一个接口
/**
* 员工信息分页查询
* @param page
* @param pageSize
* @param name
* @return
*/
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
//构造分页构造器
Page pageInfo = new Page(page,pageSize);
//构造条件构造器
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
//添加过滤条件
queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
//添加排序条件
queryWrapper.orderByDesc(Employee::getUpdateTime);
//执行查询
employeeService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}
4、测试
响应给页面数据:
{
"code":1,
"msg":null,
"data":{
"records":[
{
"id":1634812678390161409,
"username":"123456",
"name":"zhangsan",
"password":"xxxxxxx",
"phone":"13133333333",
"sex":"1",
"idNumber":"130111119801018111",
"status":1,
"createTime":[
2023,
3,
12,
15,
4,
51
],
"updateTime":[
2023,
3,
12,
15,
4,
51
],
"createUser":1,
"updateUser":1
},
{
"id":1,
"username":"admin",
"name":"管理员",
"password":"xxxxxx",
"phone":"13812312312",
"sex":"1",
"idNumber":"110101199001010047",
"status":1,
"createTime":[
2021,
5,
6,
17,
20,
7
],
"updateTime":[
2021,
5,
10,
2,
24,
9
],
"createUser":1,
"updateUser":1
}
],
"total":2,
"size":10,
"current":1,
"orders":[
],
"optimizeCountSql":true,
"hitCount":false,
"countId":null,
"maxLimit":null,
"searchCount":true,
"pages":1
},
"map":{
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135704.html