PageHelper分页插件

为什么分页插件不生效:

1.是否PageHelper.startPage(pageNum, pageSize);放置的位置正确(确保放置在要分页的查询条件前面)

2.是否对结果进行变更使已经分页好的内容的Page参数丢失

 

官方使用说明:

官方示例代码:

  1.  
    //第一种,RowBounds方式的调用
  2.  
    List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(0, 10));
  3.  
     
  4.  
    //第二种,Mapper接口方式的调用,推荐这种使用方式。
  5.  
    PageHelper.startPage(1, 10);
  6.  
    List<Country> list = countryMapper.selectIf(1);
  7.  
     
  8.  
    //第三种,Mapper接口方式的调用,推荐这种使用方式。
  9.  
    PageHelper.offsetPage(1, 10);
  10.  
    List<Country> list = countryMapper.selectIf(1);
  11.  
     
  12.  
    //第四种,参数方法调用
  13.  
    //存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
  14.  
    public interface CountryMapper {
  15.  
    List<Country> selectByPageNumSize(
  16.  
    @Param("user") User user,
  17.  
    @Param("pageNum") int pageNum,
  18.  
    @Param("pageSize") int pageSize);
  19.  
    }
  20.  
    //配置supportMethodsArguments=true
  21.  
    //在代码中直接调用:
  22.  
    List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10);
  23.  
     
  24.  
    //第五种,参数对象
  25.  
    //如果 pageNum 和 pageSize 存在于 User 对象中,只要参数有值,也会被分页
  26.  
    //有如下 User 对象
  27.  
    public class User {
  28.  
    //其他fields
  29.  
    //下面两个参数名和 params 配置的名字一致
  30.  
    private Integer pageNum;
  31.  
    private Integer pageSize;
  32.  
    }
  33.  
    //存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
  34.  
    public interface CountryMapper {
  35.  
    List<Country> selectByPageNumSize(User user);
  36.  
    }
  37.  
    //当 user 中的 pageNum!= null && pageSize!= null 时,会自动分页
  38.  
    List<Country> list = countryMapper.selectByPageNumSize(user);
  39.  
     
  40.  
    //第六种,ISelect 接口方式
  41.  
    //jdk6,7用法,创建接口
  42.  
    Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(new ISelect() {
  43.  
    @Override
  44.  
    public void doSelect() {
  45.  
    countryMapper.selectGroupBy();
  46.  
    }
  47.  
    });
  48.  
    //jdk8 lambda用法
  49.  
    Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(()-> countryMapper.selectGroupBy());
  50.  
     
  51.  
    //也可以直接返回PageInfo,注意doSelectPageInfo方法和doSelectPage
  52.  
    pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {
  53.  
    @Override
  54.  
    public void doSelect() {
  55.  
    countryMapper.selectGroupBy();
  56.  
    }
  57.  
    });
  58.  
    //对应的lambda用法
  59.  
    pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(() -> countryMapper.selectGroupBy());
  60.  
     
  61.  
    //count查询,返回一个查询语句的count数
  62.  
    long total = PageHelper.count(new ISelect() {
  63.  
    @Override
  64.  
    public void doSelect() {
  65.  
    countryMapper.selectLike(country);
  66.  
    }
  67.  
    });
  68.  
    //lambda
  69.  
    total = PageHelper.count(()->countryMapper.selectLike(country));

 

如何开启分页(不涉及到排序):

下面代码可以放置在Controller层,也可以设置到Service

PageHelper.startPage(pageNum, pageSize);

注:

分页代码需要放置在查询语句的上面,并且只会影响查询的第一个Sql语句上

 

如何在分页的时候,对分页结果进行转换

1)数据库查询结果想要进行一定的处理转换为DTO

解决方式:

参考文档:

https://github.com/pagehelper/Mybatis-PageHelper/issues/32

https://www.cnblogs.com/hanstrovsky/p/12527022.html

https://blog.csdn.net/Ep_Little_prince/article/details/102869479

 

个人解决方式:(内容比较简单, 就不提供实体类信息了)

1.查询出来的是User对象 想要转换为UserDTO的方法

(存在的问题 第12行代码会有泛型警告)

  1.  
    PageHelper.startPage(pageNum, pageSize);
  2.  
    List<User> listByPage = userMapper.findListByPage(conditionMap);
  3.  
    List<UserDTO> list = listByPage
  4.  
    .stream()
  5.  
    .map(user -> {
  6.  
    UserDTO userResponse = new UserDTO();
  7.  
    BeanUtils.copyProperties(user, userResponse);
  8.  
    return userResponse;
  9.  
    }).collect(Collectors.toList());
  10.  
     
  11.  
     
  12.  
    PageInfo<UserDTO> pageInfo = new PageInfo(listByPage);
  13.  
    pageInfo.setList(list);
  14.  
    return pageInfo;

2.如何将分页转换的部分提取成通用方法

  1.  
    PageHelper.startPage(pageNum, pageSize);
  2.  
    List<User> listByPage = userMapper.findListByPage(conditionMap);
  3.  
    List<UserDTO> list = listByPage
  4.  
    .stream()
  5.  
    .map(user -> {
  6.  
    UserDTO userResponse = new UserDTO();
  7.  
    BeanUtils.copyProperties(user, userResponse);
  8.  
    return userResponse;
  9.  
    }).collect(Collectors.toList());
  10.  
    return PageUtils.convertPageInfoVo(listByPage, list);
  1.  
    public class PageUtils {
  2.  
    public static <Do, Vo> PageInfo<Vo> convertPageInfoVo(List<Do> list, List<Vo> convertList) {
  3.  
    PageInfo<Do> pageInfoDo = PageInfo.of(list);
  4.  
    PageInfo<Vo> pageInfo = new PageInfo<>();
  5.  
    BeanUtils.copyProperties(pageInfoDo, pageInfo);
  6.  
    pageInfo.setList(convertList);
  7.  
    return pageInfo;
  8.  
    }
  9.  
    }

 

 
posted @ 2022-08-30 19:06  甜菜波波  阅读(510)  评论(0编辑  收藏  举报