一、导入pagehelper-5.1.10.jar和jsqlparser-3.1.jar两个jar包

二、配置pagehelper

2.1 在mybatis配置文件中配置

<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>

2.2 在spring配置文件中配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
</bean>
</array>
</property>
</bean>

三、在已经实现mybatis连接数据库进行查询的前提下,修改service实现类的代码,修改如下:

package com.yh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.yh.entity.Comment;
import com.yh.mybatis.mapper.CommentMapper;
import com.yh.service.CommentService; @Service("commentService")
public class CommentServiceImpl implements CommentService { @Autowired
private CommentMapper commentMapper; @Override
public List<Comment> findCommentByBookId(int bookId) {
// TODO Auto-generated method stub
PageHelper.startPage(1, 3);
List<Comment> comments = commentMapper.findCommentByBookId(bookId);
// Page<Comment> page = (Page<Comment>) comments;
return comments;
} }

黄色背景为添加代码,这是最简单的实现方式这是最简单的,两个参数分别是查找页数和每页长度。

四、其他方式(待补充)

package com.cjs.example.service.impl;

import com.cjs.example.dao.CouponMapper;
import com.cjs.example.model.Coupon;
import com.cjs.example.model.CouponExample;
import com.cjs.example.service.CouponService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class CouponServiceImpl implements CouponService { @Autowired
private CouponMapper couponMapper; /**
* 静态方法startPage
*/
@Override
public List<Coupon> getCouponListByPage(CouponExample couponExample, Integer pageNum, Integer pageSize) {
// 在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。
// 只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的
PageHelper.startPage(pageNum, pageSize);
return couponMapper.selectByExample(couponExample);
} /**
* 分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>
* 因为 public class Page<E> extends ArrayList<E> implements Closeable
*/
@Override
public Page<Coupon> getCouponListByPage1(CouponExample couponExample, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Coupon> list = couponMapper.selectByExample(couponExample);
if (null != list) {
Page<Coupon> page = (Page<Coupon>) list;
System.out.println(page);
return page;
}
return null;
} /**
* 用PageRowBounds
*/
@Override
public List<Coupon> getCouponListByPage2(CouponExample couponExample, Integer pageNum, Integer pageSize) {
PageRowBounds pageRowBounds = new PageRowBounds(pageNum, pageSize);
List<Coupon> couponList = couponMapper.selectByExample(couponExample, pageRowBounds); System.out.println(pageRowBounds.getTotal()); Page<Coupon> page = (Page<Coupon>) couponList;
System.out.println(page); return couponList;
} @Override
public Page<Coupon> getCouponListByPage3(CouponExample couponExample, Integer pageNum, Integer pageSize) {
Page<Coupon> page = PageHelper.startPage(pageNum, pageSize).doSelectPage(()->couponMapper.selectByExample(couponExample));
System.out.println(page);
return page;
} /**
* 方法参数
*/
@Override
public PageInfo<Coupon> getCouponListByPage4(CouponExample couponExample, Integer pageNum, Integer pageSize) {
PageInfo<Coupon> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(()->couponMapper.selectByExample(couponExample));
System.out.println(pageInfo);
return pageInfo;
} /**
* PageInfo
*/
@Override
public PageInfo<Coupon> getCouponListByPage5(CouponExample couponExample, Integer pageNum, Integer pageSize) {
List<Coupon> list = couponMapper.selectByExample(couponExample);
if (null == list) {
return null;
}
PageInfo<Coupon> pageInfo = new PageInfo<>(list);
System.out.println(pageInfo);
return pageInfo;
} @Override
public Page<Coupon> getCouponListByPage6(CouponExample couponExample, Integer offset, Integer limit) {
return (Page<Coupon>) couponMapper.selectByExample(couponExample, new PageRowBounds(offset, limit));
}
}

