springboot项目中信息分页查询的常规操作流程

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 springboot项目中信息分页查询的常规操作流程,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1、分页查询的执行过程

在实际的项目开发过程中,分页显示是很常见的页面布局;下面我们来梳理一下常规操作中分页查询的执行过程:

    1. 页面发送ajax请求,将分页查询参数(page、pagesize、name等额外信息)提交到服务端
    1. 服务端(controller)接收页面提交的数据并调用Service查询数据
    1. Service调用Mapper操作数据库,查询分页数据
    1. Controller将查询到的分页数据响应给页面
    1. 页面接收到分页数据并通过前端组件(例如: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

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!