05---前后端实现分页查询

现在需要新加一个需求:实现分页查询,模糊查询,(例如通过用户名查询出数据) 从上往下修改

1、controller层

UserController.java

 @GetMapping("/page")
    public Map<String,Object> findPage(@RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize,
                                       @RequestParam String username){
      return userService.findPage(pageNum,pageSize,username);

    }

三个参数,pageNum,pageSize,username

2、service层

写实现方法

UserService.java

 public Map<String,Object> findPage(Integer pageNum, Integer pageSize,String username){
        pageNum=(pageNum-1)*pageSize;  //第一个参数
        username="%"+username+"%";
        int total = userMapper.pageTotal(username);  //查询出符合条件的数据总条数
        List<User> data = userMapper.selectPage(pageNum, pageSize,username);
        Map<String,Object>res=new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return res;

    }
  • 写具体的实现方法,业务代码都放在这里。
  • 分页查询出来有两个结果,一个是user列,一个是total总数,所以用一个Map来封装这两个结果Map<String,Object>res=new HashMap<>();

3、UserMapper

UserMapper.java

     List<User> selectPage(Integer pageNum, Integer pageSize,String username);

    int pageTotal(String username);
  • 这个没有什么讲究,为了代码规范

4、sql语句

UserMapper.xml

<!--    分页查询-->
    <select id="selectPage" resultType="User">
        select * from user where username like #{username} limit #{pageNum},#{pageSize}
    </select>

    <select id="pageTotal" resultType="int">
        select count(*) from user where username like #{username}
    </select>


  • 分页查询,模糊查询

5、运行测试

  1. 启动主项目,进入swagger页面测试接口,http://localhost:8081/swagger-ui.html

在这里插入图片描述

  1. 测试分页接口

在这里插入图片描述

结果:

在这里插入图片描述

测试成功,成功取到符合条件的数据并分页

6、前端实现分页

  1. 从后端请求到数据,并显示在前端页面上
  2. 遇到bug:

BUG:Access to fetch at ‘http://localhost:8081/user/page?pageNum=1&pageSize=2’ from origin ‘http://localhost:8080’ has been blocked by CORS

解决:跨域问题

  • 在后端配置一个CorsConfig
package com.xqh.common;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    // 当前跨域请求最大有效时长。这里默认1天
    private static final long MAX_AGE = 24 * 60 * 60;

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("http://localhost:8080"); // 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
        corsConfiguration.setMaxAge(MAX_AGE);
        source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }
}

  1. 实现分页。分页按钮,跳转时有响应
<el-pagination
       @size-change="handleSizeChange"
       @current-change="handleCurrentChange"
         :current-page="pageNum"
         :page-sizes="[2, 4, 6, 10]"
         :page-size="pageSize"
         layout="total, sizes, prev, pager, next, jumper"
         :total="total">
       </el-pagination>
<script>
 load(){
      fetch("http://localhost:8081/user/page?pageNum="+this.pageNum+ "&pageSize=" +this.pageSize+"&username="+this.username)
      .then(res=>res.json()).then(
      res=>{console.log(res)

      this.tableData=res.data

      this.total=res.total
      })

     },
    
     handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.load()
     },
     handleCurrentChange(pageNum){
      this.pageNum=pageNum
      this.load()
     }
  </script>
  1. 实现输入用户名搜索
 <el-input style="width:200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
      
       <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
  1. Home.vue完整代码
