当前位置: 首页 > news >正文

Springboot3+vue3实现增删改查、分页查询、批量删除(上)

安装 axios 封装前后端对接数据工具

npm i axios -S

屏幕截图 2026-02-14 155922

简单测试 axios

axios.get("http://localhost:9999/admin/selectAll").then(res =>{console.log(res)
})

跨域处理

跨域错误:

屏幕截图 2026-02-14 161230

在 Springboot 里面设置统一的跨域处理

package com.example.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 {@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.addAllowedOrigin("*"); //1 设置访问源地址corsConfiguration.addAllowedHeader("*"); //2 设置访问源请求头corsConfiguration.addAllowedMethod("*");//3 设置访问源请求方法source.registerCorsConfiguration("/**", corsConfiguration);//4 对接口配置跨域设置return new CorsFilter(source);}
}

设置完记得重启

封装统一的请求工具 request.js

import axios  from "axios";
import {ElMessage} from "element-plus";const request = axios.create({baseURL:'http://localhost:9999',timeout:30000 //后台接口超过时间
})//request 拦截器
//可以自请求发送前对请求做一些处理
request.interceptors.request.use(config => {config.headers['Content-Type'] = 'application/json;charest=utf-8';return config
},error => {return Promise.reject(error)
});//respond 拦截器
//可以在接口响应后统一处理结果
request.interceptors.response.use(response => {let res = response.data;//兼容服务端返回的字符串数据if (typeof res == 'string'){res = res ? JSON.parse(res) : res}return res;},error => {if (error.response.status === 404){ElMessage.error('未找到请求接口')}else if (error.response.status ===500){ElMessage.error('系统异常,请查看后端控制台报错')}else {console.error(error.message)}return Promise.reject(error)}
)export default request

测试前后端数据查询

使用 request 工具发起一次请求

  if (res.code === '200'){console.log(res)}else {ElMessage.error(res.msg)}
})

获取的数据样例

屏幕截图 2026-02-14 190229

分页查询

分页对象

total:数据库表的所有数据总的个数
list:返回的数据数组
pageNumber:当前的页码
pageSize:每页展示的个数

使用 PageHelper 插件

在 pom.xml 里面添加依赖

      <!-- 分页插件pageHelper --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version><exclusions><exclusion><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></exclusion></exclusions></dependency>

后端返回的分页对象

屏幕截图 2026-02-14 200537

分页查询的请求方法

    <div class="card" style="margin-bottom: 5px"><el-table :data="data.tableData" style="width: 100%" :header-cell-style="{color:'#333',backgroundColor:'#eaf4ff'}"><el-table-column type="selection" width="55" /><el-table-column prop="username" label="账号" /><el-table-column prop="name" label="名称" /><el-table-column prop="phone" label="电话" /><el-table-column prop="email" label="邮箱" /></el-table><div class="card"><el-paginationv-model:current-page="data.pageNum"v-model:page-size="data.pageSize"layout="total, sizes,prev, pager, next,jumper":page-sizes="[5,10,20]":total="data.total"@current-change="load"@size-change="load"/></div></div>
const load = () => {request.get('/admin/selectPage',{params:{pageNum:data.pageNum,pageSize:data.pageSize}}).then(res => {if(res.code === '200'){data.tableData = res.data.listdata.total = res.data.total}else {ElMessage.error(res.msg)}})
}
load()

排查问题:页面数据少了,不展示怎么办?
看网络请求的数据对不对

屏幕截图 2026-02-14 202826

卸载 vue 的 dev-tools

 npm uninstall vite-plugin-vue-devtools

屏幕截图 2026-02-14 195755

条件查询

前端传入条件,后端输出结果,注意看 sql 语句

屏幕截图 2026-02-14 212803

屏幕截图 2026-02-14 212648

在网络请求的载荷里面有数据

屏幕截图 2026-02-14 213219

条件查询 sql 语句

