CREATE OR REPLACE PROCEDURE PROC_PAGER( tableName IN VARCHAR2, pageIndex IN INTEGER :=1, pageSize IN INTEGER :=10, condition IN VARCHAR2:=' WHERE 1=1 ', orderlist IN VARCHAR2:='', recountCount OUT INTEGER, pageCount OUT INTEGER, lresult OUT sys_refcursor ) AS lcount INTEGER; sql_recordCount VARCHAR2(2000):='SELECT COUNT(*) FROM {TABLE_NAME} {CONDITION}'; sql_pager VARCHAR2(2000):=' SELECT * FROM ( SELECT ROWNUM RN,model.* FROM ( SELECT * FROM {TABLE_NAME} {CONDITION} {ORDER_LIST} ) model WHERE ROWNUM<={TOP_COUNT} ) WHERE RN>{PASS_COUNT}'; BEGIN --总条数 sql_recordCount:=REPLACE(sql_recordCount,'{TABLE_NAME}',tableName); DBMS_OUTPUT.put_line(condition); IF (TRIM(condition) IS NOT NULL) THEN sql_recordCount:=REPLACE(sql_recordCount,'{CONDITION}',' WHERE ' || LTRIM(LTRIM(UPPER(condition)),'WHERE')); ELSE sql_recordCount:=REPLACE(sql_recordCount,'{CONDITION}',''); END IF; dbms_output.put_line(sql_recordCount); EXECUTE IMMEDIATE sql_recordCount INTO lcount; recountCount:=lcount; IF (recountCount mod pageSize) = 0 THEN pageCount:=recountCount / pageSize; ELSE pageCount:=FLOOR(recountCount / pageSize) + 1; END IF; --分页 sql_pager:=REPLACE(sql_pager,'{TABLE_NAME}',tableName); IF (TRIM(condition) IS NOT NULL) THEN sql_pager:=REPLACE(sql_pager,'{CONDITION}',' WHERE ' || LTRIM(LTRIM(UPPER(condition)),'WHERE')); ELSE sql_pager:=REPLACE(sql_pager,'{CONDITION}',''); END IF; IF (orderlist IS NOT NULL) THEN sql_pager:=REPLACE(sql_pager,'{ORDER_LIST}',' ORDER BY '|| orderlist); ELSE sql_pager:=REPLACE(sql_pager,'{ORDER_LIST}',''); END IF; sql_pager:=REPLACE(sql_pager,'{TOP_COUNT}',pageIndex*pageSize); sql_pager:=REPLACE(sql_pager,'{PASS_COUNT}',(pageIndex-1)*pageSize); DBMS_OUTPUT.put_line(sql_pager); OPEN lresult FOR sql_pager; END PROC_PAGER;
测试:
DECLARE recordCount number; pageCount number; lresult sys_refcursor; BEGIN PROC_PAGER('PET_TYPE',condition => '1=1',orderlist => 'NAME DESC',recountCount => recordCount,pageCount => pageCount,lresult => lresult); dbms_output.put_line('总条数' || recordCount || ' 总页数' || pageCount); END;
Java调用:
/** * @param args * @throws ClassNotFoundException * @throws SQLException */ public static void main(String[] args) throws ClassNotFoundException, SQLException { // TODO Auto-generated method stub Class.forName("oracle.jdbc.driver.OracleDriver"); Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","EPET","EPET"); CallableStatement cst = c.prepareCall("{call PROC_PAGER(?,?,?,?,?,?,?,?)}"); /* * CREATE OR REPLACE PROCEDURE PROC_PAGER( tableName IN VARCHAR2, pageIndex IN INTEGER :=1, pageSize IN INTEGER :=10, condition IN VARCHAR2:=' WHERE 1=1 ', orderlist IN VARCHAR2:='', recountCount OUT INTEGER, pageCount OUT INTEGER, lresult OUT sys_refcursor ) * */ cst.setString(1,"PET_TYPE"); //输入参数 cst.setInt(2, 1); cst.setInt(3, 5); cst.setString(4, " 1=1 "); cst.setString(5, ""); cst.registerOutParameter(6, Types.INTEGER); cst.registerOutParameter(7, Types.INTEGER); cst.registerOutParameter(8,OracleTypes.CURSOR); //输出参数为结果集参数 cst.executeQuery(); ResultSet rs = ((OracleCallableStatement)cst).getCursor(8); //得到输出结果集参数 System.out.printf("共%d条%d页,当前第%d页\n\n",cst.getInt(6),cst.getInt(7),1); if (rs!=null){ while(rs.next()){ System.out.println(rs.getInt("id")+"\t"+rs.getString("name")); } } rs.close(); cst.close(); c.close(); }