一.Mybatis-plus实现分页查询
1.Controller层
T:所要查询的对象实体
page:当前页码
limit:每页记录数
IPage pageModel = adService.selectPage(page, limit);
List records = pageModel.getRecords();
long total = pageModel.getTotal();
@ApiOperation("推荐类别分页列表")
@GetMapping("list/{page}/{limit}")
public R listPage(@ApiParam(value = "当前页码",required = true)@PathVariable long page,@ApiParam(value = "每页记录数",required = true)@PathVariable long limit){
IPage<AdVo> pageModel = adService.selectPage(page, limit);
List<AdVo> records = pageModel.getRecords();
long total = pageModel.getTotal();
return R.ok().data("total", total).data("rows", records);
}
2.ServiceImpl层
实现Service层的 selectPage接口
public IPage<AdVo> selectPage(long page, long limit) {
QueryWrapper<AdVo> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("a.type_id", "a.sort");
//创建分页对象
Page<AdVo> pageParam = new Page<>(page, limit);
List<AdVo> records = baseMapper.selectPageByQueryWrapper(pageParam, queryWrapper);
pageParam.setRecords(records);
return pageParam;
}
3.Mapper层
实现baseMapper.selectPageByQuery方法
@Param(Constants.WRAPPER) :相当于@Param(“ew”),与mapper.xml中${ew.customSqlSegment}拼接where子句
List<AdVo> selectPageByQueryWrapper(Page<AdVo> pageParam,@Param(Constants.WRAPPER) QueryWrapper<AdVo> queryWrapper);
}
4.mapper.xml
<select id="selectPageByQueryWrapper" resultType="com.atguigu.guli.service.cms.entity.vo.AdVo">
SELECT
a.id,
a.title,
a.sort,
t.title AS type
FROM
cms_ad a
LEFT JOIN cms_ad_type t
ON a.type_id = t.id
${ew.customSqlSegment}
</select>
二、Pagination分页组件
1.list.vue
<template>
<div>
<!-- 分页组件 -->
<el-pagination
:current-page="page"
:total="total"
:page-size="limit"
:page-sizes="[5, 10, 20, 30, 40, 50, 100]"
style="padding: 30px 0; text-align: center;"
layout="total, sizes, prev, pager, next, jumper"
@size-change="changePageSize"
@current-change="changeCurrentPage"
/>
</div>
</template>
export default {
// 定义数据模型
data() {
return {
list: [], // 列表
total: 0, // 总记录数
page: 1, // 页码
limit: 10 // 每页记录数
}
},
// 定义方法
methods: {
fetchData() {
// 调用api
adTypeApi.pageList(this.page, this.limit).then(response => {
this.list = response.data.rows
this.total = response.data.total
})
},
// 每页记录数改变,size:回调参数,表示当前选中的“每页条数”
changePageSize(size) {
this.limit = size
this.fetchData()
},
// 改变页码,page:回调参数,表示当前选中的“页码”
changeCurrentPage(page) {
this.page = page
this.fetchData()
},
附录:list.vue
<template>
<div class="app-container">
<!-- 工具按钮 -->
<div style="margin-bottom: 10px">
<router-link to="/ad/type-create">
<el-button type="primary" size="mini" icon="el-icon-edit">添加推荐位</el-button>
</router-link>
</div>
<!-- 表格 -->
<el-table :data="list" border stripe>
<el-table-column
label="#"
width="50">
<template slot-scope="scope">
{{ (page - 1) * limit + scope.$index + 1 }}
</template>
</el-table-column>
<el-table-column prop="title" label="推荐位名称" />
<el-table-column label="操作" width="200" align="center">
<template slot-scope="scope">
<router-link :to="'/ad/type-edit/'+scope.row.id">
<el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
</router-link>
<el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页组件 -->
<el-pagination
:current-page="page"
:total="total"
:page-size="limit"
:page-sizes="[5, 10, 20, 30, 40, 50, 100]"
style="padding: 30px 0; text-align: center;"
layout="total, sizes, prev, pager, next, jumper"
@size-change="changePageSize"
@current-change="changeCurrentPage"
/>
</div>
</template>
<script>
import adTypeApi from '@/api/adType'
export default {
// 定义数据模型
data() {
return {
list: [], // 列表
total: 0, // 总记录数
page: 1, // 页码
limit: 10 // 每页记录数
}
},
// 页面渲染成功后获取数据
created() {
this.fetchData()
},
// 定义方法
methods: {
fetchData() {
// 调用api
adTypeApi.pageList(this.page, this.limit).then(response => {
this.list = response.data.rows
this.total = response.data.total
})
},
// 每页记录数改变,size:回调参数,表示当前选中的“每页条数”
changePageSize(size) {
this.limit = size
this.fetchData()
},
// 改变页码,page:回调参数,表示当前选中的“页码”
changeCurrentPage(page) {
this.page = page
this.fetchData()
},
// 根据id删除数据
removeById(id) {
this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
return adTypeApi.removeById(id)
}).then((response) => {
this.fetchData()
this.$message.success(response.message)
}).catch(error => {
// 当取消时会进入catch语句:error = 'cancel'
// 当后端服务抛出异常时:error = 'error'
if (error === 'cancel') {
this.$message.info('取消删除')
}
})
}
}
}
</script>