Springboot+mybatis使用PageHelper实现vue前端分页

1、未分页前的vue前端效果图

在这里插入图片描述

2、Springboot+mybatis使用PageHelper分页逻辑:

(1)Springboot、mybatis、PageHelper的版本:

在这里插入图片描述
在这里插入图片描述

(2)yml文件配置pagehelper:

pagehelper:
    # 分页插件会自动检测当前的数据库链接,自动选择合适的分页方式(可以不设置)
    helper-dialect: mysql
    # 上面数据库设置后,下面的设置为true不会改变上面的结果(默认为true)
    auto-dialect: true
    page-size-zero: false
    reasonable: true
    # 默认值为 false,该参数对使用 RowBounds 作为分页参数时有效。(一般用不着)
    offset-as-page-num: false
    # 默认值为 false,RowBounds是否进行count查询(一般用不着)
    row-bounds-with-count: false
    # 用于控制默认不带 count 查询的方法中,是否执行 count 查询,这里设置为true后,total会为-1
    default-count: false
    supportMethodsArguments: true
    params: count=countSql
    autoRuntimeDialect: true

此处我是配置在spring层级下的:
在这里插入图片描述

(3)controller、serviceimpl层级逻辑:

controller层:要接收前端传的当前页、页面数据量,没有就设置默认值,避免出现异常。
在这里插入图片描述

impl层:Mapper以及Service都不需要更改,直接在impl中加入分页方法即可。
PageHelper.startPage(pageNum, pageSize);
在这里插入图片描述
eg:PageHelper.startPage(2, 10)==>即查询第二页的数据,每页是10条数据。

3、前端vue的分页逻辑:

(1)外部:

如果传入了 page-size,且布局包含 page-size 选择器(即 layout 包含 sizes),必须监听 page-size 变更的事件。

<Switch v-model:value="status" /> //v-model等价于@update:value="status=$event"

<el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :small="small"
                :disabled="disabled"
                :background="background"
                layout="sizes, prev, pager, next"
                :total="countProducts"
                @size-change="handleSizeChange"
                @current-change="handleSizeChange"
        />

在这里插入图片描述

(2)内部:

前端设置默认赋值:

data () {
            return {
                // accountInfo数据绑定对象
                productInfos: [],
                show: true,
                countProducts: 20,//总数据默认20条
                currentPage: 1,//默认当前第一页
                pageSize: 10,//页面大小,默认十条
                selectMode: 'single'
            }
        }

在这里插入图片描述

内部的请求方法:

进入列表时自动请求展示数据(第一页的10条数据,以及获取总数分页码)

productInfo() {
                const proInfo = this.$http.post('productByPage')
                proInfo.then((dd) => {
                    const res = dd
                    console.log(res)
                    if (res.data.code !== 1) {
                        alert("发生某些错误,请重试")
                        this.$router.push('/userIndex')
                    }
                    // turn in /home
                    if (res.data.code === 1)
                        this.productInfos = res.data.data
                    console.log(this.productInfos)
                })

                const countProduct = this.$http.post('productTotal')
                countProduct.then((dd) => {
                    const res = dd
                    console.log(res)
                    if (res.data.code !== 1) {
                        alert("发生某些错误,请重试")
                        this.$router.push('/userIndex')
                    }
                    // turn in /home
                    if (res.data.code === 1)
                        this.countProducts = res.data.data
                    console.log(this.countProducts)
                })
            }

在这里插入图片描述

切换页码时下一页或者指定页码的请求方法:此处切换页面数据量大小(外部对应@current-change=“handleSizeChange”)和切换页码(外部对应@size-change=“handleSizeChange”)均用此方法。

handleSizeChange(){
                const proInfo = this.$http.post('productByPage?pageNum='+ this.currentPage + '&pageSize=' +this.pageSize)
                proInfo.then((dd) => {
                    const res = dd
                    console.log(res)
                    if (res.data.code !== 1) {
                        alert("发生某些错误,请重试")
                        this.$router.push('/userIndex')
                    }
                    // turn in /home
                    if (res.data.code === 1)
                        this.productInfos = res.data.data
                    console.log(this.productInfos)
                })

                const countProduct = this.$http.post('productTotal')
                countProduct.then((dd) => {
                    const res = dd
                    console.log(res)
                    if (res.data.code !== 1) {
                        alert("发生某些错误,请重试")
                        this.$router.push('/userIndex')
                    }
                    // turn in /home
                    if (res.data.code === 1)
                        this.countProducts = res.data.data
                    console.log(this.countProducts)
                })
            }

在这里插入图片描述

4、分页后的vue前端效果图

在这里插入图片描述
在这里插入图片描述

至此,Springboot+mybatis使用PageHelper实现了vue前端分页功能(支持页码切换和展示的数据量切换)。

参考资料:
1、vue-element3官方分页组件使用:https://element-plus.gitee.io/zh-CN/component/pagination.html
2、你不知道的PageHelper:(主要参考配置文件需要配置的参数)https://zhuanlan.zhihu.com/p/77147210

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴能为您提供帮助!下面是一个简单的SpringBoot+MyBatis+MySQL+Vue实现图书管理系统的步骤: 1. 创建SpringBoot项目 使用Spring Initializr创建一个新的SpringBoot项目,并添加web、mybatis和mysql依赖。 2. 配置MySQL数据库 在application.properties文件中添加MySQL数据库连接信息,包括数据库URL、用户名、密码等。 3. 创建数据库表 在MySQL中创建一个名为books的数据库表,用于存储图书信息,包括图书ID、图书名称、图书作者、图书出版社等字段。 4. 创建MyBatis映射文件 在src/main/resources目录下创建一个名为mapper的文件夹,用于存放MyBatis映射文件。创建一个名为BookMapper.xml的映射文件,定义对books表的CRUD操作。 5. 创建Book实体类 在src/main/java目录下创建一个名为entity的包,用于存放实体类。创建一个名为Book的实体类,属性包括图书ID、图书名称、图书作者、图书出版社等。 6. 创建BookService接口和实现类 在src/main/java目录下创建一个名为service的包,用于存放服务接口和实现类。创建一个名为BookService的接口,并定义对books表的CRUD操作。创建一个名为BookServiceImpl的实现类,实现BookService接口。 7. 创建BookController类 在src/main/java目录下创建一个名为controller的包,用于存放控制器类。创建一个名为BookController的控制器类,定义RESTful API接口,包括查询图书列表、添加图书、修改图书和删除图书等。 8. 创建前端页面 使用Vue.js创建一个前端页面,包括图书列表、添加图书、修改图书和删除图书等功能。 9. 测试运行 启动SpringBoot应用程序,并在浏览器中测试RESTful API接口和前端页面功能。 以上是简单的实现步骤,具体实现过程和代码可以根据您的需要进行调整和修改。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值