PageHelper分页插件
为什么分页插件不生效:
1.是否PageHelper.startPage(pageNum, pageSize);放置的位置正确(确保放置在要分页的查询条件前面)
2.是否对结果进行变更使已经分页好的内容的Page参数丢失
官方示例代码:
-
//第一种,RowBounds方式的调用
-
List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(0, 10));
-
-
//第二种,Mapper接口方式的调用,推荐这种使用方式。
-
PageHelper.startPage(1, 10);
-
List<Country> list = countryMapper.selectIf(1);
-
-
//第三种,Mapper接口方式的调用,推荐这种使用方式。
-
PageHelper.offsetPage(1, 10);
-
List<Country> list = countryMapper.selectIf(1);
-
-
//第四种,参数方法调用
-
//存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
-
public interface CountryMapper {
-
List<Country> selectByPageNumSize(
-
-
int pageNum,
-
int pageSize);
-
}
-
//配置supportMethodsArguments=true
-
//在代码中直接调用:
-
List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10);
-
-
//第五种,参数对象
-
//如果 pageNum 和 pageSize 存在于 User 对象中,只要参数有值,也会被分页
-
//有如下 User 对象
-
public class User {
-
//其他fields
-
//下面两个参数名和 params 配置的名字一致
-
private Integer pageNum;
-
private Integer pageSize;
-
}
-
//存在以下 Mapper 接口方法,你不需要在 xml 处理后两个参数
-
public interface CountryMapper {
-
List<Country> selectByPageNumSize(User user);
-
}
-
//当 user 中的 pageNum!= null && pageSize!= null 时,会自动分页
-
List<Country> list = countryMapper.selectByPageNumSize(user);
-
-
//第六种,ISelect 接口方式
-
//jdk6,7用法,创建接口
-
Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(new ISelect() {
-
-
public void doSelect() {
-
countryMapper.selectGroupBy();
-
}
-
});
-
//jdk8 lambda用法
-
Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(()-> countryMapper.selectGroupBy());
-
-
//也可以直接返回PageInfo,注意doSelectPageInfo方法和doSelectPage
-
pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {
-
-
public void doSelect() {
-
countryMapper.selectGroupBy();
-
}
-
});
-
//对应的lambda用法
-
pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(() -> countryMapper.selectGroupBy());
-
-
//count查询,返回一个查询语句的count数
-
long total = PageHelper.count(new ISelect() {
-
-
public void doSelect() {
-
countryMapper.selectLike(country);
-
}
-
});
-
//lambda
-
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行代码会有泛型警告)
-
PageHelper.startPage(pageNum, pageSize);
-
List<User> listByPage = userMapper.findListByPage(conditionMap);
-
List<UserDTO> list = listByPage
-
.stream()
-
.map(user -> {
-
UserDTO userResponse = new UserDTO();
-
BeanUtils.copyProperties(user, userResponse);
-
return userResponse;
-
}).collect(Collectors.toList());
-
-
-
PageInfo<UserDTO> pageInfo = new PageInfo(listByPage);
-
pageInfo.setList(list);
-
return pageInfo;
2.如何将分页转换的部分提取成通用方法
-
PageHelper.startPage(pageNum, pageSize);
-
List<User> listByPage = userMapper.findListByPage(conditionMap);
-
List<UserDTO> list = listByPage
-
.stream()
-
.map(user -> {
-
UserDTO userResponse = new UserDTO();
-
BeanUtils.copyProperties(user, userResponse);
-
return userResponse;
-
}).collect(Collectors.toList());
-
return PageUtils.convertPageInfoVo(listByPage, list);
-
public class PageUtils {
-
public static <Do, Vo> PageInfo<Vo> convertPageInfoVo(List<Do> list, List<Vo> convertList) {
-
PageInfo<Do> pageInfoDo = PageInfo.of(list);
-
PageInfo<Vo> pageInfo = new PageInfo<>();
-
BeanUtils.copyProperties(pageInfoDo, pageInfo);
-
pageInfo.setList(convertList);
-
return pageInfo;
-
}
-
}
分类:
工具
标签:
PageHelper分页插件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
2021-08-30 oracle for update wait 解析
2018-08-30 Spring Cloud和Dubbo
2018-08-30 Acquiring Heap Dumps
2018-08-30 java线程阻塞问题排查方法
2018-08-30 关于HashMap初始化容量问题
2016-08-30 集群下Cookie共享,必须要设置machineKey