gpt4 book ai didi

java - 如何对非实体的对象进行分页?

转载 作者:行者123 更新时间:2023-11-30 06:25:09 25 4
gpt4 key购买 nike

我正在尝试进行分页,与@RepositoryRestResource的选项非常相似但仅限于非实体的对象。

代码:

public class FloorplanDevice {

private String FloorplanName;
private Long FloorplanId;

private Long DeviceId;
private String DeviceName;
private String macAddress;
private Long groupId;
private LocatableType deviceType;

public String getFloorplanName() {
return FloorplanName;
}

public void setFloorplanName(String floorplanName) {
FloorplanName = floorplanName;
}

public Long getFloorplanId() {
return FloorplanId;
}

public void setFloorplanId(Long floorplanId) {
FloorplanId = floorplanId;
}

public Long getDeviceId() {
return DeviceId;
}

public void setDeviceId(Long deviceId) {
DeviceId = deviceId;
}

public String getDeviceName() {
return DeviceName;
}

public void setDeviceName(String deviceName) {
DeviceName = deviceName;
}

public String getMacAddress() {
return macAddress;
}

public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}

public Long getGroupId() {
return groupId;
}

public void setGroupId(Long groupId) {
this.groupId = groupId;
}

public LocatableType getDeviceType() {
return deviceType;
}

public void setDeviceType(LocatableType deviceType) {
this.deviceType = deviceType;
}

public FloorplanDevice() {
}

public FloorplanDevice(String floorplanName, Long floorplanId, Long deviceId, String deviceName, String macAddress, Long groupId, LocatableType deviceType) {
FloorplanName = floorplanName;
FloorplanId = floorplanId;
DeviceId = deviceId;
DeviceName = deviceName;
this.macAddress = macAddress;
this.groupId = groupId;
this.deviceType = deviceType;
}
}

此对象没有存储库,但它有 Controller :

   @RequestMapping(
path = arrayOf("/floorplanDevice/{groupId}", "/floorplanDevice/{groupId}/"),
method = arrayOf(RequestMethod.GET))
open fun getFloorplanDevice(@PathVariable("groupId") groupId: Long): ResponseEntity<*>{

var floorplanDevice= floorplanService.getFloorplanDevice(groupId)
return ResponseEntity(floorplanDevice, HttpStatus.OK)

}

那么如何使用页码和大小对该对象进行分页(如果也可以排序)?

我正在使用java Spring

谢谢

最佳答案

尝试这样的事情:

public Page<FloorplanDevice> getFloorplanDevice(@PathVariable("groupId") Long groupId, 
@PageableDefault Pageable pageable)
List<FloorplanDevice> list = floorplanService.getFloorplanDevice(groupId);

MutableSortDefinition sort = pageable.getSort() != null ?
StreamSupport.stream(pageable.getSort().spliterator(), false)
.findFirst()
.map(it -> new MutableSortDefinition(it.getProperty(), it.isIgnoreCase(), it.isAscending()))
.orElse(null)
: null;

PagedListHolder<FloorplanDevice> pageHolder = new PagedListHolder<>(list, sort);
pageHolder.setPage(pageable.getPageNumber());
pageHolder.setPageSize(pageable.getPageSize());
pageHolder.resort();

List<FloorplanDevice> content = pageHolder.getPageList();
Page<FloorplanDevice> page = new PageImpl<>(content, pageable, list.size());

return page;
}

关于java - 如何对非实体的对象进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47327540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com