ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ssm框架整合后使用pagehelper实现分页功能

2019-11-22 14:56:43  阅读:264  来源: 互联网

标签:pageNum 分页 pagehelper ssm couponExample Integer import com Page


一、导入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));
    }
}

 

标签:pageNum,分页,pagehelper,ssm,couponExample,Integer,import,com,Page
来源: https://www.cnblogs.com/YeHuan/p/11911498.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有