姘旀浜?/h3>
姘斿緱鎴戠洿鎺ュ垰鍒氳В鍐宠繖涓棶棰橈紝灏遍┈涓婃潵鍐欒繖绡囨枃绔犱簡
杩欐牱涔熷ソ锛屾垜鑳芥妸鏁翠釜杩囩▼瀹屾暣璁板綍涓嬫潵
閭e紑濮嬪惂
杩囩▼
鏈€杩戝湪鍋氭瘯璁惧槢锛岀敤鐨勬槸RuoYi-Vue锛岀敤璧锋潵鐪熺殑闈炲父鏂逛究锛屾瀬澶х殑鎻愰珮浜嗗紑鍙戞晥鐜囥€傚仛璧锋瘯璁炬垨鍏朵粬灏忛」鐩湡鐨勯潪甯稿鏄擄紝灏ゅ叾鍔犱笂瀹冪殑浠g爜鐢熸垚宸ュ叿锛岄潪甯告柟渚裤€傝€屼笖鎴戜笉鍏夋槸鍦ㄧ敤锛屽悓鏃跺叾瀹炰篃鏄湪瀛︿範锛屼粠涓繕鏄兘瀛﹀埌寰堝涓滆タ鐨勩€?/p>
杩樻湁涓€涓劅瑙﹀氨鏄浜庨暱鏈熻涓氬姟鎶樿吘鍒板崐姝伙紝瀵逛唬鐮佸け鍘讳俊蹇冪殑绋嬪簭鍛橈紝浠栦滑鐪嬪埌杩欐牱鐨勫紑婧愰」鐩紝鎴戠浉淇′粬浠偗瀹氭槸寮€蹇冿紝鍥犱负杩欑寮€婧愰」鐩唬鐮侀鏍间笂寰堢粺涓€銆佽鑼冩爣鍑嗘墽琛岀殑寰堝埌浣嶏紝鐪嬭捣鏉ョ湡鐨勮祻蹇冩偊鐩€?/p>
闂绠€杩?/h4>
濂戒簡锛屽洖鍒版棰橈紒
浠婂ぉ鍋氭瘯璁撅紝閫犳暟鎹祴璇曠殑鏃跺€欙紝鍙戠幇鍒嗛〉鍔熻兘澶辨晥浜嗭紝鍓嶇璋冪敤鍚庣鍒嗛〉鎺ュ彛杩斿洖鐨?code>total鏈夐棶棰?/p>
{total: 10,鈥
code: 200
msg: "鏌ヨ鎴愬姛"
rows: [{searchValue: null, createBy: null, createTime: "2022-03-18 20:36:55", updateBy: null,鈥,鈥
total: 10
total
鎬绘槸杩斿洖褰撳墠鏁版嵁鐨?code>size
鎴戝緢鏄笉瑙o紝鑷涓哄浜?code>ruoyi鍒嗛〉娴佺▼寰堟槸鐔熸倝锛屽鐓у畼缃戞壘闂锛屾劊鏄壘浜嗗崐澶╂病鍙戠幇闂
瀹樼綉锛氬悗鍙版墜鍐?| RuoYi
妫€鏌ヤ簡鍗婂ぉ娌″彂鐜颁粈涔堥棶棰橈紝鍙ソ鍏堝幓鎵炬壘鏈夋病鏈夐亣鍒板悓鏍烽棶棰樼殑锛屾灉鐒讹紝鏈変笉灏?/p>
- https://cloud.tencent.com/developer/article/1786290
- https://zhuanlan.zhihu.com/p/159199627
- https://blog.csdn.net/qq_40942359/article/details/121429234
- https://www.cnblogs.com/mantishell/p/13674818.html
- https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108017635
鐪嬪畬鍚庯紝澶ц嚧鏄庣櫧鏄€庝箞鍥炰簨锛岃繖鍏跺疄鏄?code>PageHelper鐨勯棶棰?/p>
姊崇悊娴佺▼
鎸夌収ruoyi
鐨勬祦绋嬬悊涓€閬?/p>
1銆佸悗绔垎椤垫帴鍙?code>SysUserController extends BaseController
/**
* 鑾峰彇鐢ㄦ埛鍒楄〃
*/
@PreAuthorize("@ss.hasPermi('system:user:list')")
@GetMapping("/list")
public TableDataInfo list(SysUser user) {
// 寮€鍚垎椤碉紝瑕佺揣鎺ョ潃鍒嗛〉锛坰ervice/mapper锛夛紝杩欓噷ruoyi杩涜浜嗗皝瑁?
startPage();
List<SysUser> list = userService.selectUserList(user);
return getDataTable(list);
}
鐐瑰紑startPage();
锛岃繖鏄埗绫?code>BaseController鐨勬柟娉?/p>
protected void startPage() {
// 鑾峰彇request涓叧浜庡垎椤电殑璇锋眰锛屽叿浣撴湁鍏充簬涓€涓猄ervletUtils鐨勭被锛屽彲浠ヨ嚜琛屼簡瑙?
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
Boolean reasonable = pageDomain.getReasonable();
// 鐪熸鐨勫紑濮嬪垎椤?
PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
}
}
2銆佹潵鍒?code>SysUserServiceImpl
/**
* 鏍规嵁鏉′欢鍒嗛〉鏌ヨ鐢ㄦ埛鍒楄〃
*
* @param user 鐢ㄦ埛淇℃伅
* @return 鐢ㄦ埛淇℃伅闆嗗悎淇℃伅
*/
@Override
@DataScope(deptAlias = "d", userAlias = "u")
public List<SysUser> selectUserList(SysUser user) {
return userMapper.selectUserList(user);
}
鍙戠幇杩欓噷鐨?code>service灏变竴琛岋紵
PageHelper
灏辨槸鍦ㄨ繖鍙戞尌浜嗕綔鐢紝杩涜浜嗗垎椤?/p>
鍏跺疄鎴戠殑闂灏卞湪杩欓噷锛屼笉杩囨垜瑕佸厛鎶婂悗闈㈢殑娴佺▼璧板畬
3銆佹煡鍒版暟鎹垜浠洖鍒?code>SysUserController锛岃繖娆″叧娉ㄤ粬鐨?code>return getDataTable(list);
/**
* 鍝嶅簲璇锋眰鍒嗛〉鏁版嵁
*/
@SuppressWarnings({"rawtypes", "unchecked"})
protected TableDataInfo getDataTable(List<?> list) {
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setMsg("鏌ヨ鎴愬姛");
rspData.setRows(list);
rspData.setTotal(new PageInfo(list).getTotal());
return rspData;
}
杩欏氨鏄渶缁堢殑鐩稿簲缁撴灉锛屾棦鐒舵槸total
鍑洪棶棰橈紝閭e氨閲嶇偣鍏虫敞setTotal
鏂规硶锛岀洿鎺ョ偣杩?code>new PageInfo(list).getTotal()
public PageInfo(List<T> list) {
this(list, 8);
}
public PageInfo(List<T> list, int navigatePages) {
super(list);
this.isFirstPage = false;
this.isLastPage = false;
this.hasPreviousPage = false;
this.hasNextPage = false;
if (list instanceof Page) {
Page page = (Page)list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.size = page.size();
if (this.size == 0) {
this.startRow = 0L;
this.endRow = 0L;
} else {
this.startRow = page.getStartRow() + 1L;
this.endRow = this.startRow - 1L + (long)this.size;
}
} else if (list instanceof Collection) {
this.pageNum = 1;
this.pageSize = list.size();
this.pages = this.pageSize > 0 1 : 0;
this.size = list.size();
this.startRow = 0L;
this.endRow = list.size() > 0 (long)(list.size() - 1) : 0L;
}
if (list instanceof Collection) {
this.calcByNavigatePages(navigatePages);
}
}
鏈€缁堣皟鐢ㄧ殑鏄笅闈㈢殑鏋勯€犳柟娉曪紝閭d箞灏辫繘鍒?code>super(list);
public PageSerializable(List<T> list) {
this.list = list;
// 鏄惁鏄疨age瀵硅薄
if (list instanceof Page) {
this.total = ((Page)list).getTotal();
} else {
this.total = (long)list.size();
}
}
杩欐槸com.github.pagehelper
鍖呬笅鐨勶紝灏辨槸鍦ㄨ繖閲屼负PageInfo
璁剧疆浜?code>total锛屽綋鐒朵篃鏄负鎴戜滑鏈€缁堢浉搴旇缃簡
锛燂紵锛?/p>
Page鍝噷鏉ョ殑
4銆丳age
package com.github.pagehelper;
public class Page<E> extends ArrayList<E> implements Closeable {
...
}
浠庤繖閲屽彲浠ョ湅鍑?code>Page缁ф壙浜?code>ArrayList涔熸槸灏卞疄鐜颁簡List
debug
缁撴灉濡備笅锛屽彂鐜板湪鎵ц瀹?code>service浠g爜鍚庯紝PageHelper
灏卞凡缁忓畬鎴愯繖涓€姝ワ紝閭d箞鑷劧灏辫缃ソ浜?code>total
[澶栭摼鍥剧墖杞瓨澶辫触,婧愮珯鍙兘鏈夐槻鐩楅摼鏈哄埗,寤鸿灏嗗浘鐗囦繚瀛樹笅鏉ョ洿鎺ヤ笂浼?img-tcoj9Laf-1648217426077)(https://cdn.jsdelivr.net/gh/wnhyang/clouding@master/cloud-picture/image.2n0r5fmnpj80.webp)]
涓婇潰鏄甯告祦绋嬫⒊鐞嗭紝鎺ヤ笅鐪嬫垜鐨勪唬鐮侊紝鍝噷鍑轰簡闂锛屽鑷?code>total鎬绘槸閿欑殑
闂涓庤В鍐?/h4>
涓婇潰鏄妇寰?code>ruoyi鍘熸湰鐨勪緥瀛愶紝鍥犱负鎴戞病鏈夋敼鍔紝鎵€浠ヤ篃灏辨病鏈夐棶棰?/p>
鍦ㄦ垜鐨勬ā鍧椾笂锛屾垜鍏朵粬鐨勪唬鐮佸疄鐜颁篃鐩稿悓锛屽氨鏄湪service
涓婂張澶勭悊浜嗕竴娆?/p>
/**
* 鏌ヨ浠诲姟鍒楄〃
*
* @param task 浠诲姟
* @return 浠诲姟
*/
@Override
public List<Task> selectTaskList(Task task) {
return taskMapper.selectTaskList(task).stream().map(this::fill).collect(Collectors.toList());
}
杩欓噷鐨?code>fill灏辨槸鍙堜竴涓~鍏呮暟鎹殑娴佺▼
鎵撳嵃sql鏃ュ織娌℃湁闂锛屾槸鏈夊垎椤垫祦绋嬬殑锛堟煡鎬绘暟锛岀劧鍚?code>limit锛?/p>
debug
鍙戠幇杩欓噷list
鍙樻垚浜嗭紝骞堕潪Page
锛屼篃灏辨妸total
璁剧疆涓?code>list.size()
[鍥剧墖涓婁紶澶辫触...(image-fca05e-1677945618204)]
鍏跺疄闂鏃╁氨鍦ㄦ煡鍒拌祫鏂欏悗鍙戠幇浜嗭紝浣嗘槸澶у鏁板鐞嗘柟娉曟垜閮界湅涓嶄笂锛屼粈涔堝啀璋冪敤PageHelper.startPage
锛岃浆鎹㈡垚PageInfo
涔嬬被鐨勶紝閮戒細璁╂垜鎬€鐤戞垜鏄惁鏈夊繀瑕佺敤浜?/p>
灏ゅ叾鏄湪宸茬粡浣跨敤浜?code>ruoyi涔嬪悗锛岄噸鏂板湪service鍋氶偅绠€鐩村お楹荤儲浜嗭紝鍥犱负閭f牱鎴戣繕闇€瑕佹妸ruoyi
灏佽鍦?code>Controller涓婄殑涓€浜涗笢瑗挎嬁杩囧幓锛屾姌鑵句笉浜?/p>
鏈€鍚庯紝鎵惧埌涓€绉嶉潪甯哥畝鍗曠殑鏂规硶瑙e喅浜?/p>
灏辨槸锛岀湅涓婇潰鐨勪唬鐮佸叾鏄張璋冪敤浜?code>stream鐨勮浆鎹㈡祦绋嬬殑锛屽彲鑳藉氨鏄湪杩欓噷PageHelper
鍑虹幇浜嗛棶棰?/p>
閭d箞璇曠潃灏?code>fill鏂规硶鍙樻垚void
锛屽弽姝d篃鏄~鍏呮暟鎹紝娌℃湁褰卞搷鐨勶紝鏀瑰姩鍚庡涓?/p>
@Override
public List<Task> selectTaskList(Task task) {
List<Task> taskList = taskMapper.selectTaskList(task);
taskList.forEach(this::fill);
return taskList;
}
杩欐debug
灏辨槸Page
瀵硅薄浜嗭紝涓€鍒囬兘姝e父浜?/p>
鎬荤粨
濡傛灉浣犳垚鍔熷甫鍏ユ垜鐨勮妭濂忥紝閭e緢濂斤紝浣犺鐪熺湅浜嗭紱濡傛灉浣犲彂鐜板叾涓棶棰樹簡锛岄偅鏇村ソ浜嗭紝娆㈣繋鎸囨锛涘鏋滀綘杩樿兘鍙戠幇鎴戝彟涓€涓兂璁ㄨ鐨勯棶棰橈紝閭f垜鏇村紑蹇冧簡
鎴戞兂璁ㄨ鐨勫氨钘忓湪
鍙戠幇杩欓噷鐨?code>service灏变竴琛岋紵
杩欏彞璇濋噷浜?/p>
寰堝鎬垜浠殑涓氬姟閫昏緫浠g爜涓嶉兘鏄嚑鍗佽锛屼笂鐧捐鐨勫悧锛燄煠?/p>
涓轰粈涔?code>service灏变竴琛?code>mapper锛?/p>
绡囧箙闂锛屼竴涓嬪湪璁ㄨ鍚э紒锛侊紒馃榿