SpringBoot分頁查詢功能的實現(xiàn)方法
前言:
學(xué)習(xí)了SpringBoot分頁查詢的兩種寫法,一種是手動實現(xiàn),另一種是使用框架實現(xiàn)?,F(xiàn)在我將具體的實現(xiàn)流程分享一下。
首先是手動實現(xiàn)分頁查詢:
先復(fù)習(xí)一下,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得到的是一個字符串?dāng)?shù)據(jù),需要將其轉(zhuǎn)換成Json格式,使用console.log()打印數(shù)據(jù)

@JsonIgnore忽略某一個屬性的注解
修改前端頁面表格數(shù)據(jù):
表格數(shù)據(jù)對應(yīng)數(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: [], //表格的值默認(rèn)為空數(shù)組
total: 0, //查詢條數(shù)默認(rèn)為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中http請求之restTemplate配置超時時間問題解決
這篇文章主要介紹了java中http請求之restTemplate配置超時時間,本文給大家分享三種解決方法,結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
如何使用BigDecimal實現(xiàn)Java開發(fā)商業(yè)計算
這篇文章主要介紹了如何使用BigDecimal實現(xiàn)Java開發(fā)商業(yè)計算,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
SpringBoot集成logback打印彩色日志的代碼實現(xiàn)
Logback是由log4j創(chuàng)始人設(shè)計的另一個開源日志組件,默認(rèn)情況下,Spring?Boot會用Logback來記錄日志,并用INFO級別輸出到控制臺,本文給大家介紹了SpringBoot集成logback打印彩色日志,需要的朋友可以參考下2024-03-03
Eclipse創(chuàng)建java程序可執(zhí)行jar包教程
這篇文章主要為大家分享了Eclipse創(chuàng)建java程序可執(zhí)行jar包教程,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下2016-05-05

