SpringBoot分頁查詢功能的實現(xiàn)方法
前言:
學習了SpringBoot分頁查詢的兩種寫法,一種是手動實現(xiàn),另一種是使用框架實現(xiàn)。現(xiàn)在我將具體的實現(xiàn)流程分享一下。
首先是手動實現(xiàn)分頁查詢:
先復習一下,SQL中的limit關(guān)鍵字,下面一行sql語句的意思是從第二個數(shù)據(jù)開始查,查詢出兩條數(shù)據(jù)
SELECT * FROM sys_user limit 1,2;
使用limit前一個參數(shù)pageNum是從第幾個數(shù)據(jù)開始查,后一個參數(shù)pageSize是查詢多少條數(shù)據(jù),
注意數(shù)據(jù)庫查詢pageNum=0代表第一個數(shù)據(jù)。
那么在Springboot中該如何寫呢?
controller:
// 分頁查詢 // 接口路徑:/user/page?pageNum=1&pageSize=10 // @RequestParam接收 // limit第一個參數(shù)= (pageNum-1)+pageSize @GetMapping("/page") public List<User> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize){ pageNum = (pageNum-1); return userMapper.selectPage(pageNum,pageSize); } }
mapper:
@Select("select * from sys_user limit #{pageNum},#{pageSize}") List<User> selectPage(Integer pageNum, Integer pageSize);
這樣就寫好了,測試一下:
完善一下controller,添加查詢 總條數(shù)的方法:
// 分頁查詢 // 接口路徑:/user/page?pageNum=1&pageSize=10 // @RequestParam接收 前端url // limit第一個參數(shù)= (pageNum-1)*pageSize @GetMapping("/page") public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){ pageNum = (pageNum-1)*pageSize; List<User> data = userMapper.selectPage(pageNum,pageSize); Integer total = userMapper.selectTotal(); Map<String,Object> res = new HashMap<>(); res.put("data",data); res.put("total",total); return res; }
接下來是關(guān)聯(lián)前端分頁和后臺數(shù)據(jù):
修改分頁頁面homeView:
使用fetch()請求分頁數(shù)據(jù)
created() { //請求分頁數(shù)據(jù) fetch("localhost:9090/user/page?pageNum=1&pageSize=2").then(res =>res.json()).then(res =>{ console.log(res) }) },
fetch得到的是一個字符串數(shù)據(jù),需要將其轉(zhuǎn)換成Json格式,使用console.log()打印數(shù)據(jù)
@JsonIgnore忽略某一個屬性的注解
修改前端頁面表格數(shù)據(jù):
表格數(shù)據(jù)對應數(shù)據(jù)庫的屬性
<el-table :data="tableData" border stripe :header-cell-class-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>
修改前端數(shù)據(jù)內(nèi)容:
data() { return { tableData: [], //表格的值默認為空數(shù)組 total: 0, //查詢條數(shù)默認為0 msg: "hello 阿晴", collapseBtnClass: 'el-icon-s-fold', isCollapse: false, sideWidth: 200, logoTextShow: true, headerBg: 'headerBg' } },
渲染頁面:
created() { //請求分頁數(shù)據(jù) fetch("http://localhost:9090/user/page?pageNum=1&pageSize=2").then(res =>res.json()).then(res =>{ console.log(res) this.tableData = res.data this.total = res.total }) },
分頁函數(shù):
<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[2, 5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination> 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 }) }, handleSizeChange(pageSize){ console.log(pageSize) this.pageSize = pageSize this.load() }, handleCurrentChange(pageNum){ console.log(pageNum) this.pageNum = pageNum this.load() } }
實現(xiàn)分頁功能:
模糊查詢:
controller:
@GetMapping("/page") public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam String username){ pageNum = (pageNum-1)*pageSize; List<User> data = userMapper.selectPage(pageNum,pageSize,username); Integer total = userMapper.selectTotal(); Map<String,Object> res = new HashMap<>(); res.put("data",data); res.put("total",total); return res; }
mapper:
@Select("select * from sys_user where username like concat('%', #{username}, '%') limit #{pageNum},#{pageSize}") List<User> selectPage(Integer pageNum, Integer pageSize,String username);
測試一下:
在前端綁定:
<div style="margin: 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>
data() { return { tableData: [], total: 0, pageNum: 1, pageSize: 2, username: "", msg: "hello 阿晴", collapseBtnClass: 'el-icon-s-fold', isCollapse: false, sideWidth: 200, logoTextShow: true, headerBg: 'headerBg' } },
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 }) },
總結(jié)
到此這篇關(guān)于SpringBoot分頁查詢功能實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot分頁查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java循環(huán)刪除List元素報錯的原因分析與解決
大家在工作中應該都會遇到從List集合中刪除某一個或多個元素的業(yè)務場景,相信大家都會避開在循環(huán)里面刪除元素,使用其他方式處理,這是為什么呢,下面小編就來和大家詳細聊聊2023-11-11Maven中optional和scope元素的使用弄明白了嗎
這篇文章主要介紹了Maven中optional和scope元素的使用弄明白了嗎,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Java spring webmvc如何實現(xiàn)控制反轉(zhuǎn)
這篇文章主要介紹了Java spring webmvc如何實現(xiàn)控制反轉(zhuǎn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題
我們Pojo類的屬性名和數(shù)據(jù)庫中的字段名不一致的現(xiàn)象時有發(fā)生,本文就詳細的介紹一下Mybatis中ResultMap解決屬性名和數(shù)據(jù)庫字段名不一致問題,感興趣的可以了解一下2021-10-10