简介
本文应用于ssm框架,解决controller接口返回mybatis-plus封装的IPage类型与Vue.element-ui前端的接收与分页
mybatis-plus中的IPage与Page类
首先上源码(部分)
接口Ipage类型:
IPage的实现类Page:
源码可见Page是IPage的实现类,可见想使用mybatis-plus的封装分页则要明白Page类的属性
Page类的属性介绍:
records 用来存放查询出来的数据
total 返回记录的总数
size 每页显示条数,默认 10
current 当前页,默认1
orders 排序字段信息
optimizeCountSql 自动优化 COUNT SQL,默认true
isSearchCount 是否进行 count 查询,默认true
hitCount 是否命中count缓存,默认false
实战实现
后端接口:
1 | "/getOtherWorkList") ( |
形参中size和current分别代表每页容量、当前页与Page类中属性名对应,形参能使用参数Page
方便理解可以简化成以下代码1
2
3
4
5"/getOtherWorkList") (
public IPage<Work> getOtherWorkByParam(Page<Work> page){
Page<Work> workPage = workService.page(page);
return workPage;
}
前端请求发送与数据分页
前端分页
分页组件:1
2
3
4
5
6
7
8
9
10
11<div class="pagination">
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="page.current"
:page-sizes="[1, 5, 10, 20]"
:page-size="page.size"
layout="total, sizes, prev, pager, next, jumper"
:total="pageTotal">
</el-pagination>
</div>
table组件与处理数据分页:
注意:1
:data="records.slice((page.current-1)*page.size,page.current*page.size)"
请务必加上,page.current和page.size取决于你自己vue定义的数据属性
1 | <el-table |
Vue定义绑定数据:
注意:Page数据在records中,注意看Page源码属性类型介绍1
2
3
4
5
6
7
8
9
10
11data() {
return {
tableData: [],
records: [],
multipleSelection: [],
page: {
size: 1,
current: 1
},
workParam: {},
};
Vue请求发送与接收数据:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23created() {
this.getData();
},
methods: {
// 获取 easy-mock 的模拟数据
async getData() {
this.$http
.get(`/api/work/getOtherWorkList`, {
params: {
size: this.page.size,
current: this.page.current
},
headers: {
token: this.token,
},
})
.then((response) => {
this.records = response.data.records;
console.log(this.records);
this.pageTotal = response.data.total;
console.log(this.pageTotal);
})
},
选择页面size事件方法1
2
3
4
5
6handleSizeChange(newSize){
console.log(newSize)
this.page.size = newSize;
console.log(`目前页面容量`+this.query.pageSize)
this.getData()
},
选择页码事件方法1
2
3
4
5handleCurrentChange(newPage){
console.log(newPage)
this.page.current = newPage;
this.getData()
},
实现效果展示
后端分页
数据量较大每次由后端分页并发送给前端
后端分页代码发送前端数据代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/*
** PageUtil.listToPage()方法作用为将list转化为Page类对象
*/
@GetMapping("/noParams/getTableDataChartData")
public Page<BidInfor> getTableData(){
EchartsVo echartsVo = new EchartsVo();
// 查看redis缓存
if (redisService.get(Constant.REDIS_CACHE_SYS_NO_PARAMS_TABLE_CHARTS) != null) {
String echartsVojSON = redisService.get(Constant.REDIS_CACHE_SYS_NO_PARAMS_TABLE_CHARTS).toString();
echartsVo = JSONObject.parseObject(echartsVojSON, EchartsVo.class);
List<BidInfor> bidInforList = echartsVo.getTableData();
return PageUtil.listToPage(bidInforList, 1, 100);
}
List<BidInfor> bidInforList = bidInforService.tableDataOrderByAmount();
echartsVo.setTableData(bidInforList);
redisService.set(Constant.REDIS_CACHE_SYS_NO_PARAMS_TABLE_CHARTS, echartsVo);
return PageUtil.listToPage(bidInforList, 1, 100);
}
/*
** 简化代码
*/
@GetMapping("/noParams/getTableDataChartData")
public Page<BidInfor> getTableData(){
List<BidInfor> bidInforList = bidInforService.tableDataOrderByAmount();
return PageUtil.listToPage(bidInforList, 1, 100);
前端部分1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 </el-table>
<div class="pagination">
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="page.current"
:page-sizes="[100, 500, 1000]"
:page-size="page.size"
layout="total, sizes, prev, pager, next, jumper"
:total="pageTotal"
>
</el-pagination>
</div>
data() {
return {
records: [],
pageTotal: null,
total: null,
page: {
size: 100,
current: 1
}
}
}
methods: {
handleCurrentChange(newPage) {
console.log(newPage)
this.page.current = newPage
this.likeSearch()
}
likeSearch() {
this.$axios
.post('/xxxxx, {
frontParamVo: this.frontParamVo,
current: this.page.current,
size: this.page.size,
dataType: 'json',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
})
.then(res => {
res = res.data
this.records = []
this.records = res.records
this.pageTotal = res.total
})
.catch(error => {
console.log(error)
})
},
}
...
...
This is copyright.