要實現(xiàn)Java分頁查詢接口,可以按照以下步驟進(jìn)行:
public interface PagingQueryService<T> {
List<T> queryByPage(int page, int pageSize);
}
public class PagingQueryServiceImpl<T> implements PagingQueryService<T> {
private List<T> dataSource; // 數(shù)據(jù)源,可以是數(shù)據(jù)庫、文件等
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]
}
}
以上是一種簡單的實現(xiàn)方式,根據(jù)具體需求可以進(jìn)行適當(dāng)?shù)男薷暮蛿U(kuò)展。