pageHelper实现分页
来源:互联网 发布:怎样下载视频软件 编辑:程序博客网 时间:2024/06/04 18:08
最近做的一个项目在持久层我们采用的是Mybatis今天完成了商品列表的分页查询的功能,这篇博客我分享一下如何采用pageHelper的插件实现分页。mybatis的应用,最大的好处就在于我们可以更加方便灵活的编写我们的sql语句,实现对单表或者多表的增删改查,在这基础上我们使用pageHelper插件实现分页更加方便了我们对项目的开发,提高了开发效率,我们以实现商品列表的查询为背景,详细介绍一下如何应用这个插件简单的实现分页功能。
1、jar包引入
我们项目中在依赖管理方面采用的是Maven,所以想要引入分页的jar包,我们需要配置三坐标:
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>${pagehelper.version}</version></dependency>
2、配置mybatis的拦截器:
<configuration> <!-- 配置分页插件 --><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 设置数据库类型 --><property name="dialect" value="mysql"/></plugin></plugins></configuration>
3、编写service层
页面采用的是easyUI的框架,页面接收数据采用的是json格式,所以在数据传输过程中,我们把最终的结果封装在一个实体里面,就需要在增加一个分页实体类:EUDataGridResult
package com.taotao.common.pojo;import java.util.List;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;}}编写业务层代码,增加分页处理,设置返回对象:
/** * 分页查询商品列表信息 */@Overridepublic EUDataGridResult getItemByList(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、编写前端控制层controller代码:
Controller中主要功能是接收页面传过来的参数,并且返回json类型的数据结果:
/** * 分页查询商品信息列表 * @param page * @param rows * @return */@RequestMapping("/item/list")@ResponseBodypublic 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">商品标题</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'">库存数量</th> <th data-options="field:'barcode',width:100">条形码</th> <th data-options="field:'status',width:60,align:'center',formatter:TAOTAO.formatItemStatus">状态</th> <th data-options="field:'created',width:130,align:'center',formatter:TAOTAO.formatDateTime">创建日期</th> <th data-options="field:'updated',width:130,align:'center',formatter:TAOTAO.formatDateTime">更新日期</th> </tr> </thead></table>
6、最后的实现结果
0 2
- pageHelper实现分页
- 使用PageHelper实现分页
- Mybatis+pageHelper 实现mybatis分页
- mybatis实现分页插件PageHelper
- bootstrap与pagehelper实现分页
- spring+Mybatis+ PageHelper实现分页
- Struts+Mybatis PageHelper实现分页
- Mybatis分页插件PageHelper实现自动分页
- PageHelper分页
- pagehelper分页
- mybatis使用PageHelper实现分页的技术
- pageHelper分页插件实现原理及使用方法
- mybatis使用PageHelper实现分页的技术
- mybatis(pagehelper) dataTables实现分页功能
- PageHelper+Bootstrap Paginator实现分页效果
- mybatis实现分页查询(pagehelper插件)
- Mybatis的pageHelper插件实现分页
- Spring Boot整合PageHelper 实现数据库分页
- LeetCode 78 Subsets
- 简单爬虫
- jquery上传图片本地预览插件V1.2
- Fragment里使用 PhoneGap 的 CordovaWebView
- HighCharts绘图 + Python解析文本
- pageHelper实现分页
- SDNUOJ1011(斯特灵数)
- Middle-题目105:93. Restore IP Addresses
- Linux下的进度条小程序
- 压缩跟踪Compressive Tracking
- solr服务器搭建
- 谈谈头文件
- swift字符串转double,double转字符串
- Hibernate简介