mybatis插件pageHelper實(shí)現(xiàn)分頁效果
最近做的一個(gè)項(xiàng)目在持久層我們采用的是Mybatis今天完成了商品列表的分頁查詢的功能,這篇博客我分享一下如何采用pageHelper的插件實(shí)現(xiàn)分頁。mybatis的應(yīng)用,最大的好處就在于我們可以更加方便靈活的編寫我們的sql語句,實(shí)現(xiàn)對單表或者多表的增刪改查,在這基礎(chǔ)上我們使用pageHelper插件實(shí)現(xiàn)分頁更加方便了我們對項(xiàng)目的開發(fā),提高了開發(fā)效率,我們以實(shí)現(xiàn)商品列表的查詢?yōu)楸尘?,詳?xì)介紹一下如何應(yīng)用這個(gè)插件簡單的實(shí)現(xiàn)分頁功能。
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é)果封裝在一個(gè)實(shí)體里面,就需要在增加一個(gè)分頁實(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)建一個(gè)返回值對象 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>
6、最后的實(shí)現(xiàn)結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatisPlus-QueryWrapper多條件查詢及修改方式
這篇文章主要介紹了MyBatisPlus-QueryWrapper多條件查詢及修改方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類型的數(shù)據(jù)
這篇文章主要介紹了SpringMVC參數(shù)的傳遞之如何接收List數(shù)組類型的數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Java Socket編程(五) 簡單的WEB服務(wù)器
Java Socket編程(五) 簡單的WEB服務(wù)器...2006-12-12SpringBoot中fastjson自定義序列化和反序列化的實(shí)戰(zhàn)分享
在fastjson庫中,為了提供靈活的序列化和反序列化機(jī)制,設(shè)計(jì)了一系列的擴(kuò)展點(diǎn),以下是在SpringBoot和SpringClould環(huán)境中對這些擴(kuò)展點(diǎn)的詳細(xì)介紹及其實(shí)戰(zhàn)使用,通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-07-07springboot 防止重復(fù)請求防止重復(fù)點(diǎn)擊的操作
這篇文章主要介紹了springboot 防止重復(fù)請求防止重復(fù)點(diǎn)擊的操作,URL 攔截器可以使用 spring 攔截器,但使用 spring,每個(gè)需要過濾的新 URL 都需要添加配置,因此這里使用 AOP 注解 的形式來實(shí)現(xiàn),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01