PageHelper 配合ssm + jqueryEasyui + Maven 使用
pom.xml需要的jar
<!-- 分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
<version>1.0</version>
</dependency>
创建测试类Controller
package com.taotao.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.dao.TbItemMapper;
import com.taotao.pojo.TbItem;
@Controller
public class PageTestController {
@Autowired
TbItemMapper t;
@RequestMapping("/test.do")
public void pageTest(){
//设置 从第一页开始显示,每页显示10条记录
PageHelper.startPage(1, 10);
//查询数据 返回List
List<TbItem> list = t.selectByExample(null);
for (TbItem tbItem : list) {
System.out.println(tbItem.getTitle());
}
PageInfo<TbItem> p= new PageInfo<>(list);
//获得总记录数
long total = p.getTotal();
System.out.println(total);
}
}
需要在mybatis配置文件 mybatis-config.xml配置文件 加上
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 指定数据库 还有很多参数可以配置 可以去官网查看 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
访问http://localhost:8080/tt/test.do 后台会出现 十条记录数 和总记录数
实际应用
1.创建 PageEasyui 实体类
package com.taotao.pojo; import java.util.List; public class Page { 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; } }
2.在service层 创建一个接口
//返回类型是PageEasyui类型 后面两个人参数是 Easyui页面传过来的 Page getItemList(int page,int rows);
3.serviceImpl 实现类 实现service接口
public Page getItemList(int page, int rows) { //进行分页处理 PageHelper.startPage(page, rows); List<TbItem> list = itemMapper.selectByExample(null); //创建返回对象 Page p=new Page(); p.setRows(list); PageInfo<TbItem> pageInfo=new PageInfo<>(list); p.setTotal(pageInfo.getTotal()); return p; }
4.Controller 代码
@Controller @RequestMapping("/item") public class ItemController { @Autowired ItemService itemService; @RequestMapping("/list.do") @ResponseBody public Page getItemList(@RequestParam(defaultValue="1")Integer page,@RequestParam(defaultValue="30")Integer rows){ Page itemList = itemService.getItemList(page, rows); return itemList; } }
jsp页面代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <table class="easyui-datagrid" id="itemList" title="商品列表" data-options="singleSelect:false,collapsible:true,pagination:true,url:'${pageContext.request.contextPath}/item/list.do',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>


浙公网安备 33010602011771号