当前位置: 首页>编程语言>正文

ruoyi分页问题

姘旀浜?/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>

绡囧箙闂锛屼竴涓嬪湪璁ㄨ鍚э紒锛侊紒馃榿


https://www.xamrdz.com/lan/5z41997614.html

相关文章: