现在需要新加一个需求:实现分页查询,模糊查询,(例如通过用户名查询出数据) 从上往下修改
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、运行测试
- 启动主项目,进入swagger页面测试接口,http://localhost:8081/swagger-ui.html
- 测试分页接口
结果:
测试成功,成功取到符合条件的数据并分页
6、前端实现分页
- 从后端请求到数据,并显示在前端页面上
- 遇到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);
}
}
- 实现分页。分页按钮,跳转时有响应
<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>
- 实现输入用户名搜索
<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>
- 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>
- 运行
npm run serve,打开 http://localhost:8080
分页查询OK!