分页查询SQL jdbcDAO oracle JdbcTemplate 【给我发表点意见】
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultReader;
public class JdbcDAO extends JdbcTemplate
String sql = "";
String tsql = "";
int startNum = -1;
int pageSize = -1;
int count = 0;
1),获取分页数据
public List queryJdbcForOnePage(String sql, int startNum, int pageSize)
throws PioaGisException
{
this.tsql = sql;
this.startNum = startNum;
this.pageSize = pageSize;
sql = "SELECT * FROM (SELECT A.*,ROWNUM R FROM (" + sql +
") A WHERE ROWNUM <=" + (startNum + pageSize) +
") B WHERE B.R > " + startNum;
List listContent = query(sql,
new RowMapperResultReader(new DataRowMapper()));
List list = new LinkedList();
list.add(this.fieldNames);
for (int i = 0; i < listContent.size(); ++i) {
list.add(listContent.get(i));
}
return list;
}
示例:
SELECT * FROM (SELECT A.*,ROWNUM R FROM (SELECT 字段名 FROM 表名 WHERE ( 1 = 1 ) ORDER BY 字段名 DESC) A WHERE ROWNUM <=10) B WHERE B.R > 0
2),获取数据的数量
public int queryJdbcForMaxNum()
throws PioaGisException
{
String temp = this.tsql;
if (this.tsql.indexOf("ORDER BY") != -1)
temp = this.tsql.substring(this.tsql.indexOf("FROM"), this.tsql.indexOf("ORDER BY"));
else {
temp = this.tsql.substring(this.tsql.indexOf("FROM"));
}
String tempsql = "SELECT COUNT(1) " + temp;
Map map = queryJdbcForOneRow(tempsql);
int count = Integer.parseInt(map.get("COUNT(1)").toString());
return count;
}
public Map queryJdbcForOneRow(String sql)
throws PioaGisException
{
List list = query(sql, new OneRowMapper());
if ((list != null) && (list.size() > 0)) {
return (Map)list.get(0);
}
return null;
}