SqlServer,分页查询(最简单,新手必备!)

SqlServer,分页查询(最简单,新手必备!) sqlserver数据库在处理分页数据时,不如mysql可以用limit来限制每次查找的行数,智能用top来进行查找,这对于新手程序员来说,可能不是很容易来写出来。本文分享一个最简单,最方便的查询方法(大神勿喷!),废话不多说,直接进入正题!

 

1 对应表的实体类(方便测试,只设置了一个字段)

 1 package testDemo;
 2 /**
 3  * 实体类
 4  * 
 5  * @author Dell
 6  * 
 7  */
 8 public class Student {
 9     private Integer id;
10 
11     public Integer getId() {
12         return id;
13     }
14 
15     public void setId(Integer id) {
16         this.id = id;
17     }
18 }

 

2:分页的工具类(查询时只需查询出表中所有数据存入List集合中,不确定集合类型,工具类定义为Objec,转换方法在文章末,getInfoList()方法返回给界面需要展示的数据)

import java.util.ArrayList;
import java.util.List;

public class PageUtil {

    private Integer currentPage;//当前页数
    private Integer pageSize;//页面大小
    private Integer totalCount;//总数据量
    private Integer totalPageCount;//总的页数
    private List<Object> infoList;//接受的需要分页的数据

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    /**
     * 计算得到总页数
     *
     * @param totalCount
     */
    public void setTotalCount(Integer totalCount) {
        if (totalCount > 0) {
            this.totalCount = totalCount;
            totalPageCount = this.totalCount % pageSize == 0 ? (this.totalCount / pageSize)
                    : (this.totalCount / pageSize + 1);
        }
    }

    public Integer getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(Integer totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public List<Object> getInfoList() {
        //临时数组,存储已经分页的数据集合
        List<Object> tempList = new ArrayList<Object>();
        //需要展示的第一条数据的索引
        int startIndex = pageSize * (currentPage - 1);
        //结束的数据索引
        int endIndex = (startIndex) + pageSize;

        for (int i = startIndex; i < endIndex; i++) {
            if (i < infoList.size()) {
                tempList.add(infoList.get(i));
            }
        }
        return tempList;
    }

    public void setInfoList(List<Object> infoList) {
        this.infoList = infoList;
    }
}

 

3:测试

 1 public static void main(String[] args) {
 2         
 3         // 模拟从数据中查询处的数据,20条
 4         List<Student> stuList = new ArrayList<Student>();
 5         for (int i = 0; i < 20; i++) {
 6             Student stu = new Student();
 7             stu.setId(i);
 8             stuList.add(stu);
 9         }
10         
11         PageUtil pageUtil = new PageUtil();
12         pageUtil.setPageSize(5);// 页面大小
13         pageUtil.setCurrentPage(2);// 当前页数
14         pageUtil.setTotalCount(stuList.size());
15         // 传给工具类所有数据
16         pageUtil.setInfoList(Arrays.asList(stuList.toArray()));
17         
18         // 临时集合,存储需要工具类返回的分页的数据集合
19         List<Student> showList = new ArrayList<Student>();
20         showList = (List<Student>) (Object) pageUtil.getInfoList();
21         // 模拟界面展示数据
22         for (int i = 0; i < showList.size(); i++) {
23             System.out.println(showList.get(i).getId());
24         }
25     }

 

tips:
1 List(Object)集合转换为实体类集合

List<Student> showList = (List<Student>) (Object) Object集合 ;

2 List<实体类>集合转换为List集合

List<Object> objectList = Arrays.asList(stuList.toArray());

 

posted @ 2020-07-25 12:06  huiblog  阅读(1092)  评论(0编辑  收藏  举报