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));
}
}