2021-08-11

本文介绍了如何使用SpringBoot后端和Vue前端实现分页功能。在Vue中,利用Element UI的el-pagination组件进行页面展示,并通过axios发送POST请求,传递当前页数和每页大小到后端。后端使用SpringBoot,通过接收参数并调用UserService的queryPage方法进行数据查询和分页处理。
摘要由CSDN通过智能技术生成

SpringBoot-Vue前后端 分页功能实现DEMO

vue main.js

import Vue from 'vue'
import App from './App'
import router from './router'
import elementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'element-ui/lib/theme-chalk/display.css'

Vue.use(elementUI)

import axios from 'axios'
Vue.prototype.axios = axios

import qs from 'qs';
Vue.prototype.qs = qs;

前后端 分页功能实现

前端
vue页码分页
<div class="pages">
      <el-pagination
        background
        :disabled = "disablePage"
        :current-page.sync="currentPage"  //当前1
        small
        layout="prev, pager, next"
        :page-size="pageSize" //条数5
        :total="total"
        @current-change="handleCurrentChange"> //当前页功能
      </el-pagination>
</div>

tableData:[]

postData

qs.stringify
page: this.currentPage,

handleCurrentChange() {
        console.log(`当前页: ${this.currentPage}`);
        let postData = this.qs.stringify({
          page: this.currentPage,
       <!--userName: this.search_use-->
        });
        this.axios({
          method: 'post',
          url:'/page',
          data: postData
        }).then(response =>
        {
          this.tableData = response.data;
        }).catch(error =>
        {
          console.log(error);
        });
      },

page

handleCurrentChange

后端
controller
/**
     * 分页
     * @return
     */
    @RequestMapping(value="/page")
    @ResponseBody
    public List<User> page(String userName,String sex,String companyName,Integer page){
        int pageNow = page == null ? 1 : page;
        int pageSize = 5;
        int startRows = pageSize*(pageNow-1);
        return userService.queryPage(userName,sex,companyName,startRows);
    }

mapper
<select id="sqPage" resultMap="BaseResultMap" >
        select * from user
        <where>
            deleteflag != 'Y'
            <if test="userName != null and userName !=''">
                and user_name like concat('%', #{userName}, '%')
            </if>
            <if test="companyName != null and companyName !=''">
                and company_name = #{companyName}
            </if>
            <if test="sex != null and sex !=''">
                and sex = #{sex}
            </if>
        </where>

        order by user_id <!-- desc-->
        limit #{startRows},5   <!-- 分页每页条数-->

    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值