分页查询SQL jdbcDAO oracle JdbcTemplate 【给我发表点意见】

pfpfpfpfpf 2011-03-18

importorg.springframework.jdbc.core.JdbcTemplate;

importorg.springframework.jdbc.core.ResultSetExtractor;

importorg.springframework.jdbc.core.RowMapper;

importorg.springframework.jdbc.core.RowMapperResultReader;

publicclassJdbcDAOextendsJdbcTemplate

Stringsql="";

Stringtsql="";

intstartNum=-1;

intpageSize=-1;

intcount=0;

1),获取分页数据

publicListqueryJdbcForOnePage(Stringsql,intstartNum,intpageSize)

throwsPioaGisException

{

this.tsql=sql;

this.startNum=startNum;

this.pageSize=pageSize;

sql="SELECT*FROM(SELECTA.*,ROWNUMRFROM("+sql+

")AWHEREROWNUM<="+(startNum+pageSize)+

")BWHEREB.R>"+startNum;

ListlistContent=query(sql,

newRowMapperResultReader(newDataRowMapper()));

Listlist=newLinkedList();

list.add(this.fieldNames);

for(inti=0;i<listContent.size();++i){

list.add(listContent.get(i));

}

returnlist;

}

示例:

SELECT*FROM(SELECTA.*,ROWNUMRFROM(SELECT字段名FROM表名WHERE(1=1)ORDERBY字段名DESC)AWHEREROWNUM<=10)BWHEREB.R>0

2),获取数据的数量

publicintqueryJdbcForMaxNum()

throwsPioaGisException

{

Stringtemp=this.tsql;

if(this.tsql.indexOf("ORDERBY")!=-1)

temp=this.tsql.substring(this.tsql.indexOf("FROM"),this.tsql.indexOf("ORDERBY"));

else{

temp=this.tsql.substring(this.tsql.indexOf("FROM"));

}

Stringtempsql="SELECTCOUNT(1)"+temp;

Mapmap=queryJdbcForOneRow(tempsql);

intcount=Integer.parseInt(map.get("COUNT(1)").toString());

returncount;

}

publicMapqueryJdbcForOneRow(Stringsql)

throwsPioaGisException

{

Listlist=query(sql,newOneRowMapper());

if((list!=null)&&(list.size()>0)){

return(Map)list.get(0);

}

returnnull;

}

相关推荐