Mybatis學習(4):Mybatis及PageHelper外掛和easyUI實現分頁
前言
很多專案中需要將資料庫中的所有資料分頁展示出來,這樣的技術有很多。今天我們來介紹如何使用Mybatis及其PageHelper外掛和前端easyUI實現資料庫資訊分頁展示。
正文
一,easyUI
其實我對前端js一點都不瞭解,但是為了做專案硬著頭皮也要上,下面簡單介紹一下easyUI的資料表格(datagrid)。
<table class="easyui-datagrid" id="itemList" title="商品列表"
data-options="singleSelect:false,collapsible:true,pagination:true,
url:'/item/list',method:'get',pageSize:30,toolbar:toolbar" >
<thead>
<tr>
<th data-options="field:'ck',checkbox:true"></th>
<th data-options="field:'id',width:60">商品ID</th>
<th data-options="field:'title',width:200">商品標題</th>
<th data-options="field:'cid',width:100" >葉子類目</th>
<th data-options="field:'sellPoint',width:100">賣點</th>
<th data-options="field:'price',formatter:TAOTAO.formatPrice">價格</th>
<th data-options="field:'num',width:70,align:'right'">庫存數量</th>
<th data-options ="field:'barcode',width:100">條形碼</th>
<th data-options="field:'status'">狀態</th>
<th data-options="field:'created'">建立日期</th>
<th data-options="field:'updated'">更新日期</th>
</tr>
</thead>
</table>
這就是easyUI修飾的table標籤,我們來看幾個常用的屬性(data-options包裹的屬性):
1,singleSelect :如果為true,則只允許選擇一行。
2,pagination :如果為true,則在DataGrid控制元件底部顯示分頁工具欄。
3,url :一個URL從遠端站點請求資料。
4,method :該方法型別請求遠端資料。
5,pageSize : 在設定分頁屬性的時候初始化頁面大小。
我們來看一下表格長什麼樣:
這樣,從頁面點選“查詢商品”,即訪問表格的時候的時候,就會產生一個URL:
http://localhost:8080/item/list?page=1&rows=30
而且我們點下一頁的按鈕,同樣會產生一個URL
http://localhost:8080/item/list?page=2&rows=30
http://localhost:8080/item/list?page=3&rows=30
http://localhost:8080/item/list?page=4&rows=30
我們可以使用Controller來捕獲這個url並進行處理,後面會講到。
而easyUI的DataGrid(資料表格)只接受JSON格式的返回值,並且要求的json格式為:
{total:"100",rows:[{"id":"1" , "name":"aaa"},{"id":"2" , "name":"bbb"}]}
第一個元素是資料庫中的總條目數,第2個原始是個列表,列表中是每一條具體的記錄,內容是資料庫的列名和值。
二,Mybatis
上一篇我們使用了Mybatis逆向生成程式碼,其中生成的Mapper介面中就有根據條件查詢資料庫記錄的方法,不指定條件就是查詢所有。本文就使用這個方法。
三,PageHelper外掛
該外掛可實現對Mybatis封裝的sql語句進行攔截,從而實現分頁查詢的功能。
實現原理:
使用步驟:
1,引入PageHelper的jar包。
2,需要在SqlMapConfig.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>
3,在查詢sql語句執行前,新增一行程式碼:
PageHelper.startPage(page, rows) //page:要顯示第幾頁,rows:每頁顯示的記錄數
這樣,查詢結果就能按指定的引數,將表分頁顯示出來。
4,獲取查詢結果的總量
第3,4步的結果封裝成物件,再返回給前端以json格式。
四,完整的流程。
1,dao層
該層就用Mapper介面中的查詢方法
public interface TbItemMapper {
List<TbItem> selectByExample(TbItemExample example);
}
2,json對應的pojo類
service層將查詢到的總記錄數和每頁的記錄封裝進該物件,並返回給controller。
public class EUDataGridResult {
private long total;
private List<?> rows;
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}
3,service層
public class ItemServiceImpl implements ItemService {
@Autowired
private TbItemMapper itemMapper;
@Override
public EUDataGridResult getItemList(int page, int rows) {
//查詢商品列表
TbItemExample example = new TbItemExample();
//分頁處理
PageHelper.startPage(page, rows);
List<TbItem> list = itemMapper.selectByExample(example);
//建立一個返回值物件
EUDataGridResult result = new EUDataGridResult();
result.setRows(list);
//取記錄總條數
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
result.setTotal(pageInfo.getTotal());
return result;
}
}
4,web層
url中的page和rows屬性會自動封裝進控制器方法的入參中,返回值型別就是上面的pojo型別,但是@ResponseBody註解將其轉化為對應的json格式,前端收到json就能自動解析進表格了。
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/item/list")
@ResponseBody
public EUDataGridResult getItemList(Integer page, Integer rows) {
EUDataGridResult result = itemService.getItemList(page, rows);
return result;
}
}
總結
分頁查詢是很常見的,應掌握。