逆向工程生成的代碼是不支持分頁處理的,如果想進行分頁需要自己編寫mapper,這樣就失去逆向工程的意義了。爲了提高開發效率可以使用mybatis的分頁插件PageHelper。
1、Mybatis分頁插件 - PageHelper說明
如果你也在用Mybatis,建議嘗試該分頁插件,這個一定是最方便使用的分頁插件。該插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫分頁。https://download.csdn.net/download/qq_18142501/10588992(下載鏈接)
2、使用方法
第一步:把PageHelper依賴的jar包添加到工程中。官方提供的代碼對逆向工程支持的不好,使用參考資料中的pagehelper-fix。
第二步:在Mybatis配置xml中配置攔截器插件:
<plugins>
<!-- com.github.pagehelper爲PageHelper類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
第二步:在代碼中使用
1、設置分頁信息:
//獲取第1頁,10條內容,默認查詢總數count
PageHelper.startPage(1, 10);
//緊跟着的第一個select方法會被分頁
List<Country> list = countryMapper.selectIf(1);
2、取分頁信息
//分頁後,實際返回的結果list類型是Page<E>,如果想取出分頁信息,需要強制轉換爲Page<E>,
Page<Country> listCountry = (Page<Country>)list;
listCountry.getTotal();
- 取分頁信息的第二種方法
//獲取第1頁,10條內容,默認查詢總數count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo對結果進行包裝
PageInfo page = new PageInfo(list);
//測試PageInfo全部屬性
//PageInfo包含了非常全面的分頁屬性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());
- 分頁測試
@Test public void testPageHelper() throws Exception { //初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); //獲得Mapper的代理對象 TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class); //設置分頁信息 PageHelper.startPage(1, 30); //執行查詢 TbItemExample example = new TbItemExample(); List<TbItem> list = itemMapper.selectByExample(example); //取分頁信息 PageInfo<TbItem> pageInfo = new PageInfo<>(list); System.out.println(pageInfo.getTotal()); System.out.println(pageInfo.getPages()); System.out.println(pageInfo.getPageNum()); System.out.println(pageInfo.getPageSize()); } |
Service層
參數:int page ,int rows
業務邏輯:查詢所有商品列表,要進行分頁處理。
返回值:EasyUIDataGridResult
@Override public EasyUIDataGridResult getItemList(int page, int rows) {
//設置分頁信息 PageHelper.startPage(page, rows); //執行查詢 TbItemExample example = new TbItemExample(); List<TbItem> list = itemMapper.selectByExample(example); //取分頁信息 PageInfo<TbItem> pageInfo = new PageInfo<>(list);
//創建返回結果對象 EasyUIDataGridResult result = new EasyUIDataGridResult(); result.setTotal(pageInfo.getTotal()); result.setRows(list);
return result; } |
發佈服務
表現層
引用服務:
1、初始化表格請求的url:/item/list
2、Datagrid默認請求參數:
- page:當前的頁碼,從1開始。
- rows:每頁顯示的記錄數。
- 響應的數據:json數據。EasyUIDataGridResult
@RequestMapping("/item/list") @ResponseBody public EasyUIDataGridResult getItemList(Integer page, Integer rows) { EasyUIDataGridResult result = itemService.getItemList(page, rows); return result; } |
可以設置服務超時時間:
服務調用超時時間默認1秒,