java实现数据库分页

/***

 * 工具类

 * @param pageIndex //页码

 * @param pageSize//每页数据的条数

 * @param rowCount//总的数据条数

 * @return

 */

    private List<Map<String, Object>> Pagination(int pageIndex, int pageSize, long rowCount){

        int pageCount=0;//总页码

        

        if (rowCount%pageSize>0)

          {

           pageCount = (int)(rowCount/pageSize + 1);

          } else

          {

           pageCount = (int)(rowCount/pageSize);

          }

        Map<String, Object> map =new HashMap<String, Object>();

        map.put("totalPage",pageCount);//总页数

        map.put("curPage", pageIndex);//当前页码

        map.put("pageSize", pageSize);//每页的数据条数

        map.put("countTotal", rowCount);//总的数据条数

        List<Map<String, Object>> list =new ArrayList<Map<String,Object>>();

        list.add(map);

        return list;

        

    }





/***
分页查询例子
***/
    public List hysgl_cxhylx(int pageSize, int pageNum) throws SQLException {

        String sql = "select top "+pageSize+"* from HYS_LX where ID not in(select top "+(pageNum-1)*pageSize+" ID from HYS_LX order by ID) order by ID"; 

        System.out.println(sql);

            List list1 = TestDao.readall(sql);

            List<Map<String, Object>> list2 =TestDao.readall("select count(*) as count from HYS_LX");

            int counttotal=Integer.parseInt((String)list2.get(0).get("count"));

            List<List<Map<String, Object>>> list =new ArrayList<List<Map<String,Object>>>();

            list.add(list1);

            list.add(Pagination(pageNum, pageSize, counttotal));

                return list;

    }

你可能感兴趣的:(java实现)