mybatis插件pageHelper實現(xiàn)分頁效果
最近做的一個項目在持久層我們采用的是Mybatis今天完成了商品列表的分頁查詢的功能,這篇博客我分享一下如何采用pageHelper的插件實現(xiàn)分頁。mybatis的應(yīng)用,最大的好處就在于我們可以更加方便靈活的編寫我們的sql語句,實現(xiàn)對單表或者多表的增刪改查,在這基礎(chǔ)上我們使用pageHelper插件實現(xiàn)分頁更加方便了我們對項目的開發(fā),提高了開發(fā)效率,我們以實現(xiàn)商品列表的查詢?yōu)楸尘埃敿?xì)介紹一下如何應(yīng)用這個插件簡單的實現(xiàn)分頁功能。
1、jar包引入
我們項目中在依賴管理方面采用的是Maven,所以想要引入分頁的jar包,我們需要配置三坐標(biāo):
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
2、配置mybatis的攔截器:
<configuration> <!-- 配置分頁插件 --> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <!-- 設(shè)置數(shù)據(jù)庫類型 --> <property name="dialect" value="mysql"/> </plugin> </plugins> </configuration>
3、編寫service層
頁面采用的是easyUI的框架,頁面接收數(shù)據(jù)采用的是json格式,所以在數(shù)據(jù)傳輸過程中,我們把最終的結(jié)果封裝在一個實體里面,就需要在增加一個分頁實體類:EUDataGridResult
package com.taotao.common.pojo;
import java.util.List;
public class EUDataGridResult {
//結(jié)果總數(shù)
private long total;
//結(jié)果行數(shù)
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;
}
}
編寫業(yè)務(wù)層代碼,增加分頁處理,設(shè)置返回對象:
/**
* 分頁查詢商品列表信息
*/
@Override
public EUDataGridResult getItemByList(int page, int rows) {
//查詢商品列表
TbItemExample example=new TbItemExample();
//分頁處理
PageHelper.startPage(page, rows);
List<TbItem> list=itemMapper.selectByExample(example);
//創(chuàng)建一個返回值對象
EUDataGridResult result=new EUDataGridResult();
//設(shè)置返回結(jié)果
result.setRows(list);
//設(shè)置返回的總記錄數(shù)
PageInfo<TbItem> pageInfo=new PageInfo<>(list);
result.setTotal(pageInfo.getTotal());
return result;
}
4、編寫前端控制層controller代碼:
Controller中主要功能是接收頁面?zhèn)鬟^來的參數(shù),并且返回json類型的數(shù)據(jù)結(jié)果:
/**
* 分頁查詢商品信息列表
* @param page
* @param rows
* @return
*/
@RequestMapping("/item/list")
@ResponseBody
public EUDataGridResult getItemList(Integer page,Integer rows){
EUDataGridResult result=itemService.getItemByList(page, rows);
return result;
}
5、jsp的頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<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">商品標(biāo)題</th>
<th data-options="field:'cid',width:100">葉子類目</th>
<th data-options="field:'sellPoint',width:100">賣點</th>
<th data-options="field:'price',width:70,align:'right',formatter:TAOTAO.formatPrice">價格</th>
<th data-options="field:'num',width:70,align:'right'">庫存數(shù)量</th>
<th data-options="field:'barcode',width:100">條形碼</th>
<th data-options="field:'status',width:60,align:'center',formatter:TAOTAO.formatItemStatus">狀態(tài)</th>
<th data-options="field:'created',width:130,align:'center',formatter:TAOTAO.formatDateTime">創(chuàng)建日期</th>
<th data-options="field:'updated',width:130,align:'center',formatter:TAOTAO.formatDateTime">更新日期</th>
</tr>
</thead>
</table>
6、最后的實現(xiàn)結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何解決freemarker靜態(tài)化生成html頁面亂碼的問題
這篇文章主要介紹了如何解決freemarker靜態(tài)化生成html頁面亂碼的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Java中ShardingSphere分庫分表實戰(zhàn)
我們做項目的時候,數(shù)據(jù)量比較大,單表千萬級別的,需要分庫分表,本文主要介紹了Java中ShardingSphere分庫分表實戰(zhàn),感興趣的可以了解一下2021-09-09
SpringCloud OpenFeign自定義結(jié)果解碼器方式
這篇文章主要介紹了SpringCloud OpenFeign自定義結(jié)果解碼器方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