SSM框架整合后使用pagehelper实现分页功能的更多相关文章

  1. SSM框架整合过程总结

    -----------------------siwuxie095                                 SSM 框架整合过程总结         1.导入相关 jar 包( ...

  2. 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  3. JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合

    1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...

  4. 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化

    一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...

  5. SSM框架整合模板

    SSM框架整合--MAVEN依赖 spring方面(包含了springmvc): spring-webmvc:spring与mvc的整合依赖,主要包括spring的核心包和springmvc需要的包 ...

  6. springmvc(二) ssm框架整合的各种配置

    ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...

  7. SSM框架整合搭建教程

    自己配置了一个SSM框架,打算做个小网站,这里把SSM的配置流程详细的写了出来,方便很少接触这个框架的朋友使用,文中各个资源均免费提供! 一. 创建web项目(eclipse) File-->n ...

  8. 【计理01组08号】SSM框架整合

    [计理01组08号]SSM框架整合 数据库准备 本次课程使用 MySQL 数据库.首先启动 mysql : sudo service mysql start 然后在终端下输入以下命令,进入到 MySQ ...

  9. SpringMVC详解及SSM框架整合项目

    SpringMVC ssm : mybatis + Spring + SpringMVC MVC三层架构 JavaSE:认真学习,老师带,入门快 JavaWeb:认真学习,老师带,入门快 SSM框架: ...

随机推荐

  1. 基于霸道秉火的STM32F103ZET6嵌入式开发之------基于定时TIM3的PWM实验

    1:PWM脉冲宽度调制 STM32 的定时器除了 TIM6 和 7.其他的定时器都可以用来产生 PWM 输出.其中高级定时器 TIM1 和 TIM8 可以同时产生多达 7 路的 PWM 输出.而通用定 ...

  2. 删除html标签,取其中的文本

    public String removeHtmlTags() { String str = "<p><b> welcome to test</b>< ...

  3. SQL 添加列,删除列,修改列的类型

    alter table 表名 add 列名 数据类型 如:alter table student add nickname char(20) alter table tableName(表名) add ...

  4. Java设计模式之(二)——工厂模式

    1.什么是工厂模式 Define an interface for creating an object,but let subclasses decide which class toinstant ...

  5. [cf1479E]School Clubs

    对于当前班级状态$S$,定义一个函数$\varphi(S)$,要求其满足: 令结束状态为$S_{end}$,对于任意$S\ne S_{end}$,若其下一个状态为$S'$,则$E(\varphi(S) ...

  6. lilypond 进阶—— 用scheme画图

    lilypond的许多底层设定是通过scheme语言写的,特别是要写函数的时候. 所以了解一下scheme的作用很重要. 不幸的是,不像lilypond本身的代码,scheme代码的结果是不会直接预览 ...

  7. page_fault_in_nonpaged_area异常解决方案(已解决)

    电脑在运行虚拟机的时候异常重启,多次打开结果一样,问了客服告诉我导致蓝屏的原因很多,可能是驱动不兼容,系统,或其他.{没点有用的} 百度到这个方案: 打开->此电脑->右键属性->高 ...

  8. BZOJ 3729 - Gty的游戏(Staircase 博弈+时间轴分块)

    题面传送门 介于自己以前既没有写过 Staircase-Nim 的题解,也没写过时间轴分块的题解,所以现在就来写一篇吧(fog 首先考虑最极端的情况,如果图是一条链,并且链的一个端点是 \(1\),那 ...

  9. Codeforces 582D - Number of Binominal Coefficients(Kummer 定理+数位 dp)

    Codeforces 题目传送门 & 洛谷题目传送门 一道数论与数位 dp 结合的神题 %%% 首先在做这道题之前你需要知道一个定理:对于质数 \(p\) 及 \(n,k\),最大的满足 \( ...

  10. R语言与医学统计图形【5】饼图、条件图

    R语言基础绘图系统 基础图形--饼图.克利夫兰点图.条件图 6.饼图 pie(rep(1,26),col=rainbow(26), labels = LETTERS[1:26], #标签 radius ...