关于在SSM框架下使用PageHelper
首先,如果各位在这块配置和代码有什么问题欢迎说出来,我也会尽自己最大的能力帮大家解答
这些代码我都是写在一个小项目里的,项目的github地址为:https://github.com/Albert-Best-Dong/parking,还希望哪个大佬给我这个小课题指点指点
jdk1.8,spring 4.3.18,mybatis 3.4.6,其实直接直接看pom文件就好了。
很长一段时间里,我学习编程很少总结代码。后来代码总结也只是写在一个电脑里的文件夹,觉得与互联网脱轨了,哈哈哈,所以现在也准备写一写博客,记录自己,提高水平。
这是我的第一篇,也是关于SSM框架下使用PageHelper。
这里不具体写我做的项目课题的全部内容,主要专注于PageHelper部分
工程结构如下图:

首先在pom.xml(parking_dao模块下)引入PageHelper依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parking</artifactId>
<groupId>com.dong</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>parking_dao</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies> </project>
最主要的是:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
随后需要在spring配置(spring-dao)文件里配置PageHelper
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.dong.parking.dao"/> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/car_park?useUnicode=true&characterEncoding=utf-8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.dong.parking.entity"/>
<property name="dataSource" ref="dataSource"/>
<!--配置PageHelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
offsetAsPageNum=true
rowBoundsWithCount=true
pageSizeZero=true
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<property name="basePackage" value="com.dong.parking.dao"/>
</bean>
</beans>
最主要的为:
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.dong.parking.entity"/>
<property name="dataSource" ref="dataSource"/>
<!--配置PageHelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
offsetAsPageNum=true
rowBoundsWithCount=true
pageSizeZero=true
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
Ok,关于配置已经好了,现在进入parking,结构如下,以红框标识的为例