<select id="selectAll" resultType="com.example.entity.Admin">select * from `admin`<where><if test="username != null">username like concat('%', #{username}, '%')</if><if test="name != null">and name like concat('%', #{name}, '%')</if> <!-- 相当于 like '%1%'--></where>order by id desc</select>

vue 调用:

const data = reactive({username:null,name:null,pageNum:1,pageSize:5,total:3,tableData:[]
})const load = () => {request.get('/admin/selectPage',{params:{pageNum:data.pageNum,pageSize:data.pageSize,username:data.username,name:data.name}}).then(res => {if(res.code === '200'){data.tableData = res.data.listdata.total = res.data.total}else {ElMessage.error(res.msg)}})
}
load()const reset = () => {data.username = nulldata.name = nullload()
}
http://www.jsqmd.com/news/382802/

相关文章:

  • 2026年 电磁阀厂家推荐排行榜:高频控制/高压/防爆/长通电/三通/半导体/先导/液压/高精度电磁阀及微小流量比例阀专业甄选 - 品牌企业推荐师(官方)
  • 2026年真空箱、隧道炉、垂直炉厂家实力推荐榜:工业热处理设备源头品牌深度解析与选购指南 - 品牌企业推荐师(官方)
  • 生产环境不用找售后!手机已删短信这样取,简单几步搞定最佳实践与性能优化
  • 2026年 智能设备连通性测试厂家推荐榜单:智能眼镜/耳机/价签/门锁/摄像头/音箱/开关专业测试服务深度解析 - 品牌企业推荐师(官方)
  • 信息论与编码篇---香农公式
  • 信息论与编码篇---DMS信源编码
  • 为什么你的AI提示设计没人用?提示工程架构师必须避开的7个用户参与误区
  • BUUCTF Misc 流量分析题单
  • 【信息科学与工程学】【管理科学】 第三篇 商业模式
  • 2026年云手机平台推荐榜单:安卓/iOS/多品牌/多型号/测试/办公/游戏/企业云手机平台,高效稳定智能云端解决方案 - 品牌企业推荐师(官方)
  • 自动控制原理 记录2
  • 【数据库】【Redis】缓存监控体系深度解析:从 BigKeys 到慢查询 - 详解
  • 2/14
  • 信息论与编码篇---信源编码
  • 2026年 毛绒印花/渗透印花/直喷渗透印花面料厂家推荐排行榜:创新工艺与高端质感面料品牌深度解析 - 品牌企业推荐师(官方)
  • 京东e卡回收超简单 几分钟就能搞定! - 京顺回收
  • FastAPI 与 Langchain、Coze、Dify 技术深度对比分析
  • AI产品必懂的100个概念(非常详细),RAG、Agent全解析,收藏这一篇就够了!
  • 基于TensorFlow的物联网大数据预测分析实战
  • 计算机毕业设计springboot浙江省历史文化遗产数字博物馆 基于SpringBoot框架的浙江非遗文化数字化展示与管理系统 浙江历史文化资源在线保护与传播平台的设计与实现
  • 计算机毕业设计springboot灵活用工系统 基于SpringBoot的弹性人力资源调度平台设计与实现 零工经济模式下智能用工匹配系统的开发与应用
  • 民银资本2025财报解码:1.7亿港元净利创纪录,资管+投行双轮驱动格局成型。
  • The Scientist
  • 2026年 安全阀厂家推荐排行榜:弹簧式/不锈钢/工业/燃料电池用安全阀,精准调节与高可靠性品牌深度解析 - 品牌企业推荐师(官方)
  • 2026年车库顶板排水系统厂家推荐排行榜:虹吸排水/零坡度排水/海绵城市种植顶板排水,创新技术与高效解决方案深度解析 - 品牌企业推荐师(官方)
  • 南大pa
  • 《7天学会Redis》Day 7 - Redisson 全览 - 实践
  • 1.创建实体类UserInfo
  • 细胞群体动力学仿真软件:CompuCell3D_(21).CompuCell3D在医学研究中的应用
  • rider开发asp.net webform工程