<template>
  <el-container style="height: 100vh; border: 1px solid #eee">
 <el-aside :width="sideWidth+'px'" style="background-color: rgb(238, 241, 246);height: 100%;" >
   <el-menu :default-openeds="['1', '3']" style="min-height:100%;overflow-x:hidden" 
   background-color="rgb(48,65,86)"
   text-color="#fff"
   active-text-color="ffd04b"
   :collapse-transition="false"
   :collapse="isCollapse"
   class="el-menu-vertical-demo"
   >
   <div style="height:60px;line-height:60px;text-align:center">
     <img src="../assets/logo.png" alt="" style="width:20px;position:relative;top:5px;margin-right: 5px;">
     <b style="color:#ccc" v-show="logoTextShow">后台管理系统</b>

   </div>
    
     <el-submenu index="1">
       <template slot="title">
         <i class="el-icon-message"></i>
         <span slot="title">导航一</span>
       </template>
       <el-submenu index="1-4">
         <template slot="title">选项4</template>
         <el-menu-item index="1-4-1">选项4-1</el-menu-item>
       </el-submenu>
     </el-submenu>
     <el-submenu index="2">
       <template slot="title"><i class="el-icon-menu"></i>
         <span slot="title">导航二</span>
       </template>
       <el-submenu index="2-4">
         <template slot="title">选项4</template>
         <el-menu-item index="2-4-1">选项4-1</el-menu-item>
       </el-submenu>
     </el-submenu>
     <el-submenu index="3">
       <template slot="title"><i class="el-icon-setting"></i>
         <span slot="title">导航三</span>
       </template>
       <el-submenu index="3-4">
         <template slot="title">选项4</template>
         <el-menu-item index="3-4-1">选项4-1</el-menu-item>
       </el-submenu>
     </el-submenu>
   </el-menu>
 </el-aside>
 
 <el-container>
   <el-header style=" font-size: 12px;border-bottom: 1px solid #ccc;line-height:60px;display: flex;">
     <div style="flex:1;font-size:18px">
       <span :class="collapseBtnClass" style="cursor:pointer" @click="collapse"></span>
     </div>
     <el-dropdown style="width:80px;cursor:pointer" >
       <span>Truthfully</span><i class="el-icon-arrow-down" style="margin-left:2px"></i>
       <i class="el-icon-setting" style="margin-right: 15px"></i>
       <el-dropdown-menu slot="dropdown">
         <el-dropdown-item>个人信息</el-dropdown-item>
         <el-dropdown-item>退出</el-dropdown-item>
       </el-dropdown-menu>
     </el-dropdown>
   </el-header>
   
   <el-main>
     <div style="margin-bottom:30px">
       <el-breadcrumb separator="/">
       <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
       <el-breadcrumb-item><a href="/">用户管理</a></el-breadcrumb-item>
       </el-breadcrumb> 
     </div>
     <div style="padding:10px 0">
       <el-input style="width:200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
        <!-- <el-input style="width:200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5"></el-input>
       <el-input style="width:200px" placeholder="请输入地址" suffix-icon="el-icon-position" class="ml-5"></el-input> -->
       <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      
     </div>

     <div style="margin:10px 0">
       <el-button type="primary">新增<i class="el-icon-circle-plus-outline"></i></el-button>
       <el-button type="danger">批量删除<i class="el-icon-remove-outline"></i></el-button>
       <el-button type="primary">导入<i class="el-icon-top"></i></el-button>
       <el-button type="primary">导出<i class="el-icon-bottom"></i></el-button>
     </div>






     <el-table :data="tableData" border stripe :header-cell-calss-name="headerBg" >
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
       <el-table-column prop="username" label="用户名" width="140"></el-table-column>
       <el-table-column prop="nickname" label="昵称" width="120"></el-table-column>
       <el-table-column prop="email" label="邮箱" ></el-table-column>
       <el-table-column prop="phone" label="电话" ></el-table-column>
       <el-table-column prop="address" label="地址"></el-table-column>
       <el-table-column label="操作" width="200" align="center">
         <template  >
           <el-button type="success">编辑 <i class="el-icon-edit"></i></el-button>
           <el-button type="danger">删除<i class="el-icon-remove-outline"></i></el-button>
         </template>
       </el-table-column>
     </el-table>
     <div style="padding:10px 0">
       <el-pagination
       @size-change="handleSizeChange"
       @current-change="handleCurrentChange"
         :current-page="pageNum"
         :page-sizes="[2, 4, 6, 10]"
         :page-size="pageSize"
         layout="total, sizes, prev, pager, next, jumper"
         :total="total">
       </el-pagination>
     </div>
   </el-main>
 </el-container>
</el-container>
</template>

<script>
export default {
 name:'HomeView',
 data() {
     return {
       tableData:[],
       total: 0 ,
       pageNum:1,
       pageSize:2,
       username:"",
       collapseBtnClass:'el-icon-s-fold' ,
       isCollapse:false,
       sideWidth:200 ,
       logoTextShow:true,
       headerBg: 'headerBg'
     }
   },
   created(){
    // 请求分页查询数据
   this.load()
   },
   methods:{
     collapse(){ //点击收缩按钮触发
       this.isCollapse=!this.isCollapse
       if(this.isCollapse){  //收缩
         this.sideWidth=64
         this.collapseBtnClass='el-icon-s-unfold'
         this.logoTextShow=false
       }else{  //展开
         this.sideWidth = 200 
         this.collapseBtnClass='el-icon-s-fold'
         this.logoTextShow=true 
       }

     },
     load(){
      fetch("http://localhost:8081/user/page?pageNum="+this.pageNum+ "&pageSize=" +this.pageSize+"&username="+this.username)
      .then(res=>res.json()).then(
      res=>{console.log(res)

      this.tableData=res.data

      this.total=res.total
      })

     },
    
     handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.load()
     },
     handleCurrentChange(pageNum){
      this.pageNum=pageNum
      this.load()
     }
   }
}
</script>
  1. 运行

npm run serve,打开 http://localhost:8080

在这里插入图片描述

分页查询OK!

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zero摄氏度

感谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值