前端分页
在mapper中书写对应数据库语句
这里刚开始的形参没有写@Param(“pageNum”),一直报错500,找了好久才改出来TAT
在Controller中写函数
前端分页测试
用PostMan进行测试,如下图所示即为成功
连接后端vue
在export里写函数
运行尝试报错,经典跨域问题
★★★经典跨域问题
从9090端口跨到8080端口出现问题,在SpringBoot中加上配置文件CorsConfig.java,再重启即可(spring项目也要重启,不然会一直报错),代码如下(此代码为允许任何源地址、允许任何头、允许任何方法请求后台接口)
(参照链接)
package com.ww.car.config;
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("*"); // 1 设置访问源地址
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
corsConfiguration.setMaxAge(MAX_AGE);
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
若想不展示密码部分,在实体类的密码属性前加上标签@JsonIgnore即可隐藏此部分,不展示给前端
前后对比
补充fetch为所需数据
刷新界面,可以看到数据库中的内容
分页具体设计
设置load函数获取数据
获取的包括后台数据、数据条数等信息,保存在res中
load(){
fetch("http://localhost:9090/user/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize)
.then(res => res.json()).then(res =>{
console.log(res)
this.tableData=res.data
this.total=res.total
})
},
同时将参数传入
前端即可实现分页设计
增加姓名模糊查找
模糊搜索时,用v-model绑定输入框,查询时用上即可,同时搜索按钮绑定点击触发函数
默认值为空
即可实现模糊查询
更新Home.vue代码
<template>
<div style="height:100%">
<el-container style="height: 100vh ">
<el-aside width="sideWidth+'px'" style="background-color: rgb(238, 241, 246);height:100% ;box-shadow: rgba(0,21,41,0.35) 2px 0px 6px;">
<el-menu :default-openeds="['1', '3']"
style="min-height:100%; overflow-x:hidden;"
background-color="rgb(251,185,197)"
active-text-color="rgb(255,224,230)"
:collapse-transition="false"
:collapse="isCollapse"
>
<div style="height:60px;line-height: 60px;text-align: center">
<img src="../assets/car.png" alt="" style="width:20px;position:relative;top:5px;margin-right: 5px;">
<b v-show="!isCollapse">小温的停车场管理系统</b>
</div>
<el-submenu index="1">
<template slot="title"><i class="el-icon-message"></i>
<span slot="title">导航一</span>
</template>
<el-menu-item-group>
<template slot="title">分组一</template>
<el-menu-item index="1-1">选项1</el-menu-item>
<el-menu-item index="1-2">选项2</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="1-3">选项3</el-menu-item>
</el-menu-item-group>
<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-menu-item-group>
<template slot="title">分组一</template>
<el-menu-item index="2-1">选项1</el-menu-item>
<el-menu-item index="2-2">选项2</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="2-3">选项3</el-menu-item>
</el-menu-item-group>
<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-menu-item-group>
<template slot="title">分组一</template>
<el-menu-item index="3-1">选项1</el-menu-item>
<el-menu-item index="3-2">选项2</el-menu-item>
</el-menu-item-group>
<el-menu-item-group title="分组2">
<el-menu-item index="3-3">选项3</el-menu-item>
</el-menu-item-group>
<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:20px">
<span :class="collapseBtnClass" style="cursor: pointer" @click="collapse"></span>
</div>
<el-dropdown style="width: 70px;cursor:pointer">
<span>小温</span>
<i class="el-icon-arrow-down" style="margin-left: 5px"></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:10px">
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
<el-breadcrumb-item>用户管理</el-breadcrumb-item>
</el-breadcrumb>
</div>
<div style="padding: 10px 0;margin-bottom:5px">
<el-input style="width: 280px" placeholder="请输入姓名" suffix-icon="el-icon-search" v-model="username"></el-input>
<el-input style="width: 280px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-10"></el-input>
<el-input style="width: 280px" placeholder="请输入地址" suffix-icon="el-icon-position" class="ml-10"></el-input>
<el-button class="ml-10" type="primary" @click="load">搜索</el-button>
</div>
<div class="ml-5" style="margin-bottom: 15px">
<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" style="margin-top: 10px " border stripe :row-class-name="tableRowClassName" >
<el-table-column prop="id" label="ID" width="80">
</el-table-column>
<el-table-column prop="username" label="车主姓名" width="120">
</el-table-column>
<el-table-column prop="nickname" label="昵称" width="140">
</el-table-column>
<el-table-column prop="email" label="邮箱">
</el-table-column>
<el-table-column prop="address" label="地址">
</el-table-column>
<el-table-column prop="carnum" label="车牌号">
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<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, 10, 15, 20]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</div>
</el-main>
</el-container>
</el-container>
</div>
</template>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
.headerBg{
background: #42b983!important;
}
</style>
<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
export default {
name: 'HomeView',
components: {
HelloWorld
},
data(){
return{
tableData: [],
total:0,
username:"",
pageNum:1,
pageSize:2,
msg:"hello 小温",
collapseBtnClass:'el-icon-s-fold',
isCollapse:false,
sideWidth:200
}
},
created() {
//请求分页查询数据
this.load()
},
methods:{
collapse(){ // 点击收缩按钮触发
this.isCollapse=!this.isCollapse
if(this.isCollapse){
this.sideWidth = 60
this.collapseBtnClass = 'el-icon-s-unfold'
}else{
this.sideWidth=200
this.collapseBtnClass='el-icon-s-fold'
}
},
load(){
fetch("http://localhost:9090/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){
console.log(pageSize)
this.pageSize=pageSize
this.load()
},
handleCurrentChange(pageNum){
console.log(pageNum)
this.pageNum=pageNum
this.load()
},
tableRowClassName({row, rowIndex}) {
if (rowIndex === 1) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
}
}
}
</script>