ssm框架中mysql的分页_SSM框架整合后使用pagehelper实现分页功能

该博客介绍了如何在Java项目中利用PageHelper库进行MyBatis的分页查询。通过@Autowired注解注入CouponMapper,并展示了多种分页查询的方法,包括直接使用PageHelper.startPage(),转换PageRowBounds,以及使用PageInfo和Page对象。这些方法能够帮助开发者有效地获取和展示分页数据。
摘要由CSDN通过智能技术生成

packagecom.cjs.example.service.impl;importcom.cjs.example.dao.CouponMapper;importcom.cjs.example.model.Coupon;importcom.cjs.example.model.CouponExample;importcom.cjs.example.service.CouponService;importcom.github.pagehelper.Page;importcom.github.pagehelper.PageHelper;importcom.github.pagehelper.PageInfo;importcom.github.pagehelper.PageRowBounds;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@Servicepublic class CouponServiceImpl implementsCouponService {

@AutowiredprivateCouponMapper couponMapper;/*** 静态方法startPage*/@Overridepublic ListgetCouponListByPage(CouponExample couponExample, Integer pageNum, Integer pageSize) {//在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。//只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的

PageHelper.startPage(pageNum, pageSize);returncouponMapper.selectByExample(couponExample);

}/*** 分页时,实际返回的结果list类型是Page,如果想取出分页信息,需要强制转换为Page

* 因为 public class Page extends ArrayList implements Closeable*/@Overridepublic PagegetCouponListByPage1(CouponExample couponExample, Integer pageNum, Integer pageSize) {

PageHelper.startPage(pageNum, pageSize);

List list =couponMapper.selectByExample(couponExample);if (null !=list) {

Page page = (Page) list;

System.out.println(page);returnpage;

}return null;

}/*** 用PageRowBounds*/@Overridepublic ListgetCouponListByPage2(CouponExample couponExample, Integer pageNum, Integer pageSize) {

PageRowBounds pageRowBounds= newPageRowBounds(pageNum, pageSize);

List couponList =couponMapper.selectByExample(couponExample, pageRowBounds);

System.out.println(pageRowBounds.getTotal());

Page page = (Page) couponList;

System.out.println(page);returncouponList;

}

@Overridepublic PagegetCouponListByPage3(CouponExample couponExample, Integer pageNum, Integer pageSize) {

Page page = PageHelper.startPage(pageNum, pageSize).doSelectPage(()->couponMapper.selectByExample(couponExample));

System.out.println(page);returnpage;

}/*** 方法参数*/@Overridepublic PageInfogetCouponListByPage4(CouponExample couponExample, Integer pageNum, Integer pageSize) {

PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(()->couponMapper.selectByExample(couponExample));

System.out.println(pageInfo);returnpageInfo;

}/*** PageInfo*/@Overridepublic PageInfogetCouponListByPage5(CouponExample couponExample, Integer pageNum, Integer pageSize) {

List list =couponMapper.selectByExample(couponExample);if (null ==list) {return null;

}

PageInfo pageInfo = new PageInfo<>(list);

System.out.println(pageInfo);returnpageInfo;

}

@Overridepublic PagegetCouponListByPage6(CouponExample couponExample, Integer offset, Integer limit) {return (Page) couponMapper.selectByExample(couponExample, newPageRowBounds(offset, limit));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值