這篇文章將為大家詳細(xì)講解有關(guān)怎么在mybatis中利用pageHelper實(shí)現(xiàn)一個分頁效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為左貢企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都網(wǎng)站建設(shè),左貢網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
1、jar包引入
我們項(xiàng)目中在依賴管理方面采用的是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é)果封裝在一個實(shí)體里面,就需要在增加一個分頁實(shí)體類: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">賣點(diǎn)</th> <th data-options="field:'price',width:70,align:'right',formatter:TAOTAO.formatPrice">價(jià)格</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>
關(guān)于怎么在mybatis中利用pageHelper實(shí)現(xiàn)一個分頁效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享標(biāo)題:怎么在mybatis中利用pageHelper實(shí)現(xiàn)一個分頁效果
分享鏈接:http://chinadenli.net/article28/ppdijp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、域名注冊、App開發(fā)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站策劃、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)