j2ee 里面封装通用分页。
我们现在做的项目是一个比较大型的项目
要找一种通用的分页类的设计方法?
SSH2开发
在action 里面可以设置一给参数(比例当前页数,排序字段)
但是在数据层里面的配制要少一些?
[解决办法]
通用分页对象
package www.gbsou.com.pagination;import java.io.Serializable;/*** 分页对象* @author www.gbsou.com*/public class Page implements Serializable{ private Boolean hasPrePage; private Boolean hasNextPage; /** * total count in a page.When less than 1(zero or minus), it will not * paging. */ private int pageSize; private int totalPage; private int totalRecord; private int currentPage; /** * the begin index in total record for current page */ private int beginIndex; /** * @roseuid 46C0080B02C8 */ public Page(boolean hasPrePage, boolean hasNextPage, int pageSize, int totalPage, int totalRecord, int currentPage, int beginIndex) { this.hasPrePage = hasPrePage; this.hasNextPage = hasNextPage; this.pageSize = pageSize; this.totalPage = totalPage; this.totalRecord = totalRecord; this.currentPage = currentPage; this.beginIndex = beginIndex; } public Page() { } public int getBeginIndex() { return beginIndex; } public void setBeginIndex(int beginIndex) { this.beginIndex = beginIndex; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public Boolean hasNextPage() { return hasNextPage; } public void setHasNextPage(Boolean hasNextPage) { this.hasNextPage = hasNextPage; } public Boolean hasPrePage() { return hasPrePage; } public void setHasPrePage(Boolean hasPrePage) { this.hasPrePage = hasPrePage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; }}
[解决办法]
这个数据库层 就用hibernate的
openSession().createQuery("").setFirstResult(1).setMaxResults(10).list()咯
[解决办法]
如果是用ssh的话,那么关于分页的支持,spring提供的hibernate支持类里面已经提供两个方法,你只要传两个参数就OK了。这样你在逻辑层和数据层之间只是对传递两个参数,但是对于分页,这两个参数是必须的。具体的代码就像一楼的代码那样,但是有些计算其实也是可以放到这个model中的,比如只要给了总记录数,和每页要显示的数,那么剩下的计算,比如有多少页之类的,都是在这个model里面自动完成的。
[解决办法]
[解决办法]
--包
create or replace package pkg_query as
type cur_query is ref cursor;
end pkg_query;
--过程
CREATE OR REPLACE PROCEDURE "PRC_QUERY" (p_tableName
in varchar2, --表名
p_strWhere in varchar2, --查询条件
p_orderColumn in varchar2, --排序的列
p_orderStyle in varchar2, --排序方式
p_curPage in out Number, --当前页
p_pageSize in out Number, --每页显示记录条数
p_totalRecords out Number, --总记录数
p_totalPages out Number, --总页数
v_cur out pkg_query.cur_query) --返回的结果集
IS
v_sql VARCHAR2(1000) := ''; --sql语句
v_startRecord Number(4); --开始显示的记录条数
v_endRecord Number(4); --结束显示的记录条数
BEGIN
--记录中总记录条数
v_sql := 'SELECT TO_NUMBER(COUNT(*)) FROM ' || p_tableName || ' WHERE 1=1';
IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN
v_sql := v_sql || p_strWhere;
END IF;
EXECUTE IMMEDIATE v_sql INTO p_totalRecords;
--验证页面记录大小
IF p_pageSize < 0 THEN
p_pageSize := 0;
END IF;
--根据页大小计算总页数
IF MOD(p_totalRecords,p_pageSize) = 0 THEN
p_totalPages := p_totalRecords / p_pageSize;
ELSE
p_totalPages := p_totalRecords / p_pageSize + 1;
END IF;
--验证页号
IF p_curPage < 1 THEN
p_curPage := 1;
END IF;
IF p_curPage > p_totalPages THEN
p_curPage := p_totalPages;
END IF;
--实现分页查询
v_startRecord := (p_curPage - 1) * p_pageSize + 1;
v_endRecord := p_curPage * p_pageSize;
v_sql := 'SELECT * FROM (SELECT A.*, rownum r FROM ' ||
'(SELECT * FROM ' || p_tableName;
IF p_strWhere IS NOT NULL or p_strWhere <> '' THEN
v_sql := v_sql || ' WHERE 1=1' || p_strWhere;
END IF;
IF p_orderColumn IS NOT NULL or p_orderColumn <> '' THEN
v_sql := v_sql || ' ORDER BY ' || p_orderColumn || ' ' || p_orderStyle;
END IF;
v_sql := v_sql || ') A WHERE rownum <= ' || v_endRecord || ') B WHERE r >= '
|| v_startRecord;
DBMS_OUTPUT.put_line(v_sql);
OPEN v_cur FOR v_sql;
END prc_query;
//java的一个分页类
package XXT.DB;
import java.sql.*;
import java.util.*;
import oracle.jdbc.driver.OracleTypes;
public class BestOraclePageBean {
//获得数据需要的参数
private Connection conn;//数据库连接对象
private String s_tableName="";//从那个表中取数据
private String s_whereCondition="";//条件
private String s_orderColumn="";//排序字段,不能为空!
private String s_orderStyle ="ASC";//按升序还是降序排列数据,ASC 升序,DESC降序
private int i_totalRecords;//满足条件的数据库中的记录数
private int i_curPage=1;//当前页
private int i_totalPages;//如果按照每页显示的数据行数,则共可以显示的页面数
private int i_multiple=10; //一次从数据库中取出的数据,是每页显示的数据的倍数
private int i_pageSize=20;//每页显示的数据行数
private Vector pageData;//存放一次要显示的数据
public BestOraclePageBean() {
pageData=new Vector();
}
public BestOraclePageBean(Vector v) {
pageData=v;
}
public void setData(Vector v) {
pageData=v;
}
public void setConnecton(Connection con) {
this.conn=con;
}
public void setTableName(String tablename) {
this.s_tableName =tablename;
//取出字段个数
/*
try {
Statement stmt= conn.createStatement();
ResultSet rs=stmt.executeQuery("Select count( *) From Syscolumns C, Sysobjects N where N.id=C.id and N.name='"+this.s_TableName+"'");
rs.next();
i_FieldsCount=rs.getInt(1);
rs.close();
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("获得表的字段个数错误"+e.toString());
}
*/
}
public void setWhereCondition(String strWhere) {
this.s_whereCondition =strWhere;
}
public void setOrderFieldName(String orderField) {
this.s_orderColumn =orderField;
}
public void setOrderStyle(String orderStyle) {
this.s_orderStyle =orderStyle;
}
public void setPageSize(int pageSize) {
this.i_pageSize = pageSize;
}
public void setMultiple(int multiple) {
this.i_multiple = multiple;
}
public int getTotalRecords() {
return this.i_totalRecords;
}
public int getCurPage() {
return this.i_curPage;
}
public int getPageSize() {
return this.i_pageSize;
}
public int getTotalPages() {
return this.i_totalPages;
}
public int getMultiple() {
return this.i_multiple;
}
//得到每页的数据
public ResultSet getPageData(int page) {
this.i_curPage=page;
pageData.clear();
CallableStatement call = null;
ResultSet rs=null;
try {
String sql= "{ call prc_query(?,?,?,?,?,?,?,?,?) }";
call = conn.prepareCall(sql);
call.setString(1,this.s_tableName);
call.setString(2,this.s_whereCondition);
call.setString(3,this.s_orderColumn);
call.setString(4,this.s_orderStyle);
call.setInt(5,this.i_curPage);
call.setInt(6,this.i_pageSize);
call.registerOutParameter(5,OracleTypes.INTEGER);
call.registerOutParameter(6,OracleTypes.INTEGER);
call.registerOutParameter(7,OracleTypes.INTEGER);
call.registerOutParameter(8,OracleTypes.INTEGER);
call.registerOutParameter(9, OracleTypes.CURSOR);
call.execute();
this.i_curPage=(int)call.getInt(5);
this.i_pageSize=(int)call.getInt(6);
this.i_totalRecords=(int)call.getInt(7);
this.i_totalPages=(int)call.getInt(8);
rs=(ResultSet) call.getObject(9);
return rs;
} catch (SQLException ex) {
System.out.println("调用存储过程获得数据错误:"+ex.toString());
return null;
}
}
}
[解决办法]
以上为存储过程实现
数据库是ocacle