ParkSpaceController代码如下
package com.dong.parking.controller; import com.dong.parking.biz.ParkSpaceBiz;
import com.dong.parking.entity.ParkSpace;
import com.dong.parking.entity.User;
import com.dong.parking.global.Constant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map; @Controller("parkSpaceController")
@RequestMapping("/parkSpace")
public class ParkSpaceController {
@Autowired
private ParkSpaceBiz parkSpaceBiz; @RequestMapping(value = "/list")
public String list(Map<String, Object> map, @RequestParam(value = "status", required = false) Integer status,
@RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
List<ParkSpace> list = null;
Integer pageSize = 2;
PageHelper.startPage(pageNo, pageSize);
if (status == null)
list = parkSpaceBiz.findAll();
else
list = parkSpaceBiz.findByStatus(status); PageInfo<ParkSpace> pageInfo = new PageInfo<ParkSpace>(list);
map.put("pageInfo", pageInfo);
return "park_space_list";
} @RequestMapping("/to_update")
public String toUpdate(Map<String, Object> map, @RequestParam int id) {
map.put("floor", Constant.getFloor());
map.put("area", Constant.getArea());
map.put("parkSpace", parkSpaceBiz.findById(id));
return "park_space_update";
} @RequestMapping("/update")
public String update(ParkSpace parkSpace, HttpSession session) {
User user = (User) session.getAttribute("user");
parkSpace.setUpdateBy(user.getSn());
parkSpaceBiz.edit(parkSpace);
return "redirect:list";
} @RequestMapping("/to_add")
public String toAdd(Map<String, Object> map) {
map.put("floor", Constant.getFloor());
map.put("area", Constant.getArea());
map.put("parkSpace", new ParkSpace());
return "park_space_add";
} @RequestMapping("/add")
public String add(ParkSpace parkSpace, HttpSession session) {
User user = (User) session.getAttribute("user");
parkSpace.setCreateBy(user.getSn());
parkSpace.setUpdateBy(user.getSn());
parkSpaceBiz.add(parkSpace);
return "redirect:list";
} @RequestMapping(value = "/remove", params = "id")
public String remove(int id) {
parkSpaceBiz.remove(id);
return "redirect:list";
}
}
我们关注一下list方法
@RequestMapping(value = "/list")
public String list(Map<String, Object> map, @RequestParam(value = "status", required = false) Integer status,
@RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
List<ParkSpace> list = null;
Integer pageSize = 2;
PageHelper.startPage(pageNo, pageSize);
if (status == null)
list = parkSpaceBiz.findAll();
else
list = parkSpaceBiz.findByStatus(status); PageInfo<ParkSpace> pageInfo = new PageInfo<ParkSpace>(list);
map.put("pageInfo", pageInfo);
return "park_space_list";
}
这里的形参pageNo为前台传来的值。这里的步骤可归结如下
步骤1:PageHelper.startPage(pageNo,pageSize),设置每页显示的个数和页数
步骤2:List<Bean> list = beanService.find(),获得bean的结合(这里以Bean代表一般的实体类)
步骤3:PageInfo<Bean> pageInfo = new PageInfo<Bean>(list); 将list集合封装到pageInfo对象。
步骤4:map.put("pageInfo",pageInfo);将pageInfo对象回给前台
好了,现在咱们来到前台页面,park_space_list.jsp代码如下
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <jsp:include page="top.jsp"/> <section id="content" class="table-layout animated fadeIn">
<div class="tray tray-center">
<div class="content-header">
<h2> 员工列表 </h2>
<p class="lead"></p>
</div>
<div class="admin-form theme-primary mw1000 center-block" style="padding-bottom: 175px;">
<div class="panel heading-border">
<div class="panel-menu">
<div class="row">
<div class="hidden-xs hidden-sm col-md-3">
<div class="btn-group">
<button type="button" class="btn btn-default light">
<i class="fa fa-refresh"></i>
</button>
<button type="button" class="btn btn-default light">
<i class="fa fa-trash"></i>
</button>
<button type="button" class="btn btn-default light">
<i class="fa fa-plus"
onclick="javascript:window.location.href='/parkSpace/to_add';"></i>
</button>
</div>
</div>
<div class="col-xs-12 col-md-9 text-right">
<div class="btn-group">
<button type="button" class="btn btn-default light">
<i class="fa fa-chevron-left"></i>
</button> <button type="button" class="btn btn-default light">
<i class="fa fa-chevron-right"></i>
</button>
</div>
</div>
</div>
</div>
<div class="panel-body pn">
<table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
<thead>
<tr class="">
<th class="text-center hidden-xs">Select</th>
<th class="hidden-xs">楼层</th>
<th class="hidden-xs">区域</th>
<th class="hidden-xs">车位号</th>
<th class="hidden-xs">状态</th> <th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pageInfo.list}" var="space">
<tr class="message-unread">
<td class="hidden-xs">
<label class="option block mn">
<input type="checkbox" name="mobileos" value="FR">
<span class="checkbox mn"></span>
</label>
</td>
<td>${space.floor}</td>
<td>${space.area}</td>
<td>${space.spaceId}</td>
<td>
<c:if test="${space.status == 0}">空闲</c:if>
<c:if test="${space.status == 1}">占用</c:if>
<c:if test="${space.status == 2}">预定</c:if>
</td> <td>
<a href="/parkSpace/to_update?id=${space.id}">编辑</a>
<a href="/parkSpace/remove?id=${space.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<p>当前 ${pageInfo.pageNum }页,总${pageInfo.pages }
页,总 ${pageInfo.total } 条记录
</p>
<a href="list?pageNo=${pageInfo.getFirstPage()}">首页</a>
<c:if test="${pageInfo.hasPreviousPage }">
<a href="list?pageNo=${pageInfo.pageNum-1}">上一页</a>
</c:if> <c:if test="${pageInfo.hasNextPage }">
<a href="list?pageNo=${pageInfo.pageNum+1}">下一页</a>
</c:if> <a href="list?pageNo=${pageInfo.getLastPage()}">末页</a>
</div>
</div>
</div>
</div>
</section> <jsp:include page="bottom.jsp"/>
我们关注于
<div class="panel-body pn">
<table id="message-table" class="table admin-form theme-warning tc-checkbox-1">
<thead>
<tr class="">
<th class="text-center hidden-xs">Select</th>
<th class="hidden-xs">楼层</th>
<th class="hidden-xs">区域</th>
<th class="hidden-xs">车位号</th>
<th class="hidden-xs">状态</th> <th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pageInfo.list}" var="space">
<tr class="message-unread">
<td class="hidden-xs">
<label class="option block mn">
<input type="checkbox" name="mobileos" value="FR">
<span class="checkbox mn"></span>
</label>
</td>
<td>${space.floor}</td>
<td>${space.area}</td>
<td>${space.spaceId}</td>
<td>
<c:if test="${space.status == 0}">空闲</c:if>
<c:if test="${space.status == 1}">占用</c:if>
<c:if test="${space.status == 2}">预定</c:if>
</td> <td>
<a href="/parkSpace/to_update?id=${space.id}">编辑</a>
<a href="/parkSpace/remove?id=${space.id}">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<p>当前 ${pageInfo.pageNum }页,总${pageInfo.pages }
页,总 ${pageInfo.total } 条记录
</p>
<a href="list?pageNo=${pageInfo.getFirstPage()}">首页</a>
<c:if test="${pageInfo.hasPreviousPage }">
<a href="list?pageNo=${pageInfo.pageNum-1}">上一页</a>
</c:if> <c:if test="${pageInfo.hasNextPage }">
<a href="list?pageNo=${pageInfo.pageNum+1}">下一页</a>
</c:if> <a href="list?pageNo=${pageInfo.getLastPage()}">末页</a>
</div>
最终的效果如图所示:

水平有限,表述还是很不好
关于在SSM框架下使用PageHelper的更多相关文章
- ssm框架下怎么批量删除数据?
ssm框架下批量删除怎么删除? 1.单击删除按钮选中选项后,跳转到js函数,由函数处理 2. 主要就是前端的操作 js 操作(如何全选?如何把选中的数据传到Controller中) 3.fun()函数 ...
- Jquery DataTable AJAX跨域请求的解决方法及SSM框架下服务器端返回JSON格式数据的解决方法
如题,用HBuilder开发APP,涉及到用AJAX跨域请求后台数据,刚接触,费了不少时间.幸得高手指点,得以解决. APP需要用TABLE来显示数据,因此采用了JQ 的DataTable. 在实现 ...
- SSM框架下分页的实现(封装page.java和List<?>)
之前写过一篇博客 java分页的实现(后台工具类和前台jsp页面),介绍了分页的原理. 今天整合了Spring和SpringMVC和MyBatis,做了增删改查和分页,之前的逻辑都写在了Servle ...
- ssm框架下实现文件上传
1.由于ssm框架是使用Maven进行管理的,文件上传所需要的jar包利用pom.xml进行添加,如下所示: <properties> <commons-fileupload.v ...
- SSM框架下 Failed to load resource: the server responded with a status of 404 (Not Found)错误
这个错误提示的是js的引用路径有错: 1.检查应用路径是否正确(我的问题是路径是正确的但是去到页面就会提示404错误) 引用路径,最好都使用绝对路径 <script type="tex ...
- SSM框架下的redis缓存
基本SSM框架搭建:http://www.cnblogs.com/fuchuanzhipan1209/p/6274358.html 配置文件部分: 第一步:加入jar包 pom.xml <!-- ...
- SSM框架下结合 log4j、slf4j打印日志
首先加入log4j和slf4j的jar包 <!-- 日志处理 <!-- slf4j日志包--> <dependency> <groupId>org.slf4j ...
- ssm框架下的文件上传和文件下载
最近在做一个ssm的项目,遇到了添加附件和下载的功能,在网上查了很多资料,发现很多都不好用,经过摸索,发现了一套简便的方法,和大家分享一下. 1.在自己已经构建好的maven web项目中 pom. ...
- PageHelper--Mybatis分页插件(ssm框架下的使用)
1.导入PageHelper依赖 <!-- MyBatis 分页插件 --> <dependency> <groupId>com.github.pagehelper ...
随机推荐
- [深入学习C#]C#实现多线程的方法:线程(Thread类)和线程池(ThreadPool)
简介 使用线程的主要原因:应用程序中一些操作需要消耗一定的时间,比如对文件.数据库.网络的访问等等,而我们不希望用户一直等待到操作结束,而是在此同时可以进行一些其他的操作. 这就可以使用线程来实现. ...
- hbase_学习_01_HBase环境搭建(单机)
一.前言 本文承接上一篇:hadoop_学习_02_Hadoop环境搭建(单机) ,主要是搭建HBase的单机环境 二.环境准备 1.说明 hbase 的下载来源有: 官方版本:http://arc ...
- QListWidget列表控件:当鼠标选中某行时,系统会自动设置选中的行的行号,用currentRow()返回回来,没有setCheck或setSelect类似函数
列表控件的设计思路: 只有QListWidgetItem自己能改变自己的状态(如checked,selected,颜色等)状态,QListWidget是无法改变其项的状态的. 列表控件是被动接受子项的 ...
- JDBC获得数据库连接及使用
1.Connection Java.sql.Driver 接口是所有 JDBC 驱动程序需要实现的接口.这个接口是提供给数据库厂商使用的,不同数据库厂商提供不同的实现 在程序中不需要直接去访问实现了 ...
- 机器学习 Support Vector Machines 3
Optimal margin classifiers 前面我们讲过,对如下的原始的优化问题我们希望找到一个优化的边界分类器. minγ,w,bs.t.12∥w∥2y(i)(wTx(i)+b)⩾1,i= ...
- 3.1 第一个场景 HelloWorldScene
HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...
- Muduo 多线程模型:一个 Sudoku 服务器演变
陈硕 (giantchen AT gmail) blog.csdn.net/Solstice Muduo 全系列文章列表: http://blog.csdn.net/Solstice/category ...
- 用遗传算法解决TSP问题
浅谈遗传算法:https://www.cnblogs.com/AKMer/p/9479890.html Description \(小m\)在踏上寻找\(小o\)的路程之后不小心碰到了大魔王\(fat ...
- mina中的发送延时
由于项目需要,用到了 mina 框架进行 tcp 通讯.我是初次接触 mina,于是从 Hello world 开始学习了 mina .期间遇到了一个奇怪的发送数据的延迟问题,解决的过程是曲折的,但找 ...
- 编译内核是出现:arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments
内核:Linux-3.4.2 编译内核出现arch/arm/mm/tlb-v4wbi.S:64:error: too many positional arguments 交叉工具链太老了,换新一点的. ...