要實現Java分頁查詢接口,可以按照以下步驟進行:
public interface PagingQueryService<T> {
List<T> queryByPage(int page, int pageSize);
}
public class PagingQueryServiceImpl<T> implements PagingQueryService<T> {
private List<T> dataSource; // 數據源,可以是數據庫、文件等
public PagingQueryServiceImpl(List<T> dataSource) {
this.dataSource = dataSource;
}
@Override
public List<T> queryByPage(int page, int pageSize) {
int startIndex = (page - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, dataSource.size());
return dataSource.subList(startIndex, endIndex);
}
}
public class Main {
public static void main(String[] args) {
List<String> dataSource = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
PagingQueryService<String> service = new PagingQueryServiceImpl<>(dataSource);
int page = 2;
int pageSize = 3;
List<String> result = service.queryByPage(page, pageSize);
System.out.println(result); // 輸出:[D, E, F]
}
}
以上是一種簡單的實現方式,根據具體需求可以進行適當的修改和擴展。