java中page和pageinfo两个类的区别
时间: 2023-09-14 22:03:39 浏览: 73
Page和PageInfo都是MyBatis框架中分页查询的常用类。其中,Page类是基于RowBounds实现的分页,而PageInfo类是基于物理分页,即通过SQL语句limit关键字实现的分页。Page类的使用比较灵活,可以自定义查询起始位置和返回数量;而PageInfo类则直接指定页码和每页数量,可以更方便地实现分页查询。
相关问题
Java PageInfo手动对list进行分页
在Java中对list进行分页,可以通过对list进行切片(slice)来实现。下面是一个示例代码,可以将一个list按照指定的每页数量进行分页:
```java
public class Paginator<T> {
private List<T> items;
private int pageSize;
public Paginator(List<T> items, int pageSize) {
this.items = items;
this.pageSize = pageSize;
}
public List<T> getPage(int pageNumber) {
int startIndex = (pageNumber - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, items.size());
return items.subList(startIndex, endIndex);
}
public int getTotalPages() {
return (int) Math.ceil((double) items.size() / pageSize);
}
}
```
这个类接受两个参数:一个是要分页的list,另一个是每页的数量。它提供了两个方法:getPage用于获取指定页数的分页后的list,getTotalPages用于获取总页数。
例如,假设有一个包含10个整数的list,需要每页分页3个,可以使用以下代码进行分页:
```java
List<Integer> items = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int pageSize = 3;
Paginator<Integer> paginator = new Paginator<>(items, pageSize);
for (int i = 1; i <= paginator.getTotalPages(); i++) {
List<Integer> pageItems = paginator.getPage(i);
System.out.println(pageItems);
}
```
这个代码会输出:
```
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
```
注意,这个类并没有改变原始的list,而是返回了一个新的分页后的list。如果你需要在原始list上进行分页,可以使用类似`items.subList(startIndex, endIndex).clear(); items.addAll(startIndex, newItems);`的语法来替换原始的list的对应部分。
PageInfo<DeviceInfoPageVO>和PageInfo<DeviceInfoVO>怎么转换
这两个类型是泛型类型,可以通过 Java 的类型转换进行转换。
如果 `DeviceInfoPageVO` 是 `DeviceInfoVO` 的子类,可以直接进行强制类型转换。例如:
```
PageInfo<DeviceInfoPageVO> pageInfo1 = ...
PageInfo<DeviceInfoVO> pageInfo2 = (PageInfo<DeviceInfoVO>) pageInfo1;
```
如果 `DeviceInfoPageVO` 不是 `DeviceInfoVO` 的子类,可以使用 Java 8 的 Stream API 进行转换:
```
PageInfo<DeviceInfoPageVO> pageInfo1 = ...
PageInfo<DeviceInfoVO> pageInfo2 = new PageInfo<>(pageInfo1.getList().stream()
.map(deviceInfoPageVO -> new DeviceInfoVO(deviceInfoPageVO))
.collect(Collectors.toList()));
```
以上假设 `DeviceInfoVO` 有一个接受 `DeviceInfoPageVO` 参数的构造函数。如果没有,需要手动将属性赋值给新的对象。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)