Oracle分页工具类+分页对象JavaBean

public class FenyeQueryUtil{

	/**设置分页属性
	 * 
	 * @param request
	 * @return page
	 */
	public static Page setPageAtt(HttpServletRequest request) {
		int pageNum = 1;
		int pageSize =10 ;
		String temp_pageNum = request.getParameter("pageNum");//浏览器传回pageNum
		String temp_pageSize = request.getParameter("pageSize");//浏览器传回pageSize
		//更新page分页信息
		if(temp_pageNum!=null){
			pageNum = Integer.parseInt(temp_pageNum);
		}
		if(temp_pageSize!=null){
			pageSize = Integer.parseInt(temp_pageSize);
		}
		Page page = new Page();
		page.setPageNum(pageNum);  
        page.setPageSize(pageSize);
		return page;
	}
    
	/**
	 * 
	 * @param sql	查询的sql语句
	 * @param page	分页page对象
	 * @param params	sql语句参数值
	 * @param clss		查询的类
	 * @return		list 
	 */
    public static List query(String sql, Page page, Object[] params, Class clss) {  
    	//获取qr对象
        QueryRunner qr = JdbcUtil.getQueryRunner();
       
        //返回的集合
        List list = null;  
        try {  
            //sql = sql.toUpperCase();  
            //普通查询
            if (null == page) {  
                list = qr.query(MangeThreadLocal.getConnection(),sql, new BeanListHandler(clss.newInstance().getClass()), params);  
            }  
            //分页查询
            else {  
                if (null == params) {  
                    params = new Object[]{};  
                }  
                String pageSql = "SELECT * FROM (SELECT A.*,ROWNUM RN " + "FROM (" + sql + ") A WHERE ROWNUM <=?"  
                    + ") " + "WHERE RN >=?";  
  
                // 查询总数  
                String totalSql = "SELECT COUNT(*) FROM (" + sql + ")";  
                
                Number count = (Number) qr.query(totalSql,new ScalarHandler(),params);  
                page.setTotalSize(count.intValue());  
                page.init();  
                  
                Object[] pageParams = new Object[params.length + 2];  
                System.arraycopy(params, 0, pageParams, 0, params.length);  
                pageParams[params.length] = page.getPageEnd();  
                pageParams[params.length + 1] = page.getPageBegin();  
                list = qr.query(pageSql, new BeanListHandler(clss.newInstance().getClass()), pageParams);  
                
            }  
        }  
        catch (Exception e) {  
            e.printStackTrace();  
        }  
        return list;  
    }  
  
  
}
 
  
public class Page{  
	  
    // 总条数  
    private int totalSize;  
  
    // 每页大小  
    private int pageSize;  
  
    // 总页数  
    private int totalPage;  
  
    // 第几页  
    private int pageNum = 1;  
  
    // 每页开始条数  
    private int pageBegin;  
  
    // 每页结束条数  
    private int pageEnd;  
    
    //存放记录
    private List items;
    

    public Page() {  
    }  
  
    public void init() {  
        // pageSize 默认为12  
        if (pageSize <= 0) {  
            pageSize = 12;  
        }  
  
        totalPage = totalSize / pageSize;  
  
        if (0 != totalSize % pageSize) {  
            totalPage += 1;  
        }  
  
        if (pageNum > totalPage) {  
            pageNum = totalPage;  
        }  
        if (pageNum < 1) {  
            pageNum = 1;  
        }  
        pageBegin = (pageNum - 1) * pageSize + 1;  
        pageEnd = (pageNum) * pageSize;  
    }  

你可能感兴趣的:(Java)